aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/try.lux
blob: 159fcfd7f2b0bbc6c629c5de49e2be978ff83dc9 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]
    ["[0]" functor
     ["[1]T" \\test (.only Injection Comparison)]]
    ["[0]" apply
     ["[1]T" \\test]]
    [\\specification
     ["$[0]" monad]
     ["$[0]" equivalence]]]
   [control
    ["[0]" pipe]
    ["[0]" io]]
   [data
    ["[0]" text (.use "[1]#[0]" equivalence)]]
   [math
    ["[0]" random (.only Random)]
    [number
     ["n" nat]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" / (.only Try)]])

(def injection
  (Injection Try)
  (|>> {/.#Success}))

(def comparison
  (Comparison Try)
  (function (_ ==)
    (of (/.equivalence ==) =)))

(def .public (attempt element)
  (All (_ a) (-> (Random a) (Random (Try a))))
  (all random.or
       (random.unicode 1)
       element))

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Try
              /.#Failure /.#Success])
      (do random.monad
        [expected random.nat
         alternative (|> random.nat (random.only (|>> (n.= expected) not)))
         error (random.unicode 1)
         .let [(open "io#[0]") io.monad]])
      (all _.and
           (_.for [/.equivalence]
                  ($equivalence.spec (/.equivalence n.equivalence) (..attempt random.nat)))
           (_.for [/.functor]
                  (functorT.spec ..injection ..comparison /.functor))
           (_.for [/.apply]
                  (applyT.spec ..injection ..comparison /.apply))
           (_.for [/.monad]
                  ($monad.spec ..injection ..comparison /.monad))

           (_.coverage [/.trusted]
             (n.= expected
                  (/.trusted {/.#Success expected})))
           (_.coverage [/.of_maybe]
             (when [(/.of_maybe {.#Some expected})
                    (/.of_maybe {.#None})]
               [{/.#Success actual} {/.#Failure _}]
               (n.= expected actual)

               _
               false))
           (_.coverage [/.maybe]
             (when [(/.maybe {/.#Success expected})
                    (/.maybe (is (/.Try Nat) {/.#Failure error}))]
               [{.#Some actual} {.#None}]
               (n.= expected actual)

               _
               false))
           (_.coverage [/.else]
             (and (n.= expected
                       (/.else alternative {/.#Success expected}))
                  (n.= alternative
                       (/.else alternative (is (Try Nat) {/.#Failure error})))))
           (_.coverage [/.when]
             (`` (and (,, (with_template [<scenario>]
                            [(of (/.equivalence n.equivalence) =
                                 <scenario>
                                 (/.when true <scenario>))]

                            [{/.#Success expected}]
                            [{/.#Failure error}]
                            ))
                      (of (/.equivalence n.equivalence) =
                          (/.when false {/.#Success expected})
                          (/.when false {/.#Failure error})))))
           (_.coverage [/.with /.lifted]
             (let [lifted (/.lifted io.monad)]
               (|> (do (/.with io.monad)
                     [a (lifted (io#in expected))
                      b (in alternative)]
                     (in (n.+ a b)))
                   io.run!
                   (pipe.when
                     {/.#Success result}
                     (n.= (n.+ expected alternative)
                          result)

                     _
                     false))))
           )))