aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/format/json.lux
blob: 0442d17bc04767177a985e968559202da7dfaeac (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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
(.require
 [library
  [lux (.except Variant Record #Bit #Text #Frac)
   [abstract
    [codec (.except)]
    [monad (.only do)]
    ["[0]" equivalence (.only Equivalence)
     ["[0]/[1]" \\polytypic]]
    [\\specification
     ["$[0]" equivalence]]
    ["[0]" codec
     ["[1]T" \\test]]]
   [control
    ["<>" parser]
    ["[0]" pipe]
    ["[0]" maybe]
    ["[0]" exception]
    ["[0]" try (.use "[1]#[0]" functor)]]
   [data
    ["[0]" product]
    ["[0]" bit]
    ["[0]" text (.only)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" sequence (.only sequence) (.use "[1]#[0]" functor)]
     ["[0]" dictionary (.only Dictionary)]
     ["[0]" set]
     ["[0]" list (.use "[1]#[0]" functor)]]]
   [math
    ["[0]" random (.only Random) (.use "[1]#[0]" monad)]
    [number (.only hex)
     ["n" nat]
     ["[0]" i64]
     ["[0]" frac]]]
   ["[0]" meta (.only)
    ["@" target]
    ["[0]" code]
    ["[0]" macro (.only)
     ["^" pattern]
     ["[0]" syntax (.only syntax)]]
    [type
     ["[0]" unit]]]
   [world
    [time
     ["[0]" date]
     ["[0]" instant
      ["[0]/[1]" \\test]]
     ["[0]" duration
      ["[0]/[1]" \\test]]]]
   [test
    ["_" property (.only Test)]]]]
 ["[0]" \\polytypic]
 ["[0]" \\parser]
 [\\library
  ["[0]" / (.only JSON) (.use "[1]#[0]" equivalence)]])

(def !expect
  (template (_ <pattern> <value>)
    [(when <value>
       <pattern>
       true
       
       _
       false)]))

(def safe_frac
  (Random Frac)
  (random.only (|>> frac.not_a_number? not) random.frac))

(def \\parser
  Test
  (<| (_.covering \\parser._)
      (_.for [\\parser.Parser])
      (`` (all _.and
               (do [! random.monad]
                 [expected (of ! each (|>> {/.#String}) (random.unicode 1))]
                 (_.coverage [\\parser.result \\parser.any]
                   (|> (\\parser.result \\parser.any expected)
                       (!expect (^.multi {try.#Success actual}
                                         (of /.equivalence = expected actual))))))
               (_.coverage [\\parser.null]
                 (|> (\\parser.result \\parser.null {/.#Null})
                     (!expect {try.#Success _})))
               (,, (with_template [<query> <test> <check> <random> <json> <equivalence>]
                     [(do [! random.monad]
                        [expected <random>
                         dummy (|> <random> (random.only (|>> (of <equivalence> = expected) not)))]
                        (all _.and
                             (_.coverage [<query>]
                               (|> (\\parser.result <query> {<json> expected})
                                   (!expect (^.multi {try.#Success actual}
                                                     (of <equivalence> = expected actual)))))
                             (_.coverage [<test>]
                               (and (|> (\\parser.result (<test> expected) {<json> expected})
                                        (!expect {try.#Success .true}))
                                    (|> (\\parser.result (<test> expected) {<json> dummy})
                                        (!expect {try.#Success .false}))))
                             (_.coverage [<check>]
                               (and (|> (\\parser.result (<check> expected) {<json> expected})
                                        (!expect {try.#Success _}))
                                    (|> (\\parser.result (<check> expected) {<json> dummy})
                                        (!expect {try.#Failure _}))))))]

                     [\\parser.boolean \\parser.boolean? \\parser.this_boolean random.bit /.#Boolean bit.equivalence]
                     [\\parser.number \\parser.number? \\parser.this_number ..safe_frac /.#Number frac.equivalence]
                     [\\parser.string \\parser.string? \\parser.this_string (random.unicode 1) /.#String text.equivalence]
                     ))
               (do [! random.monad]
                 [expected (random.unicode 1)
                  dummy random.bit]
                 (_.coverage [\\parser.unexpected_value]
                   (|> (\\parser.result \\parser.string {/.#Boolean dummy})
                       (!expect (^.multi {try.#Failure error}
                                         (exception.match? \\parser.unexpected_value error))))))
               (do [! random.monad]
                 [expected (random.unicode 1)
                  dummy (|> (random.unicode 1) (random.only (|>> (of text.equivalence = expected) not)))]
                 (_.coverage [\\parser.value_mismatch]
                   (|> (\\parser.result (\\parser.this_string expected) {/.#String dummy})
                       (!expect (^.multi {try.#Failure error}
                                         (exception.match? \\parser.value_mismatch error))))))
               (do [! random.monad]
                 [expected (random.unicode 1)]
                 (_.coverage [\\parser.nullable]
                   (and (|> (\\parser.result (\\parser.nullable \\parser.string) {/.#Null})
                            (!expect (^.multi {try.#Success actual}
                                              (of (maybe.equivalence text.equivalence) = {.#None} actual))))
                        (|> (\\parser.result (\\parser.nullable \\parser.string) {/.#String expected})
                            (!expect (^.multi {try.#Success actual}
                                              (of (maybe.equivalence text.equivalence) = {.#Some expected} actual)))))))
               (do [! random.monad]
                 [size (of ! each (n.% 10) random.nat)
                  expected (|> (random.unicode 1)
                               (random.list size)
                               (of ! each sequence.of_list))]
                 (_.coverage [\\parser.array]
                   (|> (\\parser.result (\\parser.array (<>.some \\parser.string))
                                        {/.#Array (sequence#each (|>> {/.#String}) expected)})
                       (!expect (^.multi {try.#Success actual}
                                         (of (sequence.equivalence text.equivalence) = expected (sequence.of_list actual)))))))
               (do [! random.monad]
                 [expected (of ! each (|>> {/.#String}) (random.unicode 1))]
                 (_.coverage [\\parser.unconsumed_input]
                   (|> (\\parser.result (\\parser.array \\parser.any) {/.#Array (sequence expected expected)})
                       (!expect (^.multi {try.#Failure error}
                                         (exception.match? \\parser.unconsumed_input error))))))
               (_.coverage [\\parser.empty_input]
                 (|> (\\parser.result (\\parser.array \\parser.any) {/.#Array (sequence)})
                     (!expect (^.multi {try.#Failure error}
                                       (exception.match? \\parser.empty_input error)))))
               (do [! random.monad]
                 [expected_boolean random.bit
                  expected_number ..safe_frac
                  expected_string (random.unicode 1)
                  [boolean_field number_field string_field] (|> (random.set text.hash 3 (random.unicode 3))
                                                                (of ! each (|>> set.list
                                                                                (pipe.when
                                                                                  (list boolean_field number_field string_field)
                                                                                  [boolean_field number_field string_field]

                                                                                  _
                                                                                  (undefined)))))]
                 (_.coverage [\\parser.object \\parser.field]
                   (|> (\\parser.result (\\parser.object (all <>.and
                                                              (\\parser.field boolean_field \\parser.boolean)
                                                              (\\parser.field number_field \\parser.number)
                                                              (\\parser.field string_field \\parser.string)))
                                        {/.#Object
                                         (dictionary.of_list text.hash
                                                             (list [boolean_field {/.#Boolean expected_boolean}]
                                                                   [number_field {/.#Number expected_number}]
                                                                   [string_field {/.#String expected_string}]))})
                       (!expect (^.multi {try.#Success [actual_boolean actual_number actual_string]}
                                         (and (of bit.equivalence = expected_boolean actual_boolean)
                                              (of frac.equivalence = expected_number actual_number)
                                              (of text.equivalence = expected_string actual_string)))))))
               (do [! random.monad]
                 [size (of ! each (n.% 10) random.nat)
                  keys (random.list size (random.unicode 1))
                  values (random.list size (random.unicode 1))
                  .let [expected (dictionary.of_list text.hash (list.zipped_2 keys values))]]
                 (_.coverage [\\parser.dictionary]
                   (|> (\\parser.result (\\parser.dictionary \\parser.string)
                                        {/.#Object
                                         (|> values
                                             (list#each (|>> {/.#String}))
                                             (list.zipped_2 keys)
                                             (dictionary.of_list text.hash))})
                       (!expect (^.multi {try.#Success actual}
                                         (of (dictionary.equivalence text.equivalence) = expected actual))))))
               ))))

(type Variant
  (.Variant
   {#Bit Bit}
   {#Text Text}
   {#Frac Frac}))

(type Recursive
  (Rec Recursive
    (.Variant
     {#Number Frac}
     {#Addition Frac Recursive})))

(type Record
  (.Record
   [#bit Bit
    #frac Frac
    #text Text
    #maybe (Maybe Frac)
    #list (List Frac)
    #dictionary (Dictionary Text Frac)
    #variant Variant
    #tuple [Bit Text Frac]
    #recursive Recursive
    ... #instant instant.Instant
    ... #duration duration.Duration
    #date date.Date
    #grams (unit.Measure unit.Gram)]))

(def gen_recursive
  (Random Recursive)
  (random.rec
   (function (_ gen_recursive)
     (random.or random.safe_frac
                (random.and random.safe_frac
                            gen_recursive)))))

(def measure
  (All (_ unit) (Random (unit.Measure unit)))
  (of random.monad each unit.measure random.int))

(def gen_record
  (Random Record)
  (do [! random.monad]
    [size (of ! each (n.% 2) random.nat)]
    (all random.and
         random.bit
         random.safe_frac
         (random.unicode size)
         (random.maybe random.safe_frac)
         (random.list size random.safe_frac)
         (random.dictionary text.hash size (random.unicode size) random.safe_frac)
         (all random.or random.bit (random.unicode size) random.safe_frac)
         (all random.and random.bit (random.unicode size) random.safe_frac)
         ..gen_recursive
         ... \\test/instant.instant
         ... \\test/duration.duration
         random.date
         ..measure
         )))

(for @.old (these)
     (these (def equivalence
              (Equivalence Record)
              (\\polytypic/equivalence.equivalence Record))

            (def codec
              (Codec JSON Record)
              (\\polytypic.codec Record))))

(def \\polytypic
  Test
  (<| (_.covering \\polytypic._)
      (_.for [\\polytypic.codec]
             (for @.old (_.test "PLACEHOLDER" true)
                  (codecT.spec ..equivalence ..codec ..gen_record)))))

(def .public random
  (Random /.JSON)
  (random.rec
   (function (_ again)
     (do [! random.monad]
       [size (of ! each (n.% 2) random.nat)]
       (all random.or
            (of ! in [])
            random.bit
            random.safe_frac
            (random.unicode size)
            (random.sequence size again)
            (random.dictionary text.hash size (random.unicode size) again)
            )))))

(def boolean
  (syntax (_ [])
    (do meta.monad
      [value meta.seed]
      (in (list (code.bit (n.even? value)))))))

(def number
  (syntax (_ [])
    (do meta.monad
      [value meta.seed]
      (in (list (code.frac (n.frac value)))))))

(def string
  (syntax (_ [])
    (do meta.monad
      [value (macro.symbol "string")]
      (in (list (code.text (%.code value)))))))

(def (digits/4 it)
  (-> Nat Text)
  (<| (if (n.< (hex "10") it)
        (format "000" (%.nat_16 it)))
      (if (n.< (hex "100") it)
        (format "00" (%.nat_16 it)))
      (if (n.< (hex "1000") it)
        (format "0" (%.nat_16 it)))
      (%.nat_16 it)))

(def escaped_string
  (Random [Text Text])
  (all random.either
       (random#in [text.tab "\t"])
       (random#in [text.back_space "\b"])
       (random#in [text.new_line "\n"])
       (random#in [text.carriage_return "\r"])
       (random#in [text.form_feed "\f"])
       (random#in [text.double_quote text.double_quote])
       (random#in ["\" "\\"])
       (do [! random.monad]
         [char (of ! each (i64.and (hex "FF"))
                   random.nat)]
         (in [(text.of_char char)
              (format "\u" (digits/4 char))]))
       ))

(def any_string
  (Random [Text Text])
  (all random.either
       escaped_string
       (do random.monad
         [it (random.alphabetic 1)]
         (in [it it]))
       ))

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [/.JSON])
      (`` (all _.and
               (_.for [/.equivalence]
                      ($equivalence.spec /.equivalence ..random))
               (_.for [/.codec]
                      (all _.and
                           (codecT.spec /.equivalence /.codec ..random)
                           (do random.monad
                             [key (random.alphabetic 1)
                              [expected escaped] any_string]
                             (_.coverage [/.#String]
                               (|> {/.#String escaped}
                                   (of /.codec encoded)
                                   (of /.codec decoded)
                                   (try#each (of /.equivalence = {/.#String expected}))
                                   (try.else false))))
                           ))

               (do random.monad
                 [sample ..random]
                 (_.coverage [/.Null /.#Null /.null?]
                   (of bit.equivalence =
                       (/.null? sample)
                       (when sample
                         {/.#Null} true
                         _ false))))
               (do random.monad
                 [expected ..random]
                 (_.coverage [/.format]
                   (|> expected
                       /.format
                       (of /.codec decoded)
                       (try#each (/#= expected))
                       (try.else false))))
               (do random.monad
                 [keys (random.set text.hash 3 (random.alphabetic 1))
                  values (random.set frac.hash 3 random.safe_frac)
                  .let [expected (list.zipped_2 (set.list keys)
                                                (list#each (|>> {/.#Number}) (set.list values)))
                        object (/.object expected)]]
                 (all _.and
                      (_.coverage [/.object /.fields]
                        (when (/.fields object)
                          {try.#Success actual}
                          (of (list.equivalence text.equivalence) =
                              (list#each product.left expected)
                              actual)
                          
                          {try.#Failure error}
                          false))
                      (_.coverage [/.field]
                        (list.every? (function (_ [key expected])
                                       (|> (/.field key object)
                                           (try#each (/#= expected))
                                           (try.else false)))
                                     expected))
                      ))
               (do random.monad
                 [key (random.alphabetic 1)
                  unknown (random.only (|>> (of text.equivalence = key) not)
                                       (random.alphabetic 1))
                  expected random.safe_frac]
                 (_.coverage [/.has]
                   (<| (try.else false)
                       (do try.monad
                         [object (/.has key {/.#Number expected} (/.object (list)))
                          .let [can_find_known_key!
                                (|> object
                                    (/.field key)
                                    (try#each (/#= {/.#Number expected}))
                                    (try.else false))

                                cannot_find_unknown_key!
                                (when (/.field unknown object)
                                  {try.#Success _}
                                  false

                                  {try.#Failure error}
                                  true)]]
                         (in (and can_find_known_key!
                                  cannot_find_unknown_key!))))))
               (,, (with_template [<type> <field> <tag> <random> <equivalence>]
                     [(do random.monad
                        [key (random.alphabetic 1)
                         value <random>]
                        (_.coverage [<type> <tag> <field>]
                          (|> (/.object (list [key {<tag> value}]))
                              (<field> key)
                              (try#each (of <equivalence> = value))
                              (try.else false))))]

                     [/.Boolean /.boolean_field /.#Boolean random.bit bit.equivalence]
                     [/.Number /.number_field /.#Number random.safe_frac frac.equivalence]
                     [/.String /.string_field /.#String (random.alphabetic 1) text.equivalence]
                     [/.Array /.array_field /.#Array (random.sequence 3 ..random) (sequence.equivalence /.equivalence)]
                     [/.Object /.object_field /.#Object (random.dictionary text.hash 3 (random.alphabetic 1) ..random) (dictionary.equivalence /.equivalence)]
                     ))
               (with_expansions [<boolean> (boolean)
                                 <number> (number)
                                 <string> (string)
                                 <array_sequence> (sequence.sequence {/.#Null}
                                                                     {/.#Boolean <boolean>}
                                                                     {/.#Number <number>}
                                                                     {/.#String <string>})
                                 <key0> (string)
                                 <key1> (string)
                                 <key2> (string)
                                 <key3> (string)
                                 <key4> (string)
                                 <key5> (string)
                                 <key6> (string)]
                 (_.coverage [/.json]
                   (and (/#= {/.#Null} (/.json ()))
                        (,, (with_template [<tag> <value>]
                              [(/#= {<tag> <value>} (/.json <value>))]
                              
                              [/.#Boolean <boolean>]
                              [/.#Number <number>]
                              [/.#String <string>]
                              ))
                        (/#= {/.#Array <array_sequence>} (/.json [() <boolean> <number> <string>]))
                        (let [object (/.json {<key0> ()
                                                     <key1> <boolean>
                                                     <key2> <number>
                                                     <key3> <string>
                                                     <key4> [() <boolean> <number> <string>]
                                                     <key5> {<key6> <number>}})]
                          (<| (try.else false)
                              (do try.monad
                                [value0 (/.field <key0> object)
                                 value1 (/.field <key1> object)
                                 value2 (/.field <key2> object)
                                 value3 (/.field <key3> object)
                                 value4 (/.field <key4> object)
                                 value5 (/.field <key5> object)
                                 value6 (/.field <key6> value5)]
                                (in (and (/#= {/.#Null} value0)
                                         (/#= {/.#Boolean <boolean>} value1)
                                         (/#= {/.#Number <number>} value2)
                                         (/#= {/.#String <string>} value3)
                                         (/#= {/.#Array <array_sequence>} value4)
                                         (/#= {/.#Number <number>} value6))))))
                        )))

               ..\\polytypic
               ..\\parser
               ))))