aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/format/xml.lux
blob: 73612963a631cfde252af68d40b957b4a24aaea9 (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
(.require
 [library
  [lux (.except char symbol)
   [abstract
    [monad (.only do)]
    ["[0]" equivalence
     ["[1]T" \\test]]
    ["[0]" codec
     ["[1]T" \\test]]]
   [control
    ["<>" parser (.use "[1]#[0]" monad)]
    ["[0]" maybe]
    ["[0]" try (.use "[1]#[0]" functor)]
    ["[0]" exception]]
   [data
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" dictionary]
     ["[0]" list (.use "[1]#[0]" functor)]]]
   [math
    ["[0]" random (.only Random) (.use "[1]#[0]" monad)]
    [number
     ["n" nat]]]
   [meta
    ["[0]" symbol (.use "[1]#[0]" equivalence)]
    ["[0]" type (.use "[1]#[0]" equivalence)]
    [macro
     ["^" pattern]
     ["[0]" template]]]
   [test
    ["_" property (.only Test)]]]]
 ["[0]" \\parser]
 [\\library
  ["[0]" / (.use "[1]#[0]" equivalence)]])

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

(def !failure
  (template (_ <exception> <cases>)
    [(with_expansions [<<cases>> (template.spliced <cases>)]
       (do [! random.monad]
         [expected (random.alphabetic 1)]
         (_.coverage [<exception>]
           (`` (and (,, (with_template [<parser> <input>]
                          [(|> (\\parser.result <parser> (list <input>))
                               (!expect (^.multi {try.#Failure error}
                                                 (exception.match? <exception> error))))]

                          <<cases>>)))))))]))

(def random_label
  (Random Symbol)
  (random.and (random.alphabetic 1)
              (random.alphabetic 1)))

(def random_tag ..random_label)
(def random_attribute ..random_label)

(def \\parser
  Test
  (<| (_.covering \\parser._)
      (_.for [\\parser.Parser])
      (all _.and
           (do [! random.monad]
             [expected (random.alphabetic 1)]
             (_.coverage [\\parser.result \\parser.text]
               (|> (\\parser.result \\parser.text (list {/.#Text expected}))
                   (!expect (^.multi {try.#Success actual}
                                     (text#= expected actual))))))
           (!failure \\parser.unconsumed_inputs
                     [[(<>#in expected)
                       {/.#Text expected}]])
           (do [! random.monad]
             [expected (of ! each (|>> {/.#Text}) (random.alphabetic 1))]
             (_.coverage [\\parser.any]
               (|> (\\parser.result \\parser.any (list expected))
                   (try#each (/#= expected))
                   (try.else false))))
           (do [! random.monad]
             [expected ..random_tag]
             (_.coverage [\\parser.tag]
               (|> (\\parser.result (do <>.monad
                                      [actual \\parser.tag
                                       _ \\parser.any]
                                      (in (symbol#= expected actual)))
                                    (list {/.#Node expected (dictionary.empty symbol.hash) (list)}))
                   (!expect {try.#Success .true}))))
           (do [! random.monad]
             [expected ..random_tag]
             (_.coverage [\\parser.node]
               (|> (\\parser.result (\\parser.node expected (<>#in []))
                                    (list {/.#Node expected (dictionary.empty symbol.hash) (list)}))
                   (!expect {try.#Success []}))))
           (!failure \\parser.wrong_tag
                     [[(\\parser.node ["" expected] (<>#in []))
                       {/.#Node [expected ""] (dictionary.empty symbol.hash) (list)}]])
           (do [! random.monad]
             [expected_tag ..random_tag
              expected_attribute ..random_attribute
              expected_value (random.alphabetic 1)]
             (_.coverage [\\parser.attribute]
               (|> (\\parser.result (<| (\\parser.node expected_tag)
                                        (<>.after (\\parser.attribute expected_attribute))
                                        (<>#in []))
                                    (list {/.#Node expected_tag
                                                   (|> (dictionary.empty symbol.hash)
                                                       (dictionary.has expected_attribute expected_value))
                                                   (list)}))
                   (!expect {try.#Success []}))))
           (!failure \\parser.unknown_attribute
                     [[(\\parser.attribute ["" expected])
                       {/.#Node [expected expected]
                                (|> (dictionary.empty symbol.hash)
                                    (dictionary.has [expected ""] expected))
                                (list)}]])
           (!failure \\parser.empty_input
                     [[(do <>.monad
                         [_ \\parser.any]
                         \\parser.any)
                       {/.#Text expected}]
                      [(do <>.monad
                         [_ \\parser.any]
                         \\parser.text)
                       {/.#Text expected}]
                      [(do <>.monad
                         [_ \\parser.any]
                         (\\parser.node [expected expected]
                                        (<>#in [])))
                       {/.#Node [expected expected]
                                (dictionary.empty symbol.hash)
                                (list)}]
                      [(do <>.monad
                         [_ \\parser.any]
                         (\\parser.node [expected expected]
                                        (\\parser.attribute [expected expected])))
                       {/.#Node [expected expected]
                                (|> (dictionary.empty symbol.hash)
                                    (dictionary.has [expected expected] expected))
                                (list)}]])
           (!failure \\parser.unexpected_input
                     [[\\parser.text
                       {/.#Node [expected expected] (dictionary.empty symbol.hash) (list)}]
                      [(\\parser.node [expected expected]
                                      (<>#in []))
                       {/.#Text expected}]
                      [(\\parser.node [expected expected]
                                      (\\parser.attribute [expected expected]))
                       {/.#Text expected}]])
           (do [! random.monad]
             [.let [node (is (-> /.Tag (List /.XML) /.XML)
                             (function (_ tag children)
                               {/.#Node tag (dictionary.empty symbol.hash) children}))]
              parent ..random_tag
              right ..random_tag
              wrong (random.only (|>> (symbol#= right) not)
                                 ..random_tag)
              .let [parser (<| (\\parser.node parent)
                               (do <>.monad
                                 [_ (<| \\parser.somewhere
                                        (\\parser.node right)
                                        (<>#in []))
                                  _ (<>.some \\parser.any)]
                                 (in [])))]
              repetitions (of ! each (n.% 10) random.nat)]
             (all _.and
                  (_.coverage [\\parser.somewhere]
                    (|> (\\parser.result parser
                                         (list (node parent
                                                     (list.together (list (list.repeated repetitions (node wrong (list)))
                                                                          (list (node right (list)))
                                                                          (list.repeated repetitions (node wrong (list))))))))
                        (!expect {try.#Success []})))
                  (_.coverage [\\parser.nowhere]
                    (|> (\\parser.result parser
                                         (list (node parent
                                                     (list.repeated repetitions (node wrong (list))))))
                        (!expect (^.multi {try.#Failure error}
                                          (exception.match? \\parser.nowhere error)))))
                  ))
           )))

(def char_range
  Text
  (format "_"
          "abcdefghijklmnopqrstuvwxyz"
          "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))

(def char
  (Random Nat)
  (do [! random.monad]
    [idx (|> random.nat (of ! each (n.% (text.size char_range))))]
    (in (maybe.trusted (text.char idx char_range)))))

(def (size bottom top)
  (-> Nat Nat (Random Nat))
  (let [constraint (|>> (n.% top) (n.max bottom))]
    (random#each constraint random.nat)))

(def (text bottom top)
  (-> Nat Nat (Random Text))
  (do random.monad
    [size (..size bottom top)]
    (random.text ..char size)))

(def symbol
  (Random Symbol)
  (random.and (..text 0 10)
              (..text 1 10)))

(def .public random
  (Random /.XML)
  (random.rec (function (_ random)
                (random.or (..text 1 10)
                           (do random.monad
                             [size (..size 0 2)]
                             (all random.and
                                  ..symbol
                                  (random.dictionary symbol.hash size ..symbol (..text 0 10))
                                  (random.list size random)))))))

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [/.XML /.#Text /.#Node])
      (all _.and
           (_.for [/.equivalence]
                  (equivalenceT.spec /.equivalence ..random))
           (_.for [/.codec]
                  (codecT.spec /.equivalence /.codec ..random))

           (do [! random.monad]
             [(^.let symbol [namespace name]) ..symbol]
             (`` (all _.and
                      (,, (with_template [<type> <format>]
                            [(_.coverage [<type> <format>]
                               (and (text#= name (<format> ["" name]))
                                    (let [symbol (<format> symbol)]
                                      (and (text.starts_with? namespace symbol)
                                           (text.ends_with? name symbol)))))]

                            [/.Tag /.tag]
                            [/.Attribute /.attribute]
                            ))
                      (_.coverage [/.Attrs /.attributes]
                        (dictionary.empty? /.attributes))
                      )))

           ..\\parser
           )))