blob: d00a21d907ea6b8f8ea94bdb7c614dca5708311a (
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
["." try (#+ Try)]
["." exception (#+ Exception)]
["." function]]
[data
["." maybe]
["." 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 (/.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)
invalid (random.filter (|>> (unicode/block.within? unicode/block.basic_latin/upper) not)
(random.char unicode.character))]
(_.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)
invalid (random.filter (|>> (unicode/block.within? unicode/block.basic_latin/lower) not)
(random.char unicode.character))]
(_.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 [unicode/block.number_forms (list)]))]
(_.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 [unicode/block.number_forms (list)]))]
(_.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 [unicode/block.number_forms (list)]))]
(_.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/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.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/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.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.character)
(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 (function (_ char)
(not (text.contains? (text.from_code char) options)))
(random.char unicode.character))]
(_.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.character)
(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 (function (_ char)
(not (text.contains? (text.from_code char) options)))
(random.char unicode.character))]
(_.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 /._)
(_.for [/.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 (/.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 [/.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 1)
expected (random.filter (|>> (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.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 1)
lower (random.ascii/lower 1)
invalid (random.filter (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.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
)))
|