aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/writer.lux
blob: de65f2d47754fe30c0f65769e86b171ba029deb3 (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
62
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [abstract
     [equivalence (#+ Equivalence)]
     [monoid (#+ Monoid)]
     [monad (#+ do)]
     [\\specification
      ["$." functor (#+ Injection Comparison)]
      ["$." apply]
      ["$." monad]]]
    [control
     ["." io]]
    [data
     ["." product]
     ["." text ("#\." equivalence)]]
    [math
     ["." random]
     [number
      ["n" nat]]]]]
  [\\library
   ["." / (#+ Writer)]])

(def: (injection monoid value)
  (All [w] (-> (Monoid w) (Injection (Writer w))))
  [(\ monoid identity) value])

(def: comparison
  (All [w] (Comparison (Writer w)))
  (function (_ == [_ left] [_ right])
    (== left right)))

(def: .public test
  Test
  (do random.monad
    [log (random.ascii 1)
     left random.nat
     right random.nat]
    (<| (_.covering /._)
        (_.for [/.Writer])
        ($_ _.and
            (_.for [/.functor]
                   ($functor.spec (..injection text.monoid) ..comparison /.functor))
            (_.for [/.apply]
                   ($apply.spec (..injection text.monoid) ..comparison (/.apply text.monoid)))
            (_.for [/.monad]
                   ($monad.spec (..injection text.monoid) ..comparison (/.monad text.monoid)))

            (_.cover [/.write]
                     (text\= log
                             (product.left (/.write log))))
            (_.cover [/.with /.lift]
                     (let [lift (/.lift text.monoid io.monad)
                           (^open "io\.") io.monad]
                       (|> (io.run (do (/.with text.monoid io.monad)
                                     [a (lift (io\in left))
                                      b (in right)]
                                     (in (n.+ a b))))
                           product.right
                           (n.= (n.+ left right)))))
            ))))