aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/text/format.lux
blob: 1f0c2c9e6062dcd0927687cde01f17830bf35a60 (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
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [abstract
    [monad (#+ do)]
    [equivalence (#+ Equivalence)]
    [functor
     {[0 #spec]
      [/
       ["$." contravariant]]}]]
   [control
    ["." try]]
   [data
    ["." text ("#\." equivalence)]
    ["." bit]
    ["." name]
    [format
     ["." xml]
     ["." json]]
    [collection
     ["." list ("#\." functor)]]]
   ["." time
    ["." 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]]]]
  {1
   ["." /]})

(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\wrap [/.bit random.bit])
      (random\wrap [/.nat random.nat])
      (random\wrap [/.int random.int])
      (random\wrap [/.rev random.rev])
      (random\wrap [/.frac random.frac])
      ))

(def: #export 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))))
              (~~ (template [<format> <codec> <random>]
                    [(do random.monad
                       [sample <random>]
                       (_.cover [<format>]
                                (text\= (\ <codec> encode 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]
                    [/.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]
                    ))
              (~~ (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]
                    [/.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\map /.nat)
                                     (text.join_with " ")
                                     list
                                     (/.list (|>>))))))
              (do {! random.monad}
                [modulus (random.one (|>> modulus.modulus
                                          try.to_maybe)
                                     random.int)
                 sample (\ ! map (modular.modular modulus)
                           random.int)]
                (_.cover [/.mod]
                         (text\= (\ (modular.codec modulus) encode sample)
                                 (/.mod sample))))
              ))))