aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/macro/pattern.lux
blob: ce57f48ea3263bd870541d20c839629bb93906a3 (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
(.using
 [library
  [lux "*"
   ["_" test {"+" Test}]
   [abstract
    [monad {"+" do}]]
   [data
    ["[0]" bit ("[1]#[0]" equivalence)]]
   [macro
    ["[0]" code]]
   [math
    ["[0]" random {"+" Random}]
    [number
     ["n" nat]
     ["i" int]
     ["f" frac]]]]]
 [\\library
  ["[0]" /]])

(type: (Pair l r)
  (Record
   [#left l
    #right r]))

(template: (!pair <left> <right>)
  [[..#left <left>
    ..#right <right>]])

(def: .public test
  Test
  (<| (_.covering /._)
      (do [! random.monad]
        [expected_nat (# ! each (n.% 1) random.nat)
         expected_int (# ! each (i.% +1) random.int)
         expected_rev (random.either (in .5)
                                     (in .25))
         expected_frac (random.either (in +0.5)
                                      (in +1.25))
         expected_text (random.either (in "+0.5")
                                      (in "+1.25"))]
        (all _.and
             (do [! random.monad]
               [sample (# ! each (n.% 5) random.nat)]
               (_.cover [/.template]
                        (case sample
                          (/.template [<case>]
                            [<case> true])
                          ([0] [1] [2] [3] [4])

                          _
                          false)))
             (_.cover [/.or]
                      (and (/.case expected_rev
                             (/.or .5 .25) true
                             _ false)
                           (/.case expected_frac
                             (/.or +0.5 +1.25) true
                             _ false)
                           (/.case expected_text
                             (/.or "+0.5" "+1.25") true
                             _ false)))
             (_.cover [/.let]
                      (let [expected_pair (is (Pair Nat Int)
                                              [..#left expected_nat ..#right expected_int])]
                        (/.case expected_pair
                          (/.let actual_pair (/.pattern (!pair actual_left actual_right)))
                          (and (/.same? expected_pair actual_pair)
                               (/.same? expected_nat actual_left)
                               (/.same? expected_int actual_right)))))
             (_.cover [/.multi]
                      (let [expected_pair (is (Pair Nat Int)
                                              [..#left expected_nat ..#right expected_int])]
                        (and (/.case expected_pair
                               (/.multi (/.pattern (!pair 0 actual_right))
                                        [actual_right
                                         +0])
                               true

                               _
                               false)
                             (/.case expected_pair
                               (/.multi (/.pattern (!pair 0 actual_right))
                                        (i.= +0 actual_right))
                               true

                               _
                               false))))
             (_.cover [/.|>]
                      (case expected_frac
                        (/.|> actual_frac [(f.* +2.0) (f.* +2.0)])
                        (f.= (f.* +4.0 expected_frac)
                             actual_frac)))
             (_.cover [/.`]
                      (case (code.text expected_text)
                        (/.` "+0.5") true
                        (/.` "+1.25") true
                        _ false))
             ))))