aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/parser/xml.lux
blob: e92216696e69a25658369f8681e5554e711a3f7a (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
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    ["." type ("#\." equivalence)]
    [abstract
     [monad (#+ do)]]
    [control
     ["." try ("#\." functor)]
     ["." exception]]
    [data
     ["." text ("#\." equivalence)]
     ["." name ("#\." equivalence)]
     [format
      ["." xml ("#\." equivalence)]]
     [collection
      ["." dictionary]
      ["." list]]]
    [macro
     ["." template]]
    [math
     ["." random (#+ Random)]
     [number
      ["n" nat]]]]]
  [\\library
   ["." /
    ["/#" // ("#\." 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.ascii/alpha 1)]
       (_.cover [<exception>]
                (`` (and (~~ (template [<parser> <input>]
                               [(|> (/.result <parser> (list <input>))
                                    (!expect (^multi (#try.Failure error)
                                                     (exception.match? <exception> error))))]

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

(def: random_label
  (Random Name)
  (random.and (random.ascii/alpha 1)
              (random.ascii/alpha 1)))

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

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Parser])
      ($_ _.and
          (do {! random.monad}
            [expected (random.ascii/alpha 1)]
            (_.cover [/.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 (\ ! map (|>> #xml.Text) (random.ascii/alpha 1))]
            (_.cover [/.any]
                     (|> (/.result /.any (list expected))
                         (try\map (xml\= expected))
                         (try.else false))))
          (do {! random.monad}
            [expected ..random_tag]
            (_.cover [/.tag]
                     (|> (/.result (do //.monad
                                     [actual /.tag
                                      _ /.any]
                                     (in (name\= expected actual)))
                                   (list (#xml.Node expected (dictionary.empty name.hash) (list))))
                         (!expect (#try.Success #1)))))
          (do {! random.monad}
            [expected ..random_tag]
            (_.cover [/.node]
                     (|> (/.result (/.node expected (//\in []))
                                   (list (#xml.Node expected (dictionary.empty name.hash) (list))))
                         (!expect (#try.Success [])))))
          (!failure /.wrong_tag
                    [[(/.node ["" expected] (//\in []))
                      (#xml.Node [expected ""] (dictionary.empty name.hash) (list))]])
          (do {! random.monad}
            [expected_tag ..random_tag
             expected_attribute ..random_attribute
             expected_value (random.ascii/alpha 1)]
            (_.cover [/.attribute]
                     (|> (/.result (<| (/.node expected_tag)
                                       (//.after (/.attribute expected_attribute))
                                       (//\in []))
                                   (list (#xml.Node expected_tag
                                                    (|> (dictionary.empty name.hash)
                                                        (dictionary.has expected_attribute expected_value))
                                                    (list))))
                         (!expect (#try.Success [])))))
          (!failure /.unknown_attribute
                    [[(/.attribute ["" expected])
                      (#xml.Node [expected expected]
                                 (|> (dictionary.empty name.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 name.hash)
                                 (list))]
                     [(do //.monad
                        [_ /.any]
                        (/.node [expected expected]
                                (/.attribute [expected expected])))
                      (#xml.Node [expected expected]
                                 (|> (dictionary.empty name.hash)
                                     (dictionary.has [expected expected] expected))
                                 (list))]])
          (!failure /.unexpected_input
                    [[/.text
                      (#xml.Node [expected expected] (dictionary.empty name.hash) (list))]
                     [(/.node [expected expected]
                              (//\in []))
                      (#xml.Text expected)]
                     [(/.node [expected expected]
                              (/.attribute [expected expected]))
                      (#xml.Text expected)]])
          (do {! random.monad}
            [.let [node (: (-> xml.Tag (List xml.XML) xml.XML)
                           (function (_ tag children)
                             (#xml.Node tag (dictionary.empty name.hash) children)))]
             parent ..random_tag
             right ..random_tag
             wrong (random.only (|>> (name\= right) not)
                                ..random_tag)
             .let [parser (<| (/.node parent)
                              (do //.monad
                                [_ (<| /.somewhere
                                       (/.node right)
                                       (//\in []))
                                 _ (//.some /.any)]
                                (in [])))]
             repetitions (\ ! map (n.% 10) random.nat)]
            ($_ _.and
                (_.cover [/.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 []))))
                (_.cover [/.nowhere]
                         (|> (/.result parser
                                       (list (node parent
                                                   (list.repeated repetitions (node wrong (list))))))
                             (!expect (^multi (#try.Failure error)
                                              (exception.match? /.nowhere error)))))
                ))
          )))