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

(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])
      (`` ($_ _.and
              (do [! random.monad]
                [expected (\ ! each (|>> #json.String) (random.unicode 1))]
                (_.cover [/.result /.any]
                         (|> (/.result /.any expected)
                             (!expect (^multi (#try.Success actual)
                                              (\ json.equivalence = expected actual))))))
              (_.cover [/.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)))]
                       ($_ _.and
                           (_.cover [<query>]
                                    (|> (/.result <query> (<json> expected))
                                        (!expect (^multi (#try.Success actual)
                                                         (\ <equivalence> = expected actual)))))
                           (_.cover [<test>]
                                    (and (|> (/.result (<test> expected) (<json> expected))
                                             (!expect (#try.Success #1)))
                                         (|> (/.result (<test> expected) (<json> dummy))
                                             (!expect (#try.Success #0)))))
                           (_.cover [<check>]
                                    (and (|> (/.result (<check> expected) (<json> expected))
                                             (!expect (#try.Success _)))
                                         (|> (/.result (<check> expected) (<json> dummy))
                                             (!expect (#try.Failure _)))))))]

                    [/.boolean /.boolean? /.boolean! random.bit #json.Boolean bit.equivalence]
                    [/.number /.number? /.number! ..safe_frac #json.Number frac.equivalence]
                    [/.string /.string? /.string! (random.unicode 1) #json.String text.equivalence]
                    ))
              (do [! random.monad]
                [expected (random.unicode 1)
                 dummy random.bit]
                (_.cover [/.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)))]
                (_.cover [/.value_mismatch]
                         (|> (/.result (/.string! expected) (#json.String dummy))
                             (!expect (^multi (#try.Failure error)
                                              (exception.match? /.value_mismatch error))))))
              (do [! random.monad]
                [expected (random.unicode 1)]
                (_.cover [/.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 row.of_list))]
                (_.cover [/.array]
                         (|> (/.result (/.array (<>.some /.string))
                                       (#json.Array (row\each (|>> #json.String) expected)))
                             (!expect (^multi (#try.Success actual)
                                              (\ (row.equivalence text.equivalence) = expected (row.of_list actual)))))))
              (do [! random.monad]
                [expected (\ ! each (|>> #json.String) (random.unicode 1))]
                (_.cover [/.unconsumed_input]
                         (|> (/.result (/.array /.any) (#json.Array (row expected expected)))
                             (!expect (^multi (#try.Failure error)
                                              (exception.match? /.unconsumed_input error))))))
              (_.cover [/.empty_input]
                       (|> (/.result (/.array /.any) (#json.Array (row)))
                           (!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
                                                                              (case> (^ (list boolean_field number_field string_field))
                                                                                     [boolean_field number_field string_field]

                                                                                     _
                                                                                     (undefined)))))]
                (_.cover [/.object /.field]
                         (|> (/.result (/.object ($_ <>.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))]]
                (_.cover [/.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))))))
              ))))