aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/abstract/codec.lux
blob: ee4752b99ffdddd4d382ede8c0c4a66aa073f6a9 (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
(.using
 [library
  [lux (.full)
   ["_" test (.only Test)]
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" try]]
   [data
    ["[0]" bit ("[1]#[0]" equivalence)]
    [format
     ["[0]" json (.only JSON)]]]
   [math
    ["[0]" random (.only Random)]]]]
 [\\library
  ["[0]" / (.only Codec)
   [//
    [equivalence (.only Equivalence)]]]])

(def: json
  (Codec JSON Bit)
  (let [field "value"]
    (implementation
     (def: encoded
       (|>> {json.#Boolean}
            [field]
            list
            json.object))
     (def: decoded
       (json.boolean_field field)))))

(def: codec
  (Codec Text Bit)
  (/.composite json.codec ..json))

(def: .public test
  Test
  (do random.monad
    [expected random.bit]
    (<| (_.covering /._)
        (_.coverage [/.composite]
          (case (|> expected (# ..codec encoded) (# ..codec decoded))
            {try.#Success actual}
            (bit#= expected actual)
            
            {try.#Failure error}
            false)))))