aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/text/format.lux
blob: a6a9aae061e2eb95208d231d9c6f93daa2779c8b (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
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [abstract
     [monad (#+ do)]
     [equivalence (#+ Equivalence)]
     [functor
      [\\specification
       ["$." contravariant]]]]
    [control
     ["." try]]
    [data
     ["." text ("#\." equivalence)]
     ["." bit]
     ["." name]
     [format
      ["." xml]
      ["." json]]
     [collection
      ["." list ("#\." functor)]]]
    ["." time
     ["." day]
     ["." month]
     ["." instant]
     ["." duration]
     ["." date]]
    [math
     ["." random (#+ Random) ("#\." monad)]
     ["." modulus]
     ["." modular]
     [number
      ["." nat]
      ["." int]
      ["." rev]
      ["." frac]
      ["." ratio]]]
    [macro
     ["." code]]
    [meta
     ["." location]]
    ["." type]]]
  ["$." /// #_
   [format
    ["#." xml]
    ["#." json]]
   ["#." name]
   [//
    ["#." type]
    [macro
     ["#." code]]]]
  [\\library
   ["." /]])

(implementation: (equivalence example)
  (All (_ a) (-> a (Equivalence (/.Format a))))

  (def: (= reference subject)
    (text\= (reference example) (subject example))))

(def: random_contravariant
  (Random (Ex (_ a) [(/.Format a)
                     (Random a)]))
  ($_ random.either
      (random\in [/.bit random.bit])
      (random\in [/.nat random.nat])
      (random\in [/.int random.int])
      (random\in [/.rev random.rev])
      (random\in [/.frac random.frac])
      ))

(def: codec
  Test
  (`` ($_ _.and
          (~~ (template [<format> <codec> <random>]
                [(do random.monad
                   [sample <random>]
                   (_.cover [<format>]
                            (text\= (\ <codec> encoded sample)
                                    (<format> sample))))]

                [/.bit bit.codec random.bit]
                [/.nat nat.decimal random.nat]
                [/.int int.decimal random.int]
                [/.rev rev.decimal random.rev]
                [/.frac frac.decimal random.frac]
                [/.ratio ratio.codec random.ratio]
                [/.name name.codec ($///name.random 5 5)]
                [/.xml xml.codec $///xml.random]
                [/.json json.codec $///json.random]
                [/.day day.codec random.day]
                [/.month month.codec random.month]
                [/.instant instant.codec random.instant]
                [/.duration duration.codec random.duration]
                [/.date date.codec random.date]
                [/.time time.codec random.time]
                
                [/.nat/2 nat.binary random.nat]
                [/.nat/8 nat.octal random.nat]
                [/.nat/10 nat.decimal random.nat]
                [/.nat/16 nat.hex random.nat]
                
                [/.int/2 int.binary random.int]
                [/.int/8 int.octal random.int]
                [/.int/10 int.decimal random.int]
                [/.int/16 int.hex random.int]
                
                [/.rev/2 rev.binary random.rev]
                [/.rev/8 rev.octal random.rev]
                [/.rev/10 rev.decimal random.rev]
                [/.rev/16 rev.hex random.rev]
                
                [/.frac/2 frac.binary random.frac]
                [/.frac/8 frac.octal random.frac]
                [/.frac/10 frac.decimal random.frac]
                [/.frac/16 frac.hex random.frac]
                ))
          )))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Format])
      (`` ($_ _.and
              (_.for [/.functor]
                     (do random.monad
                       [[format random] ..random_contravariant
                        example random]
                       ($contravariant.spec (..equivalence example)
                                            format
                                            /.functor)))
              
              (do random.monad
                [left (random.unicode 5)
                 mid (random.unicode 5)
                 right (random.unicode 5)]
                (_.cover [/.format]
                         (text\= (/.format left mid right)
                                 ($_ "lux text concat" left mid right))))
              ..codec
              (~~ (template [<format> <alias> <random>]
                    [(do random.monad
                       [sample <random>]
                       (_.cover [<format>]
                                (text\= (<alias> sample)
                                        (<format> sample))))]

                    [/.text text.format (random.unicode 5)]
                    [/.code code.format $///code.random]
                    [/.type type.format ($///type.random 0)]
                    [/.location location.format
                     ($_ random.and
                         (random.unicode 5)
                         random.nat
                         random.nat)]
                    ))
              (do random.monad
                [members (random.list 5 random.nat)]
                (_.cover [/.list]
                         (text\= (/.list /.nat members)
                                 (|> members
                                     (list\each /.nat)
                                     (text.interposed " ")
                                     list
                                     (/.list (|>>))))))
              (do random.monad
                [sample (random.maybe random.nat)]
                (_.cover [/.maybe]
                         (case sample
                           #.None
                           true
                           
                           (#.Some value)
                           (text.contains? (/.nat value)
                                           (/.maybe /.nat sample)))))
              (do {! random.monad}
                [modulus (random.one (|>> modulus.modulus
                                          try.maybe)
                                     random.int)
                 sample (\ ! each (modular.modular modulus)
                           random.int)]
                (_.cover [/.mod]
                         (text\= (\ (modular.codec modulus) encoded sample)
                                 (/.mod sample))))
              ))))