generated from koka-community/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserjson_test.kk
179 lines (148 loc) · 4.78 KB
/
serjson_test.kk
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
module serjson_test
import std/text/parse
import std/test/test
import serjson
// some structs to test parsing nested structures
struct thing {
name: string
}
struct stuff {
foo: int
bar: string
thing: thing
}
fun thing/(==)(a: thing, b: thing): bool
a.name == b.name
fun stuff/(==)(a: stuff, b: stuff): bool
a.foo == b.foo
&& a.bar == b.bar
&& a.thing == b.thing
fun eitherstuff/(==)(a: either<string,stuff>, b: either<string,stuff>): bool
match (a,b)
(Left(_aa),Left(_bb)) -> False
(Right(aa),Right(bb)) -> aa == bb
_ -> False
fun either/list/(==)(a: either<string,list<int>>, b: either<string,list<int>>): bool
match (a,b)
(Left(_aa),Left(_bb)) -> False
(Right(aa),Right(bb)) -> aa == bb
_ -> False
// valuefn and pfn for thing and stuff
fun thing/valuefn(t: thing)
Object([
Object-entry("name", String(t.name))
])
fun thing/pfn(v: value)
match v
Object([
Object-entry("name", String(name))]) ->
Thing(name)
_ -> fail("thing: bad json")
fun stuff/valuefn(s: stuff)
Object([
Object-entry("foo", Int(s.foo)),
Object-entry("bar", String(s.bar)),
Object-entry("thing", thing/valuefn(s.thing))
])
pub fun stuff/pfn(v: value)
match v
Object([
Object-entry("foo", Int(foo)),
Object-entry("bar", String(bar)),
Object-entry("thing", tv)]) ->
val t = thing/pfn(tv)
Stuff(foo, bar, t)
_ -> fail("stuff: bad json")
fun stuff/show(s: stuff)
s.json(?valuefn=stuff/valuefn).show
// tests
pub fun test-serjson()
basic/scope("value/(==)")
scoped/test("string-equals")
expect(String("foo"), fn () String("foo"))
scoped/test("int-equals")
expect(Int(1), fn () Int(1))
scoped/test("bool-equals")
expect(Bool(True), fn () Bool(True))
scoped/test("null-equals")
expect(Null, fn () Null)
scoped/test("array-test")
expect(Array([Int(1), Int(2)]), fn () Array([Int(1), Int(2)]))
scoped/test("object-test")
expect(Object([Object-entry("foo", Int(1))]), fn () Object([Object-entry("foo", Int(1))]))
basic/scope("simple parse")
scoped/test("simple parse")
val res = "{\"foo\": [1,\"two\",true,{\"bar\": 200}]}"
val expectation = Right(Object([
Object-entry("foo", Array([
Int(1),
String("two"),
Bool(True),
Object([
Object-entry("bar", Int(200))])]))]))
expect(expectation, fn () res.parse-value, ?(==) = eithervalue/(==))
basic/scope("simple roundtrip")
scoped/test("simple roundtrip")
val obj = Object([
Object-entry("foo", Array([
Int(1),
String("two"),
Bool(True),
Object([
Object-entry("bar", Int(200))])]))])
val json = obj.show
val reparsed = json.parse-value
val expectation = Right(obj)
expect(expectation, fn () reparsed, ?(==)=eithervalue/(==))
basic/scope("nested parse")
scoped/test("nested parse")
val json = "{\"foo\": 10, \"bar\": \"blah\", \"thing\": {\"name\": \"graagh\"}}"
val parsed: either<string,stuff> = json.dejson(?pfn=stuff/pfn)
val expectation = Right(Stuff(10, "blah", Thing("graagh")))
expect(expectation, fn () parsed)
basic/scope("nested roundtrip")
scoped/test("stuff-roundtrip")
val s = Stuff(10, "blah", Thing("graagh"))
val json = s.json(?valuefn=stuff/valuefn)
val reparsed: either<string,stuff> = json.dejson
val expectation = Right(s)
expect(expectation, fn () reparsed)
basic/scope("lists")
scoped/test("empty-list")
val s = []
val json = s.json(?valuefn=list/make-valuefn(int/valuefn))
val reparsed: either<string,list<int>> = json.dejson(?pfn=list/make-pfn(int/pfn))
val expectation = Right(s)
expect(expectation, fn () reparsed)
scoped/test("list of ints")
val s = [1,2,3]
val json = s.json(?valuefn=list/make-valuefn(int/valuefn))
val reparsed: either<string,list<int>> = json.dejson(?pfn=list/make-pfn(int/pfn))
val expectation = Right(s)
expect(expectation, fn () reparsed)
// examples
pub fun example-value-parse()
"{\"foo\": [1,\"two\",true,{\"bar\": 200}]}".parse-value.show
pub fun example-value-roundtrip()
val obj = Object([
Object-entry("foo", Array([
Int(1),
String("two"),
Bool(True),
Object([
Object-entry("bar", Int(200))
])
]))
])
val json = obj.show
val reparsed = json.parse-value
reparsed.show
pub fun example-nested-parse()
val json = "{\"foo\": 10, \"bar\": \"blah\", \"thing\": {\"name\": \"graagh\"}}"
val parsed: either<string,stuff> = json.dejson(?pfn=stuff/pfn)
parsed.show
pub fun example-nested-roundtrip()
val s = Stuff(10, "blah", Thing("graagh"))
val json = s.json
val reparsed: either<string,stuff> = json.dejson
reparsed.show