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

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

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