aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/parser/type.lux
blob: 3d27819bdf8796ae2abe52da8ebc4e103335b9a9 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
(.module:
  [library
   [lux {"-" [primitive]}
    ["_" test {"+" [Test]}]
    [abstract
     [monad {"+" [do]}]]
    [control
     ["[0]" try]
     ["[0]" exception]]
    [data
     ["[0]" name ("[1]\[0]" equivalence)]
     [collection
      ["[0]" list]]]
    [math
     ["[0]" random {"+" [Random]}]
     [number
      ["n" nat]]]
    ["[0]" type ("[1]\[0]" equivalence)]]]
  [\\library
   ["[0]" /
    ["/[1]" //]]])

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

(def: primitive
  (Random Type)
  (|> (random.ascii/alpha_num 1)
      (\ random.monad each (function (_ name)
                             {#.Primitive name (list)}))))

(def: matches
  Test
  (<| (_.for [/.types_do_not_match])
      (do [! random.monad]
        [expected ..primitive
         dummy (random.only (|>> (type\= expected) not)
                            ..primitive)])
      ($_ _.and
          (_.cover [/.exactly]
                   (and (|> (/.result (/.exactly expected) expected)
                            (!expect {#try.Success []}))
                        (|> (/.result (/.exactly expected) dummy)
                            (!expect (^multi {#try.Failure error}
                                             (exception.match? /.types_do_not_match error))))))
          (_.cover [/.sub]
                   (and (|> (/.result (/.sub expected) expected)
                            (!expect {#try.Success []}))
                        (|> (/.result (/.sub Any) expected)
                            (!expect {#try.Success []}))
                        (|> (/.result (/.sub expected) Nothing)
                            (!expect {#try.Success []}))
                        (|> (/.result (/.sub expected) dummy)
                            (!expect (^multi {#try.Failure error}
                                             (exception.match? /.types_do_not_match error))))))
          (_.cover [/.super]
                   (and (|> (/.result (/.super expected) expected)
                            (!expect {#try.Success []}))
                        (|> (/.result (/.super expected) Any)
                            (!expect {#try.Success []}))
                        (|> (/.result (/.super Nothing) expected)
                            (!expect {#try.Success []}))
                        (|> (/.result (/.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 (|> (/.result (<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)))))
                                 (|> (/.result (<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 (|> (/.result (/.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)))))
                          (|> (/.result (/.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 [/.applied /.not_application]
                     (and (|> (/.result (/.applied ($_ //.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)))))
                          (|> (/.result (/.applied ($_ //.and /.any /.any /.any))
                                        (type.variant (list expected_left expected_middle expected_right)))
                              (!expect (^multi {#try.Failure error}
                                               (exception.match? /.not_application error))))))
            ))))

(def: parameter
  Test
  (do random.monad
    [quantification ..primitive
     argument ..primitive
     not_parameter ..primitive
     parameter random.nat]
    ($_ _.and
        (_.cover [/.not_parameter]
                 (|> (/.result /.parameter not_parameter)
                     (!expect (^multi {#try.Failure error}
                                      (exception.match? /.not_parameter error)))))
        (_.cover [/.unknown_parameter]
                 (|> (/.result /.parameter {#.Parameter parameter})
                     (!expect (^multi {#try.Failure error}
                                      (exception.match? /.unknown_parameter error)))))
        (_.cover [/.with_extension]
                 (|> (/.result (<| (/.with_extension quantification)
                                   (/.with_extension argument)
                                   /.any)
                               not_parameter)
                     (!expect (^multi {#try.Success [quantification\\binding argument\\binding actual]}
                                      (same? not_parameter actual)))))
        (_.cover [/.parameter]
                 (|> (/.result (<| (/.with_extension quantification)
                                   (/.with_extension argument)
                                   /.parameter)
                               {#.Parameter 0})
                     (!expect {#try.Success [quantification\\binding argument\\binding _]})))
        (_.cover [/.wrong_parameter]
                 (|> (/.result (<| (/.with_extension quantification)
                                   (/.with_extension argument)
                                   (/.parameter! 1))
                               {#.Parameter 0})
                     (!expect (^multi {#try.Failure error}
                                      (exception.match? /.wrong_parameter error)))))
        (_.cover [/.parameter!]
                 (|> (/.result (<| (/.with_extension quantification)
                                   (/.with_extension argument)
                                   (/.parameter! 0))
                               {#.Parameter 0})
                     (!expect {#try.Success [quantification\\binding argument\\binding _]})))
        )))

(def: polymorphic
  Test
  (do [! random.monad]
    [not_polymorphic ..primitive
     expected_inputs (\ ! each (|>> (n.% 10) ++) random.nat)]
    ($_ _.and
        (_.cover [/.not_polymorphic]
                 (and (|> (/.result (/.polymorphic /.any)
                                    not_polymorphic)
                          (!expect (^multi {#try.Failure error}
                                           (exception.match? /.not_polymorphic error))))
                      (|> (/.result (/.polymorphic /.any)
                                    (type.univ_q 0 not_polymorphic))
                          (!expect (^multi {#try.Failure error}
                                           (exception.match? /.not_polymorphic error))))))
        (_.cover [/.polymorphic]
                 (|> (/.result (/.polymorphic /.any)
                               (type.univ_q expected_inputs not_polymorphic))
                     (!expect (^multi {#try.Success [g!poly actual_inputs bodyT]}
                                      (and (n.= expected_inputs (list.size actual_inputs))
                                           (same? not_polymorphic bodyT))))))
        )))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Parser])
      ($_ _.and
          (do [! random.monad]
            [expected ..primitive]
            (_.cover [/.result /.any]
                     (|> (/.result /.any expected)
                         (!expect (^multi {#try.Success actual}
                                          (type\= expected actual))))))
          (do [! random.monad]
            [expected ..primitive]
            (_.cover [/.next /.unconsumed_input]
                     (and (|> (/.result (do //.monad
                                          [actual /.next
                                           _ /.any]
                                          (in actual))
                                        expected)
                              (!expect (^multi {#try.Success actual}
                                               (type\= expected actual))))
                          (|> (/.result /.next expected)
                              (!expect (^multi {#try.Failure error}
                                               (exception.match? /.unconsumed_input error)))))))
          (do [! random.monad]
            [expected ..primitive]
            (_.cover [/.empty_input]
                     (`` (and (~~ (template [<parser>]
                                    [(|> (/.result (do //.monad
                                                     [_ /.any]
                                                     <parser>)
                                                   expected)
                                         (!expect (^multi {#try.Failure error}
                                                          (exception.match? /.empty_input error))))]

                                    [/.any]
                                    [/.next]
                                    ))))))
          (do [! random.monad]
            [expected ..primitive]
            (_.cover [/.Env /.env /.fresh]
                     (|> (/.result (do //.monad
                                     [env /.env
                                      _ /.any]
                                     (in env))
                                   expected)
                         (!expect (^multi {#try.Success environment}
                                          (same? /.fresh environment))))))
          (do [! random.monad]
            [expected ..primitive
             dummy (random.only (|>> (type\= expected) not)
                                ..primitive)]
            (_.cover [/.local]
                     (|> (/.result (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]
                     (|> (/.result /.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]
                     (|> (/.result /.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
          ..parameter
          ..polymorphic
          )))