aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/writer.lux
blob: b5fb372d8c1eba548ea9a94486c90db376b7ddae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(.module:
  [lux #*
   ["." io]
   [control
    ["M" monad (#+ Monad do)]
    pipe
    ["&" writer]]
   [data
    ["." product]
    ["." text ("text/." equivalence)]]]
  lux/test)

(context: "Writer."
  (let [(^open "&/.") (&.monad text.monoid)
        (^open "&/.") (&.apply text.monoid)]
    ($_ seq
        (test "Functor respects Writer."
              (i/= +11 (product.right (&/map inc ["" +10]))))
        
        (test "Apply respects Writer."
              (and (i/= +20 (product.right (&/wrap +20)))
                   (i/= +30 (product.right (&/apply (&/wrap (i/+ +10)) (&/wrap +20))))))
        
        (test "Monad respects Writer."
              (i/= +30 (product.right (do (&.monad text.monoid)
                                        [f (wrap i/+)
                                         a (wrap +10)
                                         b (wrap +20)]
                                        (wrap (f a b))))))
        
        (test "Can log any value."
              (text/= "YOLO" (product.left (&.log "YOLO"))))
        )))

(context: "Monad transformer"
  (let [lift (&.lift text.monoid io.monad)
        (^open "io/.") io.monad]
    (test "Can add writer functionality to any monad."
          (|> (io.run (do (&.WriterT text.monoid io.monad)
                        [a (lift (io/wrap +123))
                         b (wrap +456)]
                        (wrap (i/+ a b))))
              (case> ["" +579] #1
                     _         #0)))
    ))