aboutsummaryrefslogtreecommitdiff
path: root/stdlib/test/test/lux/data/log.lux
blob: abc1112a230e2d01b047741af710e3a5f9d928d6 (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
##  Copyright (c) Eduardo Julian. All rights reserved.
##  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
##  If a copy of the MPL was not distributed with this file,
##  You can obtain one at http://mozilla.org/MPL/2.0/.

(;module:
  lux
  (lux [io]
       (control monad)
       (data ["&" log]
             [text "Text/" Monoid<Text> Eq<Text>]
             [number]
             [product])
       (codata function)
       pipe)
  lux/test)

(test: "Logs"
  (let [(^open "&/") (&;Monad<Log> text;Monoid<Text>)]
    ($_ seq
        (assert "Functor respects Log."
                (i.= 11 (product;right (&/map i.inc ["" 10]))))
        
        (assert "Applicative respects Log."
                (and (i.= 20 (product;right (&/wrap 20)))
                     (i.= 30 (product;right (&/apply (&/wrap (i.+ 10)) (&/wrap 20))))))
        
        (assert "Monad respects Log."
                (i.= 30 (product;right (do (&;Monad<Log> text;Monoid<Text>)
                                         [f (wrap i.+)
                                          a (wrap 10)
                                          b (wrap 20)]
                                         (wrap (f a b))))))
        
        (assert "Can log any value."
                (Text/= "YOLO" (product;left (&;log "YOLO"))))
        )))

(test: "Monad transformer"
  (let [lift (&;lift-log text;Monoid<Text> io;Monad<IO>)
        (^open "io/") io;Monad<IO>]
    (assert "Can add log functionality to any monad."
            (|> (io;run (do (&;LogT text;Monoid<Text> io;Monad<IO>)
                          [a (lift (io/wrap 123))
                           b (wrap 456)]
                          (wrap (i.+ a b))))
                (case> ["" 579] true
                       _ false)))
    ))