aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/parser/json.lux
blob: 40d48afab00c6392c28ee280d88d5e1782c417c1 (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
(.using
 [library
  [lux "*"
   ["_" test {"+" Test}]
   [abstract
    [monad {"+" do}]]
   [control
    ["[0]" pipe]
    ["[0]" maybe]
    ["[0]" try]
    ["[0]" exception]
    ["<>" parser]]
   [data
    ["[0]" bit]
    ["[0]" text]
    [collection
     ["[0]" list ("[1]#[0]" functor)]
     ["[0]" set]
     ["[0]" dictionary]
     ["[0]" sequence {"+" sequence} ("[1]#[0]" functor)]]
    [format
     ["[0]" json]]]
   [macro
    ["^" pattern]]
   [math
    ["[0]" random {"+" Random}]
    [number
     ["n" nat]
     ["[0]" frac]]]]]
 [\\library
  ["[0]" /]])

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

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

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

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

                                                                                 _
                                                                                 (undefined)))))]
                 (_.coverage [/.object /.field]
                   (|> (/.result (/.object (all <>.and
                                                (/.field boolean_field /.boolean)
                                                (/.field number_field /.number)
                                                (/.field string_field /.string)))
                                 {json.#Object
                                  (dictionary.of_list text.hash
                                                      (list [boolean_field {json.#Boolean expected_boolean}]
                                                            [number_field {json.#Number expected_number}]
                                                            [string_field {json.#String expected_string}]))})
                       (!expect (^.multi {try.#Success [actual_boolean actual_number actual_string]}
                                         (and (# bit.equivalence = expected_boolean actual_boolean)
                                              (# frac.equivalence = expected_number actual_number)
                                              (# text.equivalence = expected_string actual_string)))))))
               (do [! random.monad]
                 [size (# ! 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 [/.dictionary]
                   (|> (/.result (/.dictionary /.string)
                                 {json.#Object
                                  (|> values
                                      (list#each (|>> {json.#String}))
                                      (list.zipped_2 keys)
                                      (dictionary.of_list text.hash))})
                       (!expect (^.multi {try.#Success actual}
                                         (# (dictionary.equivalence text.equivalence) = expected actual))))))
               ))))