aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/parser/type.lux
blob: 99e995f2d63062989e0ece5ec9d6b9b773a0d9d7 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
(.module:
  [lux (#- primitive)
   ["_" test (#+ Test)]
   [abstract
    [monad (#+ do)]]
   [control
    ["." try]
    ["." exception]]
   [data
    ["." name ("#@." equivalence)]
    [number
     ["n" nat]]]
   [math
    ["." random (#+ Random)]]
   ["." type ("#@." equivalence)]]
  {1
   ["." /
    ["/#" //]]})

(template: (!expect <pattern> <value>)
  (case <value>
    <pattern>
    true
    
    _
    false))

(def: primitive
  (Random Type)
  (|> (random.ascii/alpha-num 1)
      (:: random.monad map (function (_ name)
                             (#.Primitive name (list))))))

(def: matches
  Test
  (<| (_.with-cover [/.types-do-not-match])
      (do {@ random.monad}
        [expected ..primitive
         dummy (random.filter (|>> (type@= expected) not)
                              ..primitive)])
      ($_ _.and
          (_.cover [/.exactly]
                   (and (|> (/.run (/.exactly expected) expected)
                            (!expect (#try.Success [])))
                        (|> (/.run (/.exactly expected) dummy)
                            (!expect (^multi (#try.Failure error)
                                             (exception.match? /.types-do-not-match error))))))
          (_.cover [/.sub]
                   (and (|> (/.run (/.sub expected) expected)
                            (!expect (#try.Success [])))
                        (|> (/.run (/.sub Any) expected)
                            (!expect (#try.Success [])))
                        (|> (/.run (/.sub expected) Nothing)
                            (!expect (#try.Success [])))
                        (|> (/.run (/.sub expected) dummy)
                            (!expect (^multi (#try.Failure error)
                                             (exception.match? /.types-do-not-match error))))))
          (_.cover [/.super]
                   (and (|> (/.run (/.super expected) expected)
                            (!expect (#try.Success [])))
                        (|> (/.run (/.super expected) Any)
                            (!expect (#try.Success [])))
                        (|> (/.run (/.super Nothing) expected)
                            (!expect (#try.Success [])))
                        (|> (/.run (/.super expected) dummy)
                            (!expect (^multi (#try.Failure error)
                                             (exception.match? /.types-do-not-match error))))))
          )))

(def: aggregate
  Test
  (do {@ random.monad}
    [expected-left ..primitive
     expected-middle ..primitive
     expected-right ..primitive]
    (`` ($_ _.and
            (~~ (template [<parser> <exception> <good-constructor> <bad-constructor>]
                  [(_.cover [<parser> <exception>]
                            (and (|> (/.run (<parser> ($_ //.and /.any /.any /.any))
                                            (<good-constructor> (list expected-left expected-middle expected-right)))
                                     (!expect (^multi (#try.Success [actual-left actual-middle actual-right])
                                                      (and (type@= expected-left actual-left)
                                                           (type@= expected-middle actual-middle)
                                                           (type@= expected-right actual-right)))))
                                 (|> (/.run (<parser> ($_ //.and /.any /.any /.any))
                                            (<bad-constructor> (list expected-left expected-middle expected-right)))
                                     (!expect (^multi (#try.Failure error)
                                                      (exception.match? <exception> error))))))]

                  [/.variant /.not-variant type.variant type.tuple]
                  [/.tuple /.not-tuple type.tuple type.variant]
                  ))

            (_.cover [/.function /.not-function]
                     (and (|> (/.run (/.function ($_ //.and /.any /.any) /.any)
                                     (type.function (list expected-left expected-middle) expected-right))
                              (!expect (^multi (#try.Success [[actual-left actual-middle] actual-right])
                                               (and (type@= expected-left actual-left)
                                                    (type@= expected-middle actual-middle)
                                                    (type@= expected-right actual-right)))))
                          (|> (/.run (/.function ($_ //.and /.any /.any) /.any)
                                     (type.variant (list expected-left expected-middle expected-right)))
                              (!expect (^multi (#try.Failure error)
                                               (exception.match? /.not-function error))))))
            (_.cover [/.apply /.not-application]
                     (and (|> (/.run (/.apply ($_ //.and /.any /.any /.any))
                                     (type.application (list expected-middle expected-right) expected-left))
                              (!expect (^multi (#try.Success [actual-left actual-middle actual-right])
                                               (and (type@= expected-left actual-left)
                                                    (type@= expected-middle actual-middle)
                                                    (type@= expected-right actual-right)))))
                          (|> (/.run (/.apply ($_ //.and /.any /.any /.any))
                                     (type.variant (list expected-left expected-middle expected-right)))
                              (!expect (^multi (#try.Failure error)
                                               (exception.match? /.not-application error))))))
            ))))

(def: #export test
  Test
  (<| (_.covering /._)
      (_.with-cover [/.Parser])
      ($_ _.and
          (do {@ random.monad}
            [expected ..primitive]
            (_.cover [/.run /.any]
                     (|> (/.run /.any expected)
                         (!expect (^multi (#try.Success actual)
                                          (type@= expected actual))))))
          (do {@ random.monad}
            [expected ..primitive]
            (_.cover [/.peek /.unconsumed-input]
                     (and (|> (/.run (do //.monad
                                       [actual /.peek
                                        _ /.any]
                                       (wrap actual))
                                     expected)
                              (!expect (^multi (#try.Success actual)
                                               (type@= expected actual))))
                          (|> (/.run /.peek expected)
                              (!expect (^multi (#try.Failure error)
                                               (exception.match? /.unconsumed-input error)))))))
          (do {@ random.monad}
            [expected ..primitive]
            (_.cover [/.empty-input]
                     (`` (and (~~ (template [<parser>]
                                    [(|> (/.run (do //.monad
                                                  [_ /.any]
                                                  <parser>)
                                                expected)
                                         (!expect (^multi (#try.Failure error)
                                                          (exception.match? /.empty-input error))))]

                                    [/.any]
                                    [/.peek]
                                    ))))))
          (do {@ random.monad}
            [expected ..primitive]
            (_.cover [/.Env /.env /.fresh]
                     (|> (/.run (do //.monad
                                  [env /.env
                                   _ /.any]
                                  (wrap env))
                                expected)
                         (!expect (^multi (#try.Success environment)
                                          (is? /.fresh environment))))))
          (do {@ random.monad}
            [expected ..primitive
             dummy (random.filter (|>> (type@= expected) not)
                                  ..primitive)]
            (_.cover [/.local]
                     (|> (/.run (do //.monad
                                  [_ /.any]
                                  (/.local (list expected)
                                           /.any))
                                dummy)
                         (!expect (^multi (#try.Success actual)
                                          (type@= expected actual))))))
          (do {@ random.monad}
            [expected random.nat]
            (_.cover [/.existential /.not-existential]
                     (|> (/.run /.existential
                                (#.Ex expected))
                         (!expect (^multi (#try.Success actual)
                                          (n.= expected actual))))))
          (do {@ random.monad}
            [expected-name (random.and (random.ascii/alpha-num 1)
                                       (random.ascii/alpha-num 1))
             expected-type ..primitive]
            (_.cover [/.named /.not-named]
                     (|> (/.run /.named
                                (#.Named expected-name expected-type))
                         (!expect (^multi (#try.Success [actual-name actual-type])
                                          (and (name@= expected-name actual-name)
                                               (type@= expected-type actual-type)))))))
          ..aggregate
          ..matches
          )))