aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/parser/text.lux
blob: 30ebe0cad01a1267a5cd49e0d4d510d3e15c8c28 (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:
  [lux #*
   ["_" test (#+ Test)]
   [abstract
    [monad (#+ do)]]
   [control
    ["." try (#+ Try)]
    ["." exception (#+ Exception)]
    ["." function]]
   [data
    ["." maybe]
    ["." text ("#@." equivalence)
     ["." unicode]
     ["%" format (#+ format)]]
    [number
     ["n" nat]]
    [collection
     ["." set]
     ["." list ("#@." functor)]]]
   [math
    ["." random]]
   [macro
    ["." code]]]
  {1
   ["." /
    ["<>" //
     ["<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 (/.run parser sample)
    (#try.Failure error)
    (exception.match? exception error)
    
    _
    false))

(def: (should-fail sample parser)
  (All [a] (-> Text (/.Parser a) Bit))
  (case (/.run parser sample)
    (#try.Failure _)
    true
    
    _
    false))

(def: (should-pass expected parser)
  (-> Text (/.Parser Text) Bit)
  (|> expected
      (/.run parser)
      (:: try.functor map (text@= expected))
      (try.default false)))

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

(def: character-classes
  Test
  ($_ _.and
      (do {@ random.monad}
        [offset (:: @ map (n.% 50) random.nat)
         range (:: @ map (|>> (n.% 50) (n.+ 10)) random.nat)
         #let [limit (n.+ offset range)]
         expected (:: @ map (|>> (n.% range) (n.+ offset) text.from-code) random.nat)
         out-of-range (case offset
                        0 (:: @ map (|>> (n.% 10) inc (n.+ limit) text.from-code) random.nat)
                        _ (:: @ map (|>> (n.% offset) text.from-code) 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-alpha)
         invalid (random.filter (|>> (unicode.within? unicode.basic-latin/upper-alpha) not)
                                (random.char unicode.full))]
        (_.cover [/.upper]
                 (and (..should-pass (text.from-code expected) /.upper)
                      (..should-fail (text.from-code invalid) /.upper))))
      (do {@ random.monad}
        [expected (random.char unicode.ascii/lower-alpha)
         invalid (random.filter (|>> (unicode.within? unicode.basic-latin/lower-alpha) not)
                                (random.char unicode.full))]
        (_.cover [/.lower]
                 (and (..should-pass (text.from-code expected) /.lower)
                      (..should-fail (text.from-code invalid) /.lower))))
      (do {@ random.monad}
        [expected (:: @ map (n.% 10) random.nat)
         invalid (random.char (unicode.set (list unicode.aegean-numbers)))]
        (_.cover [/.decimal]
                 (and (..should-pass (:: n.decimal encode expected) /.decimal)
                      (..should-fail (text.from-code invalid) /.decimal))))
      (do {@ random.monad}
        [expected (:: @ map (n.% 8) random.nat)
         invalid (random.char (unicode.set (list unicode.aegean-numbers)))]
        (_.cover [/.octal]
                 (and (..should-pass (:: n.octal encode expected) /.octal)
                      (..should-fail (text.from-code invalid) /.octal))))
      (do {@ random.monad}
        [expected (:: @ map (n.% 16) random.nat)
         invalid (random.char (unicode.set (list unicode.aegean-numbers)))]
        (_.cover [/.hexadecimal]
                 (and (..should-pass (:: n.hex encode expected) /.hexadecimal)
                      (..should-fail (text.from-code invalid) /.hexadecimal))))
      (do {@ random.monad}
        [expected (random.char unicode.ascii/alpha)
         invalid (random.filter (function (_ char)
                                  (not (or (unicode.within? unicode.basic-latin/upper-alpha char)
                                           (unicode.within? unicode.basic-latin/lower-alpha char))))
                                (random.char unicode.full))]
        (_.cover [/.alpha]
                 (and (..should-pass (text.from-code expected) /.alpha)
                      (..should-fail (text.from-code invalid) /.alpha))))
      (do {@ random.monad}
        [expected (random.char unicode.ascii/alpha-num)
         invalid (random.filter (function (_ char)
                                  (not (or (unicode.within? unicode.basic-latin/upper-alpha char)
                                           (unicode.within? unicode.basic-latin/lower-alpha char)
                                           (unicode.within? unicode.basic-latin/decimal char))))
                                (random.char unicode.full))]
        (_.cover [/.alpha-num]
                 (and (..should-pass (text.from-code expected) /.alpha-num)
                      (..should-fail (text.from-code invalid) /.alpha-num))))
      (do {@ random.monad}
        [expected ($_ random.either
                      (wrap text.tab)
                      (wrap text.vertical-tab)
                      (wrap text.space)
                      (wrap text.new-line)
                      (wrap text.carriage-return)
                      (wrap text.form-feed))
         invalid (|> (random.unicode 1) (random.filter (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.full)
                     (random.set n.hash num-options)
                     (:: @ map (|>> set.to-list
                                    (list@map text.from-code)
                                    (text.join-with ""))))
         expected (:: @ map (function (_ value)
                              (|> options
                                  (text.nth (n.% num-options value))
                                  maybe.assume))
                      random.nat)
         invalid (random.filter (|>> text.from-code
                                     (text.contains? options)
                                     not)
                                (random.char unicode.full))]
        (_.cover [/.one-of /.one-of! /.character-should-be]
                 (and (..should-pass (text.from-code expected) (/.one-of options))
                      (..should-fail (text.from-code invalid) (/.one-of options))
                      (..should-fail' (text.from-code invalid) (/.one-of options)
                                      /.character-should-be)

                      (..should-pass! (text.from-code expected) (/.one-of! options))
                      (..should-fail (text.from-code invalid) (/.one-of! options))
                      (..should-fail' (text.from-code invalid) (/.one-of! options)
                                      /.character-should-be))))
      (do {@ random.monad}
        [#let [num-options 3]
         options (|> (random.char unicode.full)
                     (random.set n.hash num-options)
                     (:: @ map (|>> set.to-list
                                    (list@map text.from-code)
                                    (text.join-with ""))))
         invalid (:: @ map (function (_ value)
                             (|> options
                                 (text.nth (n.% num-options value))
                                 maybe.assume))
                     random.nat)
         expected (random.filter (|>> text.from-code
                                      (text.contains? options)
                                      not)
                                 (random.char unicode.full))]
        (_.cover [/.none-of /.none-of! /.character-should-not-be]
                 (and (..should-pass (text.from-code expected) (/.none-of options))
                      (..should-fail (text.from-code invalid) (/.none-of options))
                      (..should-fail' (text.from-code invalid) (/.none-of options)
                                      /.character-should-not-be)

                      (..should-pass! (text.from-code expected) (/.none-of! options))
                      (..should-fail (text.from-code invalid) (/.none-of! options))
                      (..should-fail' (text.from-code invalid) (/.none-of! options)
                                      /.character-should-not-be))))
      ))

(def: runs
  Test
  (let [octal! (/.one-of! "01234567")]
    ($_ _.and
        (do {@ random.monad}
          [left (:: @ map (|>> (n.% 8) (:: n.octal encode)) random.nat)
           right (:: @ map (|>> (n.% 8) (:: n.octal encode)) random.nat)
           #let [expected (format left right)]
           invalid (|> random.nat
                       (:: @ map (n.% 16))
                       (random.filter (n.>= 8))
                       (:: @ map (:: n.hex encode)))]
          (_.cover [/.many /.many!]
                   (and (..should-pass expected (/.many /.octal))
                        (..should-fail invalid (/.many /.octal))

                        (..should-pass! expected (/.many! octal!)))))
        (do {@ random.monad}
          [left (:: @ map (|>> (n.% 8) (:: n.octal encode)) random.nat)
           right (:: @ map (|>> (n.% 8) (:: n.octal encode)) random.nat)
           #let [expected (format left right)]
           invalid (|> random.nat
                       (:: @ map (n.% 16))
                       (random.filter (n.>= 8))
                       (:: @ map (:: n.hex encode)))]
          (_.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 (:: @ map (|>> (n.% 8) (:: n.octal encode)) 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 (:: @ map (|>> (n.% 8) (:: n.octal encode)) 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 (:: @ map (|>> (n.% 8) (:: n.octal encode)) 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 (:: @ map (|>> (n.% 8) (:: n.octal encode)) random.nat)]
           first octal
           second octal
           third octal]
          (_.cover [/.between /.between!]
                   (and (..should-pass (format first second) (/.between 2 3 /.octal))
                        (..should-pass (format first second third) (/.between 2 3 /.octal))
                        (..should-fail (format first) (/.between 2 3 /.octal))

                        (..should-pass! (format first second) (/.between! 2 3 octal!))
                        (..should-pass! (format first second third) (/.between! 2 3 octal!))
                        (..should-fail (format first) (/.between! 2 3 octal!)))))
        )))

(def: #export test
  Test
  (<| (_.covering /._)
      (_.with-cover [/.Parser])
      ($_ _.and
          (do {@ random.monad}
            [sample (random.unicode 1)]
            (_.cover [/.run /.end!]
                     (and (|> (/.run /.end!
                                     "")
                              (!expect (#try.Success _)))
                          (|> (/.run /.end!
                                     sample)
                              (!expect (#try.Failure _))))))
          (do {@ random.monad}
            [#let [size 10]
             expected (random.unicode size)
             dummy (|> (random.unicode size)
                       (random.filter (|>> (text@= expected) not)))]
            (_.cover [/.this /.cannot-match]
                     (and (|> (/.run (/.this expected)
                                     expected)
                              (!expect (#try.Success [])))
                          (|> (/.run (/.this expected)
                                     dummy)
                              (!expect (^multi (#try.Failure error)
                                               (exception.match? /.cannot-match error)))))))
          (_.cover [/.Slice /.slice /.cannot-slice]
                   (|> ""
                       (/.run (do <>.monad
                                [#let [_ (log! " PRE SLICE")]
                                 slice (/.slice /.any!)
                                 #let [_ (log! "POST SLICE")]]
                                (wrap slice)))
                       (!expect (^multi (#try.Failure error)
                                        (exec (log! (format "error = " 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 [/.peek /.cannot-parse]
                     (and (..should-pass expected (<>.before /.any /.peek))
                          (|> ""
                              (/.run (<>.before /.any /.peek))
                              (!expect (^multi (#try.Failure error)
                                               (exception.match? /.cannot-parse error)))))))
          (do {@ random.monad}
            [dummy (random.unicode 1)]
            (_.cover [/.unconsumed-input]
                     (|> (format dummy dummy)
                         (/.run /.any)
                         (!expect (^multi (#try.Failure error)
                                          (exception.match? /.unconsumed-input error))))))
          (do {@ random.monad}
            [sample (random.unicode 1)]
            (_.cover [/.Offset /.offset]
                     (|> sample
                         (/.run (do <>.monad
                                  [pre /.offset
                                   _ /.any
                                   post /.offset]
                                  (wrap [pre post])))
                         (!expect (#try.Success [0 1])))))
          (do {@ random.monad}
            [left (random.unicode 1)
             right (random.unicode 1)
             #let [input (format left right)]]
            (_.cover [/.get-input]
                     (|> input
                         (/.run (do <>.monad
                                  [pre /.get-input
                                   _ /.any
                                   post /.get-input
                                   _ /.any]
                                  (wrap (and (text@= input pre)
                                             (text@= right post)))))
                         (!expect (#try.Success #1)))))
          (do {@ random.monad}
            [left (random.unicode 1)
             right (random.unicode 1)
             expected (random.filter (|>> (text@= right) not)
                                     (random.unicode 1))]
            (_.cover [/.enclosed]
                     (|> (format left expected right)
                         (/.run (/.enclosed [left right] (/.this expected)))
                         (!expect (#try.Success _)))))
          (do {@ random.monad}
            [in (random.unicode 1)
             out (random.unicode 1)]
            (_.cover [/.local]
                     (|> out
                         (/.run (do <>.monad
                                  [_ (/.local in (/.this in))]
                                  (/.this out)))
                         (!expect (#try.Success _)))))
          (do {@ random.monad}
            [expected (:: @ map (|>> (n.% 8) (:: n.octal encode)) random.nat)]
            (_.cover [/.embed]
                     (|> (list (code.text expected))
                         (<c>.run (/.embed /.octal <c>.text))
                         (!expect (^multi (#try.Success actual)
                                          (text@= expected actual))))))
          (do {@ random.monad}
            [invalid (random.ascii/upper-alpha 1)
             expected (random.filter (|>> (unicode.within? unicode.basic-latin/upper-alpha)
                                          not)
                                     (random.char unicode.full))
             #let [upper! (/.one-of! "ABCDEFGHIJKLMNOPQRSTUVWXYZ")]]
            (_.cover [/.not /.not! /.expected-to-fail]
                     (and (..should-pass (text.from-code expected) (/.not /.upper))
                          (|> invalid
                              (/.run (/.not /.upper))
                              (!expect (^multi (#try.Failure error)
                                               (exception.match? /.expected-to-fail error))))

                          (..should-pass! (text.from-code expected) (/.not! upper!))
                          (|> invalid
                              (/.run (/.not! upper!))
                              (!expect (^multi (#try.Failure error)
                                               (exception.match? /.expected-to-fail error)))))))
          (do {@ random.monad}
            [upper (random.ascii/upper-alpha 1)
             lower (random.ascii/lower-alpha 1)
             invalid (random.filter (function (_ char)
                                      (not (or (unicode.within? unicode.basic-latin/upper-alpha char)
                                               (unicode.within? unicode.basic-latin/lower-alpha char))))
                                    (random.char unicode.full))
             #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.from-code invalid) lower) (/.and /.upper /.lower))
                          (..should-fail (format upper (text.from-code invalid)) (/.and /.upper /.lower))

                          (..should-pass! (format upper lower) (/.and! upper! lower!))
                          (..should-fail (format (text.from-code invalid) lower) (/.and! upper! lower!))
                          (..should-fail (format upper (text.from-code 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
          )))