aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/abstract/codec.lux
blob: d3338547a6e465a0576f776a9a499bcc1a8c19a2 (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
(.module:
  [library
   [lux "*"
    ["_" test {"+" [Test]}]
    [abstract
     [monad {"+" [do]}]]
    [control
     ["[0]" try]]
    [data
     ["[0]" bit ("[1]#[0]" equivalence)]
     [format
      ["[0]" json {"+" [JSON]}]]]
    [math
     ["[0]" random {"+" [Random]}]]]]
  [\\library
   ["[0]" / {"+" [Codec]}
    [//
     [equivalence {"+" [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 /._)
        (_.cover [/.composite]
                 (case (|> expected (# ..codec encoded) (# ..codec decoded))
                   {try.#Success actual}
                   (bit#= expected actual)
                   
                   {try.#Failure error}
                   false)))))