aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/format/xml.lux
blob: 27f2e76a5c76d24f6200cc2da06af34d8f64b91a (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
(.module:
  [library
   [lux {"-" [char]}
    ["_" test {"+" [Test]}]
    [abstract
     [monad {"+" [Monad do]}]
     [\\specification
      ["$[0]" equivalence]
      ["$[0]" codec]]]
    [control
     pipe
     ["[0]" maybe]
     ["[0]" try]
     ["p" parser
      ["</>" xml]]]
    [data
     ["[0]" name]
     ["[0]" text ("[1]\[0]" equivalence)
      ["%" format {"+" [format]}]]
     [collection
      ["[0]" dictionary]
      ["[0]" list ("[1]\[0]" functor)]]]
    [math
     ["[0]" random {"+" [Random]} ("[1]\[0]" monad)]
     [number
      ["n" nat]]]]]
  [\\library
   ["[0]" / {"+" [XML]}]])

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

(def: char
  (Random Nat)
  (do [! random.monad]
    [idx (|> random.nat (\ ! 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: identifier
  (Random Name)
  (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)]
                             ($_ random.and
                                 ..identifier
                                 (random.dictionary name.hash size ..identifier (..text 0 10))
                                 (random.list size random)))))))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.XML])
      ($_ _.and
          (_.for [/.equivalence]
                 ($equivalence.spec /.equivalence ..random))
          (_.for [/.codec]
                 ($codec.spec /.equivalence /.codec ..random))

          (do [! random.monad]
            [(^@ identifier [namespace name]) ..identifier]
            (`` ($_ _.and
                    (~~ (template [<type> <format>]
                          [(_.cover [<type> <format>]
                                    (and (text\= name (<format> ["" name]))
                                         (let [identifier (<format> identifier)]
                                           (and (text.starts_with? namespace identifier)
                                                (text.ends_with? name identifier)))))]

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