aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/macro/poly/json.lux
blob: 5e0bcfbd4f17be3b8b37aa538dd1e306d2cb5fc5 (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
(.module:
  [lux #*
   ["%" data/text/format (#+ format)]
   ["_" test (#+ Test)]
   [abstract
    codec
    [monad (#+ do)]
    [equivalence (#+ Equivalence)
     {[0 #poly]
      ["poly/equivalence" /]}]
    {[0 #spec]
     [/
      ["$." equivalence]
      ["$." codec]]}]
   [control
    pipe
    ["p" parser
     ## TODO: Get rid of this import ASAP
     [json (#+)]]]
   [data
    ["." bit]
    ["." maybe]
    ["." text]
    [number
     ["n" nat]
     ["." frac]]
    [format
     [json (#+)
      {[0 #poly]
       ["." /]}]]
    [collection
     [row (#+ row)]
     ["d" dictionary]
     ["." list]]]
   [macro
    [poly (#+ derived:)]]
   [type
    ["." unit]]
   [math
    ["r" random (#+ Random)]]
   [time
    ["ti" instant]
    ["tda" date]
    ## ["tdu" duration]
    ]]
  [test
   [lux
    [time
     ["_." instant]
     ## ["_." duration]
     ["_." date]]]])

(type: Variant
  (#Bit Bit)
  (#Text Text)
  (#Frac Frac))

(type: #rec Recursive
  (#Number Frac)
  (#Addition Frac Recursive))

(type: Record
  {#bit Bit
   #frac Frac
   #text Text
   #maybe (Maybe Frac)
   #list (List Frac)
   #dictionary (d.Dictionary Text Frac)
   #variant Variant
   #tuple [Bit Frac Text]
   #recursive Recursive
   ## #instant ti.Instant
   ## #duration tdu.Duration
   #date tda.Date
   #grams (unit.Qty unit.Gram)
   })

(def: gen-recursive
  (Random Recursive)
  (r.rec (function (_ gen-recursive)
           (r.or r.frac
                 (r.and r.frac gen-recursive)))))

(derived: recursive-equivalence (poly/equivalence.equivalence Recursive))

(def: qty
  (All [unit] (Random (unit.Qty unit)))
  (|> r.int (:: r.monad map unit.in)))

(def: gen-record
  (Random Record)
  (do {@ r.monad}
    [size (:: @ map (n.% 2) r.nat)]
    ($_ r.and
        r.bit
        r.frac
        (r.unicode size)
        (r.maybe r.frac)
        (r.list size r.frac)
        (r.dictionary text.hash size (r.unicode size) r.frac)
        ($_ r.or r.bit (r.unicode size) r.frac)
        ($_ r.and r.bit r.frac (r.unicode size))
        ..gen-recursive
        ## _instant.instant
        ## _duration.duration
        _date.date
        ..qty
        )))

(derived: equivalence (poly/equivalence.equivalence Record))
(derived: codec (/.codec Record))

(def: #export test
  Test
  (<| (_.context (%.name (name-of /._)))
      ($codec.spec ..equivalence ..codec gen-record)))