aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/reader.lux
blob: 59763c0e85faa5c1ee01e3c78e2056ed250049b6 (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
(.module:
  [lux #*
   ["." io (#+ IO)]
   ["_" test (#+ Test)]
   [control
    [monad (#+ do)]
    {[0 #test]
     [/
      ["$." functor (#+ Injection Comparison)]
      ["$." apply]
      ["$." monad]]}]
   [data
    [text
     format]]
   [math
    ["r" random]]]
  {1
   ["." / (#+ Reader)]})

(def: (injection value)
  (Injection (All [a r] (Reader r a)))
  (function (_ env)
    value))

(def: comparison
  (Comparison (All [a r] (Reader r a)))
  (function (_ == left right)
    (== (/.run [] left) (/.run [] right))))

(def: #export test
  Test
  (<| (_.context (%name (name-of /.Reader)))
      (do r.monad
        [sample r.nat
         factor r.nat]
        ($_ _.and
            ($functor.spec ..injection ..comparison /.functor)
            ($apply.spec ..injection ..comparison /.apply)
            ($monad.spec ..injection ..comparison /.monad)

            (_.test "Can query the environment."
                    (n/= sample
                         (/.run sample /.ask)))
            (_.test "Can modify an environment locally."
                    (n/= (n/* factor sample)
                         (/.run sample (/.local (n/* factor) /.ask))))
            (let [(^open "io@.") io.monad]
              (_.test "Can add reader functionality to any monad."
                      (|> (: (/.Reader Any (IO Nat))
                             (do (/.with io.monad)
                               [a (/.lift (io@wrap sample))
                                b (wrap factor)]
                               (wrap (n/* b a))))
                          (/.run [])
                          io.run
                          (n/= (n/* factor sample)))))))))