aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/error.lux
blob: 78e63338e9714a567a44baa297e960578e6bef41 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(.module:
  [lux #*
   ["." io]
   [control
    [monad (#+ do Monad)]
    pipe]
   [data
    ["/" error (#+ Error)]]]
  lux/test)

(context: "Errors"
  (let [(^open "&;.") /.apply
        (^open "&;.") /.monad]
    ($_ seq
        (test "Functor correctly handles both cases."
              (and (|> (: (Error Int) (#/.Success +10))
                       (&;map inc)
                       (case> (#/.Success +11) #1 _ #0))

                   (|> (: (Error Int) (#/.Failure "YOLO"))
                       (&;map inc)
                       (case> (#/.Failure "YOLO") #1 _ #0))
                   ))
        
        (test "Apply correctly handles both cases."
              (and (|> (&;wrap +20)
                       (case> (#/.Success +20) #1 _ #0))
                   (|> (&;apply (&;wrap inc) (&;wrap +10))
                       (case> (#/.Success +11) #1 _ #0))
                   (|> (&;apply (&;wrap inc) (#/.Failure "YOLO"))
                       (case> (#/.Failure "YOLO") #1 _ #0))))
        
        (test "Monad correctly handles both cases."
              (and (|> (do /.monad
                         [f (wrap i/+)
                          a (wrap +10)
                          b (wrap +20)]
                         (wrap (f a b)))
                       (case> (#/.Success +30) #1 _ #0))
                   (|> (do /.monad
                         [f (wrap i/+)
                          a (#/.Failure "YOLO")
                          b (wrap +20)]
                         (wrap (f a b)))
                       (case> (#/.Failure "YOLO") #1 _ #0))
                   ))
        )))

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