-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathjson_parser_test.cw
232 lines (208 loc) · 8.25 KB
/
json_parser_test.cw
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
module:
import fmt
import jp = json_parser
import test
global test_empty = ""
global test_val_num = """ 0 """
global test_val_bool = """ false """
global test_val_str = """ "str" """
global test_val_str_esc = r""" "str\"" """
fun test_simple() void:
test::AssertEq#(jp::NumJsonObjectsNeeded(test_val_bool), 1_u32)
test::AssertEq#(jp::NumJsonObjectsNeeded(test_val_num), 1_u32)
test::AssertEq#(jp::NumJsonObjectsNeeded(test_val_str), 1_u32)
--
let! objects = {[200]jp::Object:}
ref let! file jp::File
--
set file = {jp::File: test_empty, objects}
test::AssertIs#(jp::Parse(@!file), jp::DataError)
--
set file = {jp::File: test_val_num, objects}
test::AssertIs#(jp::Parse(@!file), jp::Success)
test::AssertEq#(file.used_objects, jp::NumJsonObjectsNeeded(test_val_num))
test::AssertEq#(jp::IndexGetKind(file.root), jp::ObjKind:Atom)
test::AssertEq#(jp::AtomGetKind(@file, file.root), jp::AtomKind:Num)
test::AssertSliceEq#(jp::AtomGetData(@file, file.root), "0")
--
set file = {jp::File: test_val_bool, objects}
test::AssertIs#(jp::Parse(@!file), jp::Success)
test::AssertEq#(file.used_objects, jp::NumJsonObjectsNeeded(test_val_bool))
test::AssertEq#(jp::IndexGetKind(file.root), jp::ObjKind:Atom)
test::AssertEq#(jp::AtomGetKind(@file, file.root), jp::AtomKind:Num)
test::AssertSliceEq#(jp::AtomGetData(@file, file.root), "false")
--
set file = {jp::File: test_val_str, objects}
test::AssertIs#(jp::Parse(@!file), jp::Success)
test::AssertEq#(file.used_objects, jp::NumJsonObjectsNeeded(test_val_str))
test::AssertEq#(jp::IndexGetKind(file.root), jp::ObjKind:Atom)
test::AssertEq#(jp::AtomGetKind(@file, file.root), jp::AtomKind:Str)
test::AssertSliceEq#(jp::AtomGetData(@file, file.root), "str")
--
set file = {jp::File: test_val_str_esc, objects}
test::AssertIs#(jp::Parse(@!file), jp::Success)
test::AssertEq#(
file.used_objects, jp::NumJsonObjectsNeeded(test_val_str_esc))
test::AssertEq#(jp::IndexGetKind(file.root), jp::ObjKind:Atom)
test::AssertEq#(jp::AtomGetKind(@file, file.root), jp::AtomKind:EscStr)
test::AssertSliceEq#(jp::AtomGetData(@file, file.root), r"""str\"""")
global test_dict_empty = """{}"""
global test_dict_simple = """{"ip": "8.8.8.8"}"""
global test_dict_small = """ {
"string": "string",
"bool": false,
"num": 127664
} """
fun test_dict() void:
test::AssertEq#(jp::NumJsonObjectsNeeded(test_dict_empty), 1_u32)
test::AssertEq#(jp::NumJsonObjectsNeeded(test_dict_simple), 4_u32)
test::AssertEq#(jp::NumJsonObjectsNeeded(test_dict_small), 10_u32)
--
let! objects = {[200]jp::Object:}
ref let! file jp::File
--
set file = {jp::File: test_dict_empty, objects}
test::AssertIs#(jp::Parse(@!file), jp::Success)
test::AssertEq#(
file.used_objects, jp::NumJsonObjectsNeeded(test_dict_empty))
test::AssertEq#(jp::IndexGetKind(file.root), jp::ObjKind:Cont)
test::AssertEq#(jp::ContGetKind(@file, file.root), jp::ContKind:Dict)
--
set file = {jp::File: test_dict_simple, objects}
test::AssertIs#(jp::Parse(@!file), jp::Success)
test::AssertEq#(
file.used_objects, jp::NumJsonObjectsNeeded(test_dict_simple))
test::AssertEq#(jp::IndexGetKind(file.root), jp::ObjKind:Cont)
test::AssertEq#(jp::ContGetKind(@file, file.root), jp::ContKind:Dict)
--
set file = {jp::File: test_dict_small, objects}
test::AssertIs#(jp::Parse(@!file), jp::Success)
test::AssertEq#(
file.used_objects, jp::NumJsonObjectsNeeded(test_dict_small))
test::AssertEq#(jp::IndexGetKind(file.root), jp::ObjKind:Cont)
test::AssertEq#(jp::ContGetKind(@file, file.root), jp::ContKind:Dict)
global test_vec_empty = """[]"""
global test_vec_simple = """[100]"""
global test_vec_small = """[100, 500, 300, 200, 400 ]"""
fun test_vec() void:
test::AssertEq#(jp::NumJsonObjectsNeeded(test_vec_empty), 1_u32)
test::AssertEq#(jp::NumJsonObjectsNeeded(test_vec_simple), 3_u32)
test::AssertEq#(jp::NumJsonObjectsNeeded(test_vec_small), 11_u32)
--
let! objects = {[200]jp::Object:}
ref let! file jp::File
--
set file = {jp::File: test_vec_empty, objects}
test::AssertIs#(jp::Parse(@!file), jp::Success)
test::AssertEq#(file.used_objects, jp::NumJsonObjectsNeeded(test_vec_empty))
test::AssertEq#(jp::IndexGetKind(file.root), jp::ObjKind:Cont)
test::AssertEq#(jp::ContGetKind(@file, file.root), jp::ContKind:Vec)
test::AssertEq#(jp::ContGetSize(@file, file.root), 0_u32)
--
set file = {jp::File: test_vec_simple, objects}
test::AssertIs#(jp::Parse(@!file), jp::Success)
test::AssertEq#(
file.used_objects, jp::NumJsonObjectsNeeded(test_vec_simple))
test::AssertEq#(jp::IndexGetKind(file.root), jp::ObjKind:Cont)
test::AssertEq#(jp::ContGetKind(@file, file.root), jp::ContKind:Vec)
test::AssertEq#(jp::ContGetSize(@file, file.root), 1_u32)
--
set file = {jp::File: test_vec_small, objects}
test::AssertIs#(jp::Parse(@!file), jp::Success)
test::AssertEq#(file.used_objects, jp::NumJsonObjectsNeeded(test_vec_small))
test::AssertEq#(jp::IndexGetKind(file.root), jp::ObjKind:Cont)
test::AssertEq#(jp::ContGetKind(@file, file.root), jp::ContKind:Vec)
test::AssertEq#(jp::ContGetSize(@file, file.root), 5_u32)
global test4 = """[
{
"string": "string",
"bool": false,
"num": 127664
}
]"""
global test5 = """[
{
"string": "string",
"bool": false,
"num": 127664
},
{
"string": "string",
"bool": false,
"num": 127664
}
]"""
fun test_parser() void:
test::AssertEq#(jp::NumJsonObjectsNeeded(test4), 12_u32)
test::AssertEq#(jp::NumJsonObjectsNeeded(test5), 23_u32)
test::AssertEq#(jp::NumJsonObjectsNeeded(test6), 99_u32)
--
let! objects = {[200]jp::Object:}
ref let! file jp::File
--
set file = {jp::File: test4, objects}
test::AssertIs#(jp::Parse(@!file), jp::Success)
test::AssertEq#(file.used_objects, jp::NumJsonObjectsNeeded(test4))
test::AssertEq#(jp::IndexGetKind(file.root), jp::ObjKind:Cont)
test::AssertEq#(jp::ContGetKind(@file, file.root), jp::ContKind:Vec)
test::AssertEq#(jp::ContGetSize(@file, file.root), 1_u32)
--
set file = {jp::File: test5, objects}
test::AssertIs#(jp::Parse(@!file), jp::Success)
test::AssertEq#(file.used_objects, jp::NumJsonObjectsNeeded(test5))
test::AssertEq#(jp::IndexGetKind(file.root), jp::ObjKind:Cont)
test::AssertEq#(jp::ContGetKind(@file, file.root), jp::ContKind:Vec)
test::AssertEq#(jp::ContGetSize(@file, file.root), 2_u32)
global test6 = """
{
"string": "string",
"bool": false,
"num": 127664,
"array": [ 100, 500, 300, 200, 400 ],
"dict":
{
"string": "string",
"bool": false,
"num": 127664,
"array": [ 100, 500, 300, 200, 400 ]
},
"array_of_dict": [
{
"string": "string",
"bool": false,
"num": 127664,
"array": [ 100, 500, 300, 200, 400 ]
},
{
"string": "string",
"bool": false,
"num": 127664,
"array": [ 100, 500, 300, 200, 400 ]
}]
}
"""
fun test_walk() void:
let! objects = {[200]jp::Object:}
ref let! file jp::File
set file = {jp::File: test6, objects}
test::AssertIs#(jp::Parse(@!file), jp::Success)
test::AssertEq#(file.used_objects, jp::NumJsonObjectsNeeded(test6))
test::AssertEq#(jp::IndexGetKind(file.root), jp::ObjKind:Cont)
test::AssertEq#(jp::ContGetKind(@file, file.root), jp::ContKind:Dict)
let array_item = jp::ContGetItemForKey(@file, file.root, "array_of_dict")
test::AssertNe#(array_item, jp::NullIndex)
let array = jp::ItemGetVal(@file, array_item)
test::AssertEq#(jp::IndexGetKind(array), jp::ObjKind:Cont)
let index1_item = jp::ContGetItemForIndex(@file, array, 1)
let index1 = jp::ItemGetVal(@file, index1_item)
let num_item = jp::ContGetItemForKey(@file, index1, "num")
let num = jp::ItemGetVal(@file, num_item)
test::AssertSliceEq#(jp::AtomGetData(@file, num), "127664")
fun main(argc s32, argv ^^u8) s32:
do test_simple()
do test_vec()
do test_dict()
do test_parser()
do test_walk()
test::Success#()
return 0