aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/parser/text.lux
blob: 7207e447712868823a886c102c0f66e0d3cc31a8 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
(.module:
  [library
   [lux "*"
    ["_" test {"+" [Test]}]
    [abstract
     [monad {"+" [do]}]]
    [control
     ["." maybe]
     ["." try {"+" [Try]}]
     ["." exception {"+" [Exception]}]
     ["." function]]
    [data
     ["." text ("#\." equivalence)
      ["%" format {"+" [format]}]
      ["." unicode "_"
       ["#" set]
       ["#/." block]]]
     [collection
      ["." set]
      ["." list ("#\." functor)]
      [tree
       ["." finger]]]]
    [math
     ["." random]
     [number {"+" [hex]}
      ["n" nat]]]
    [macro
     ["." code]]]]
  [\\library
   ["." /
    ["<>" //
     ["<c>" code]]]])

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

(def: (should_fail' sample parser exception)
  (All (_ a e) (-> Text (/.Parser a) (Exception e) Bit))
  (case (/.result parser sample)
    (#try.Failure error)
    (exception.match? exception error)
    
    _
    false))

(def: (should_fail sample parser)
  (All (_ a) (-> Text (/.Parser a) Bit))
  (case (/.result parser sample)
    (#try.Failure _)
    true
    
    _
    false))

(def: (should_pass expected parser)
  (-> Text (/.Parser Text) Bit)
  (|> expected
      (/.result parser)
      (\ try.functor each (text\= expected))
      (try.else false)))

(def: (should_pass! expected parser)
  (-> Text (/.Parser /.Slice) Bit)
  (..should_pass expected (/.slice parser)))

(def: character_classes
  Test
  ($_ _.and
      (do [! random.monad]
        [offset (\ ! each (n.% 50) random.nat)
         range (\ ! each (|>> (n.% 50) (n.+ 10)) random.nat)
         .let [limit (n.+ offset range)]
         expected (\ ! each (|>> (n.% range) (n.+ offset) text.of_char) random.nat)
         out_of_range (case offset
                        0 (\ ! each (|>> (n.% 10) ++ (n.+ limit) text.of_char) random.nat)
                        _ (\ ! each (|>> (n.% offset) text.of_char) random.nat))]
        (_.cover [/.range]
                 (and (..should_pass expected (/.range offset limit))
                      (..should_fail out_of_range (/.range offset limit)))))
      (do [! random.monad]
        [expected (random.char unicode.ascii/upper)
         invalid (random.only (|>> (unicode/block.within? unicode/block.basic_latin/upper) not)
                              (random.char unicode.character))]
        (_.cover [/.upper]
                 (and (..should_pass (text.of_char expected) /.upper)
                      (..should_fail (text.of_char invalid) /.upper))))
      (do [! random.monad]
        [expected (random.char unicode.ascii/lower)
         invalid (random.only (|>> (unicode/block.within? unicode/block.basic_latin/lower) not)
                              (random.char unicode.character))]
        (_.cover [/.lower]
                 (and (..should_pass (text.of_char expected) /.lower)
                      (..should_fail (text.of_char invalid) /.lower))))
      (do [! random.monad]
        [expected (\ ! each (n.% 10) random.nat)
         invalid (random.char (unicode.set [unicode/block.number_forms (list)]))]
        (_.cover [/.decimal]
                 (and (..should_pass (\ n.decimal encoded expected) /.decimal)
                      (..should_fail (text.of_char invalid) /.decimal))))
      (do [! random.monad]
        [expected (\ ! each (n.% 8) random.nat)
         invalid (random.char (unicode.set [unicode/block.number_forms (list)]))]
        (_.cover [/.octal]
                 (and (..should_pass (\ n.octal encoded expected) /.octal)
                      (..should_fail (text.of_char invalid) /.octal))))
      (do [! random.monad]
        [expected (\ ! each (n.% 16) random.nat)
         invalid (random.char (unicode.set [unicode/block.number_forms (list)]))]
        (_.cover [/.hexadecimal]
                 (and (..should_pass (\ n.hex encoded expected) /.hexadecimal)
                      (..should_fail (text.of_char invalid) /.hexadecimal))))
      (do [! random.monad]
        [expected (random.char unicode.ascii/alpha)
         invalid (random.only (function (_ char)
                                (not (or (unicode/block.within? unicode/block.basic_latin/upper char)
                                         (unicode/block.within? unicode/block.basic_latin/lower char))))
                              (random.char unicode.character))]
        (_.cover [/.alpha]
                 (and (..should_pass (text.of_char expected) /.alpha)
                      (..should_fail (text.of_char invalid) /.alpha))))
      (do [! random.monad]
        [expected (random.char unicode.ascii/alpha_num)
         invalid (random.only (function (_ char)
                                (not (or (unicode/block.within? unicode/block.basic_latin/upper char)
                                         (unicode/block.within? unicode/block.basic_latin/lower char)
                                         (unicode/block.within? unicode/block.basic_latin/decimal char))))
                              (random.char unicode.character))]
        (_.cover [/.alpha_num]
                 (and (..should_pass (text.of_char expected) /.alpha_num)
                      (..should_fail (text.of_char invalid) /.alpha_num))))
      (do [! random.monad]
        [expected ($_ random.either
                      (in text.tab)
                      (in text.vertical_tab)
                      (in text.space)
                      (in text.new_line)
                      (in text.carriage_return)
                      (in text.form_feed))
         invalid (|> (random.unicode 1) (random.only (function (_ char)
                                                       (not (or (text\= text.tab char)
                                                                (text\= text.vertical_tab char)
                                                                (text\= text.space char)
                                                                (text\= text.new_line char)
                                                                (text\= text.carriage_return char)
                                                                (text\= text.form_feed char))))))]
        (_.cover [/.space]
                 (and (..should_pass expected /.space)
                      (..should_fail invalid /.space))))
      (do [! random.monad]
        [.let [num_options 3]
         options (|> (random.char unicode.character)
                     (random.set n.hash num_options)
                     (\ ! each (|>> set.list
                                    (list\each text.of_char)
                                    text.together)))
         expected (\ ! each (function (_ value)
                              (|> options
                                  (text.char (n.% num_options value))
                                  maybe.trusted))
                     random.nat)
         invalid (random.only (function (_ char)
                                (not (text.contains? (text.of_char char) options)))
                              (random.char unicode.character))]
        (_.cover [/.one_of /.one_of! /.character_should_be]
                 (and (..should_pass (text.of_char expected) (/.one_of options))
                      (..should_fail (text.of_char invalid) (/.one_of options))
                      (..should_fail' (text.of_char invalid) (/.one_of options)
                                      /.character_should_be)

                      (..should_pass! (text.of_char expected) (/.one_of! options))
                      (..should_fail (text.of_char invalid) (/.one_of! options))
                      (..should_fail' (text.of_char invalid) (/.one_of! options)
                                      /.character_should_be)
                      )))
      (do [! random.monad]
        [.let [num_options 3]
         options (|> (random.char unicode.character)
                     (random.set n.hash num_options)
                     (\ ! each (|>> set.list
                                    (list\each text.of_char)
                                    text.together)))
         invalid (\ ! each (function (_ value)
                             (|> options
                                 (text.char (n.% num_options value))
                                 maybe.trusted))
                    random.nat)
         expected (random.only (function (_ char)
                                 (not (text.contains? (text.of_char char) options)))
                               (random.char unicode.character))]
        (_.cover [/.none_of /.none_of! /.character_should_not_be]
                 (and (..should_pass (text.of_char expected) (/.none_of options))
                      (..should_fail (text.of_char invalid) (/.none_of options))
                      (..should_fail' (text.of_char invalid) (/.none_of options)
                                      /.character_should_not_be)

                      (..should_pass! (text.of_char expected) (/.none_of! options))
                      (..should_fail (text.of_char invalid) (/.none_of! options))
                      (..should_fail' (text.of_char invalid) (/.none_of! options)
                                      /.character_should_not_be)
                      )))
      ))

(def: runs
  Test
  (let [octal! (/.one_of! "01234567")]
    ($_ _.and
        (do [! random.monad]
          [left (\ ! each (|>> (n.% 8) (\ n.octal encoded)) random.nat)
           right (\ ! each (|>> (n.% 8) (\ n.octal encoded)) random.nat)
           .let [expected (format left right)]
           invalid (|> random.nat
                       (\ ! each (n.% 16))
                       (random.only (n.>= 8))
                       (\ ! each (\ n.hex encoded)))]
          (_.cover [/.many /.many!]
                   (and (..should_pass expected (/.many /.octal))
                        (..should_fail invalid (/.many /.octal))

                        (..should_pass! expected (/.many! octal!)))))
        (do [! random.monad]
          [left (\ ! each (|>> (n.% 8) (\ n.octal encoded)) random.nat)
           right (\ ! each (|>> (n.% 8) (\ n.octal encoded)) random.nat)
           .let [expected (format left right)]
           invalid (|> random.nat
                       (\ ! each (n.% 16))
                       (random.only (n.>= 8))
                       (\ ! each (\ n.hex encoded)))]
          (_.cover [/.some /.some!]
                   (and (..should_pass expected (/.some /.octal))
                        (..should_pass "" (/.some /.octal))
                        (..should_fail invalid (/.some /.octal))

                        (..should_pass! expected (/.some! octal!))
                        (..should_pass! "" (/.some! octal!)))))
        (do [! random.monad]
          [.let [octal (\ ! each (|>> (n.% 8) (\ n.octal encoded)) random.nat)]
           first octal
           second octal
           third octal]
          (_.cover [/.exactly /.exactly!]
                   (and (..should_pass (format first second) (/.exactly 2 /.octal))
                        (..should_fail (format first second third) (/.exactly 2 /.octal))
                        (..should_fail (format first) (/.exactly 2 /.octal))

                        (..should_pass! (format first second) (/.exactly! 2 octal!))
                        (..should_fail (format first second third) (/.exactly! 2 octal!))
                        (..should_fail (format first) (/.exactly! 2 octal!)))))
        (do [! random.monad]
          [.let [octal (\ ! each (|>> (n.% 8) (\ n.octal encoded)) random.nat)]
           first octal
           second octal
           third octal]
          (_.cover [/.at_most /.at_most!]
                   (and (..should_pass (format first second) (/.at_most 2 /.octal))
                        (..should_pass (format first) (/.at_most 2 /.octal))
                        (..should_fail (format first second third) (/.at_most 2 /.octal))

                        (..should_pass! (format first second) (/.at_most! 2 octal!))
                        (..should_pass! (format first) (/.at_most! 2 octal!))
                        (..should_fail (format first second third) (/.at_most! 2 octal!)))))
        (do [! random.monad]
          [.let [octal (\ ! each (|>> (n.% 8) (\ n.octal encoded)) random.nat)]
           first octal
           second octal
           third octal]
          (_.cover [/.at_least /.at_least!]
                   (and (..should_pass (format first second) (/.at_least 2 /.octal))
                        (..should_pass (format first second third) (/.at_least 2 /.octal))
                        (..should_fail (format first) (/.at_least 2 /.octal))

                        (..should_pass! (format first second) (/.at_least! 2 octal!))
                        (..should_pass! (format first second third) (/.at_least! 2 octal!))
                        (..should_fail (format first) (/.at_least! 2 octal!)))))
        (do [! random.monad]
          [.let [octal (\ ! each (|>> (n.% 8) (\ n.octal encoded)) random.nat)]
           first octal
           second octal
           third octal]
          (_.cover [/.between /.between!]
                   (and (..should_pass (format first second) (/.between 2 1 /.octal))
                        (..should_pass (format first second third) (/.between 2 1 /.octal))
                        (..should_fail (format first) (/.between 2 1 /.octal))

                        (..should_pass! (format first second) (/.between! 2 1 octal!))
                        (..should_pass! (format first second third) (/.between! 2 1 octal!))
                        (..should_fail (format first) (/.between! 2 1 octal!)))))
        )))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Parser])
      ($_ _.and
          (do [! random.monad]
            [sample (random.unicode 1)]
            (_.cover [/.result /.end!]
                     (and (|> (/.result /.end!
                                        "")
                              (!expect (#try.Success _)))
                          (|> (/.result /.end!
                                        sample)
                              (!expect (#try.Failure _))))))
          (do [! random.monad]
            [.let [size 10]
             expected (random.unicode size)
             dummy (|> (random.unicode size)
                       (random.only (|>> (text\= expected) not)))]
            (_.cover [/.this /.cannot_match]
                     (and (|> (/.result (/.this expected)
                                        expected)
                              (!expect (#try.Success [])))
                          (|> (/.result (/.this expected)
                                        dummy)
                              (!expect (^multi (#try.Failure error)
                                               (exception.match? /.cannot_match error)))))))
          (_.cover [/.Slice /.slice /.cannot_slice]
                   (|> ""
                       (/.result (/.slice /.any!))
                       (!expect (^multi (#try.Failure error)
                                        (exception.match? /.cannot_slice error)))))
          (do [! random.monad]
            [expected (random.unicode 1)]
            (_.cover [/.any /.any!]
                     (and (..should_pass expected /.any)
                          (..should_fail "" /.any)

                          (..should_pass! expected /.any!)
                          (..should_fail "" /.any!))))
          (do [! random.monad]
            [expected (random.unicode 1)]
            (_.cover [/.next /.cannot_parse]
                     (and (..should_pass expected (<>.before /.any /.next))
                          (|> ""
                              (/.result (<>.before /.any /.next))
                              (!expect (^multi (#try.Failure error)
                                               (exception.match? /.cannot_parse error)))))))
          (do [! random.monad]
            [dummy (random.unicode 1)]
            (_.cover [/.unconsumed_input]
                     (|> (format dummy dummy)
                         (/.result /.any)
                         (!expect (^multi (#try.Failure error)
                                          (exception.match? /.unconsumed_input error))))))
          (do [! random.monad]
            [sample (random.unicode 1)]
            (_.cover [/.Offset /.offset]
                     (|> sample
                         (/.result (do <>.monad
                                     [pre /.offset
                                      _ /.any
                                      post /.offset]
                                     (in [pre post])))
                         (!expect (#try.Success [0 1])))))
          (do [! random.monad]
            [left (random.unicode 1)
             right (random.unicode 1)
             .let [input (format left right)]]
            (_.cover [/.remaining]
                     (|> input
                         (/.result (do <>.monad
                                     [pre /.remaining
                                      _ /.any
                                      post /.remaining
                                      _ /.any]
                                     (in (and (text\= input pre)
                                              (text\= right post)))))
                         (!expect (#try.Success #1)))))
          (do [! random.monad]
            [left (random.unicode 1)
             right (random.unicode 1)
             expected (random.only (|>> (text\= right) not)
                                   (random.unicode 1))]
            (_.cover [/.enclosed]
                     (|> (format left expected right)
                         (/.result (/.enclosed [left right] (/.this expected)))
                         (!expect (#try.Success _)))))
          (do [! random.monad]
            [input (random.unicode 1)
             output (random.unicode 1)]
            (_.cover [/.local]
                     (|> output
                         (/.result (do <>.monad
                                     [_ (/.local input (/.this input))]
                                     (/.this output)))
                         (!expect (#try.Success _)))))
          (do [! random.monad]
            [expected (\ ! each (|>> (n.% 8) (\ n.octal encoded)) random.nat)]
            (_.cover [/.then]
                     (|> (list (code.text expected))
                         (<c>.result (/.then /.octal <c>.text))
                         (!expect (^multi (#try.Success actual)
                                          (text\= expected actual))))))
          (do [! random.monad]
            [invalid (random.ascii/upper 1)
             expected (random.only (|>> (unicode/block.within? unicode/block.basic_latin/upper)
                                        not)
                                   (random.char unicode.character))
             .let [upper! (/.one_of! "ABCDEFGHIJKLMNOPQRSTUVWXYZ")]]
            (_.cover [/.not /.not! /.expected_to_fail]
                     (and (..should_pass (text.of_char expected) (/.not /.upper))
                          (|> invalid
                              (/.result (/.not /.upper))
                              (!expect (^multi (#try.Failure error)
                                               (exception.match? /.expected_to_fail error))))

                          (..should_pass! (text.of_char expected) (/.not! upper!))
                          (|> invalid
                              (/.result (/.not! upper!))
                              (!expect (^multi (#try.Failure error)
                                               (exception.match? /.expected_to_fail error)))))))
          (do [! random.monad]
            [upper (random.ascii/upper 1)
             lower (random.ascii/lower 1)
             invalid (random.only (function (_ char)
                                    (not (or (unicode/block.within? unicode/block.basic_latin/upper char)
                                             (unicode/block.within? unicode/block.basic_latin/lower char))))
                                  (random.char unicode.character))
             .let [upper! (/.one_of! "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
                   lower! (/.one_of! "abcdefghijklmnopqrstuvwxyz")]]
            (_.cover [/.and /.and!]
                     (and (..should_pass (format upper lower) (/.and /.upper /.lower))
                          (..should_fail (format (text.of_char invalid) lower) (/.and /.upper /.lower))
                          (..should_fail (format upper (text.of_char invalid)) (/.and /.upper /.lower))

                          (..should_pass! (format upper lower) (/.and! upper! lower!))
                          (..should_fail (format (text.of_char invalid) lower) (/.and! upper! lower!))
                          (..should_fail (format upper (text.of_char invalid)) (/.and! upper! lower!)))))
          (do [! random.monad]
            [expected (random.unicode 1)
             invalid (random.unicode 1)]
            (_.cover [/.satisfies /.character_does_not_satisfy_predicate]
                     (and (..should_pass expected (/.satisfies (function.constant true)))
                          (..should_fail' invalid (/.satisfies (function.constant false))
                                          /.character_does_not_satisfy_predicate))))
          ..character_classes
          ..runs
          )))