-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_json.mojo
417 lines (354 loc) · 12.9 KB
/
test_json.mojo
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
from json.parser import parse
from json.types import Value, JsonDict, JsonList
from testing import assert_equal, assert_almost_equal
from json.stringify import any_json_type_to_string, stringify
from json.lexer import lex
fn test_parse_array() raises -> None:
var tokens = List[Value]()
tokens.append(Value(String("[")))
tokens.append(Value(1))
tokens.append(Value(String(",")))
tokens.append(Value(2))
tokens.append(Value(String(",")))
tokens.append(Value(3))
tokens.append(Value(String("]")))
var initial_position = 0
var res = parse(tokens, initial_position)
print(stringify(res))
var json_list = res._variant[JsonList]._data
assert_equal(len(json_list), 3, "Length should be 3")
fn check_element(index: Int, value: Int) raises -> None:
assert_equal(
json_list[index]._variant.take[Int](),
value,
"Element should be " + str(value),
)
check_element(0, 1)
check_element(1, 2)
check_element(2, 3)
fn test_parse_object() raises -> None:
var lex_array = List[Value]()
lex_array.append(Value(String("{")))
lex_array.append(Value(String("key")))
lex_array.append(Value(String(":")))
lex_array.append(Value(1))
lex_array.append(Value(String(",")))
lex_array.append(Value(String("key_2")))
lex_array.append(Value(String(":")))
lex_array.append(Value(String("hello")))
lex_array.append(Value(String(",")))
lex_array.append(Value(String("key_3")))
lex_array.append(Value(String(":")))
lex_array.append(Value(False))
lex_array.append(Value(String(",")))
lex_array.append(Value(String("key_4")))
lex_array.append(Value(String(":")))
lex_array.append(Value(1.2))
lex_array.append(Value(String("}")))
var initial_position = 0
var res = parse(lex_array, initial_position)
print(stringify(res))
var json_dict = res._variant[JsonDict]._data
assert_equal(len(json_dict), 4, "Length should be 4")
fn check_element(key: String, value: Value) raises -> None:
assert_equal(
any_json_type_to_string(json_dict[key]),
any_json_type_to_string(value),
"Element should be " + any_json_type_to_string(value),
)
check_element("key", Value(1))
check_element("key_2", Value(String("hello")))
check_element("key_3", Value(False))
check_element("key_4", Value(1.2))
fn test_parse_object_with_array() raises -> None:
var lex_array = List[Value]()
lex_array.append(Value(String("{")))
lex_array.append(Value(String("key")))
lex_array.append(Value(String(":")))
lex_array.append(Value(1))
lex_array.append(Value(String(",")))
lex_array.append(Value(String("key_2")))
lex_array.append(Value(String(":")))
lex_array.append(Value(String("hello")))
lex_array.append(Value(String(",")))
lex_array.append(Value(String("key_3")))
lex_array.append(Value(String(":")))
lex_array.append(Value(False))
lex_array.append(Value(String(",")))
lex_array.append(Value(String("key_4")))
lex_array.append(Value(String(":")))
lex_array.append(Value(1.2))
lex_array.append(Value(String(",")))
lex_array.append(Value(String("key_5")))
lex_array.append(Value(String(":")))
lex_array.append(Value(String("[")))
lex_array.append(Value(1))
lex_array.append(Value(String(",")))
lex_array.append(Value(2))
lex_array.append(Value(String(",")))
lex_array.append(Value(3))
lex_array.append(Value(String("]")))
lex_array.append(Value(String("}")))
var initial_position = 0
var res = parse(lex_array, initial_position)
print(stringify(res))
var json_dict = res._variant[JsonDict]._data
assert_equal(len(json_dict), 5, "Length should be 5")
fn check_element(key: String, value: Value) raises -> None:
assert_equal(
any_json_type_to_string(json_dict[key]),
any_json_type_to_string(value),
"Element should be " + any_json_type_to_string(value),
)
check_element("key", Value(1))
check_element("key_2", Value(String("hello")))
check_element("key_3", Value(False))
check_element("key_4", Value(1.2))
var expected_list = List[Value]()
expected_list.append(Value(1))
expected_list.append(Value(2))
expected_list.append(Value(3))
check_element("key_5", Value(JsonList(expected_list)))
fn test_json_type_to_string() raises -> None:
var v1 = Value(str("json"))
print(any_json_type_to_string(v1))
var v2_list = List[Value]()
v2_list.append(Value(str("json")))
v2_list.append(Value(1))
v2_list.append(Value(1.1))
v2_list.append(Value(True))
v2_list.append(Value(False))
v2_list.append(Value(None))
var v2 = Value(JsonList(v2_list))
print(any_json_type_to_string(v2))
var v3_dict = Dict[String, Value]()
v3_dict["name"] = Value(str("John"))
v3_dict["age"] = Value(30)
v3_dict["wage"] = Value(25.4)
v3_dict["is_student"] = Value(False)
v3_dict["has_job"] = Value(True)
v3_dict["is_null"] = Value(None)
var v3 = Value(JsonDict(v3_dict))
print(any_json_type_to_string(v3))
var job1 = Dict[String, Value]()
job1["title"] = Value(str("Software Engineer"))
job1["company"] = Value(str("Google"))
job1["years"] = Value(2)
var job2 = Dict[String, Value]()
job2["title"] = Value(str("Product Manager"))
job2["company"] = Value(str("Facebook"))
job2["years"] = Value(3)
var job3 = Dict[String, Value]()
job3["title"] = Value(str("Data Scientist"))
job3["company"] = Value(str("Amazon"))
job3["years"] = Value(1)
var john = Dict[String, Value]()
john["name"] = Value(str("John"))
john["age"] = Value(30)
john["wage"] = Value(25.4)
var john_jobs = List[Value]()
john_jobs.append(Value(JsonDict(job1)))
john_jobs.append(Value(JsonDict(job2)))
john["jobs"] = Value(JsonList(john_jobs))
var sam = Dict[String, Value]()
sam["name"] = Value(str("Sam"))
sam["age"] = Value(25)
sam["wage"] = Value(20.4)
var people = List[Value]()
people.append(Value(JsonDict(john)))
people.append(Value(JsonDict(sam)))
var v4_dict = Dict[String, Value]()
v4_dict["people"] = Value(JsonList(people))
v4_dict["count"] = Value(2)
var v4 = Value(JsonDict(v4_dict))
print(any_json_type_to_string(v4))
fn test_lexor_array() raises -> None:
var v1: String = "[1, 2, 3]"
var tokens = lex(v1)
# for i in range(len(tokens)):
# print("Item: ", any_json_type_to_string(tokens[i]))
assert_equal(len(tokens), 7, "Length should be 5")
fn test_lexor_object() raises -> None:
var v1: String = '{"key": 1, "key_2": "hello", "key_3": false, "key_4": 1.2}'
var tokens = lex(v1)
# for i in range(len(tokens)):
# print("Item: ", any_json_type_to_string(tokens[i]))
assert_equal(len(tokens), 17, "Length should be 17")
fn test_lexor_sub_lists_and_objects() raises -> None:
var v1: String = '{ "key": [1, 2, 3], "key_2": { "name": "John", "age": 30 } }'
var tokens = lex(v1)
# for i in range(len(tokens)):
# print("Item: ", any_json_type_to_string(tokens[i]))
assert_equal(len(tokens), 23, "Length should be 23")
fn test_sub_lists_objects() raises -> None:
var raw_json: String = """
{
"name": "John",
"age": 30,
"wage": 25.4,
"is_student": false,
"has_job": true,
"is_null": null,
"nickname": "",
"address": {
"street": "Main Street",
"city": "New York",
"zip": 10001
},
"phone_numbers": [
"123-456-7890",
"098-765-4321"
],
"friends": [
{
"name": "Jane",
"age": 25,
"phone_numbers": [
"123-456-7890",
"098-765-4321"
]
},
{
"name": "Doe",
"age": 35
}
]
}
"""
var tokens = lex(raw_json)
for i in range(len(tokens)):
print("Item: ", any_json_type_to_string(tokens[i]))
assert_equal(len(tokens), 85, "Length should be 85")
var initial_position = 0
var res = parse(tokens, initial_position)
print(stringify(res))
fn test_object_with_list_object() raises -> None:
var lex_array = List[Value]()
lex_array.append(Value(String("{")))
lex_array.append(Value(String("friends")))
lex_array.append(Value(String(":")))
lex_array.append(Value(String("[")))
lex_array.append(Value(String("{")))
lex_array.append(Value(String("name")))
lex_array.append(Value(String(":")))
lex_array.append(Value(String("Jane")))
lex_array.append(Value(String(",")))
lex_array.append(Value(String("age")))
lex_array.append(Value(String(":")))
lex_array.append(Value(25))
lex_array.append(Value(String(",")))
lex_array.append(Value(String("phone_numbers")))
lex_array.append(Value(String(":")))
lex_array.append(Value(String("[")))
lex_array.append(Value(String("123-456-7890")))
lex_array.append(Value(String(",")))
lex_array.append(Value(String("098-765-4321")))
lex_array.append(Value(String("]")))
lex_array.append(Value(String("}")))
lex_array.append(Value(String(",")))
lex_array.append(Value(String("{")))
lex_array.append(Value(String("name")))
lex_array.append(Value(String(":")))
lex_array.append(Value(String("Doe")))
lex_array.append(Value(String(",")))
lex_array.append(Value(String("age")))
lex_array.append(Value(String(":")))
lex_array.append(Value(35))
lex_array.append(Value(String("}")))
lex_array.append(Value(String("]")))
lex_array.append(Value(String("}")))
var initial_position = 0
var res = parse(lex_array, initial_position)
print(stringify(res))
fn test_object_with_list_object_2() raises -> None:
var raw_json: String = """
{
"friends": [
{"name": 1}
]
}
"""
var tokens = lex(raw_json)
var initial_position = 0
var res = parse(tokens, initial_position)
print(stringify(res))
fn test_negative_integer() raises -> None:
var raw_json: String = """
{
"number": -1
}
"""
var tokens = lex(raw_json)
var initial_position = 0
var res = parse(tokens, initial_position)
print(stringify(res))
assert_equal(any_json_type_to_string(res), '{"number":-1}', "Should be -1")
fn test_negative_float() raises -> None:
"""TODO: Currently failing cause of floating point issues."""
var raw_json: String = """
{
"number": -1.1
}
"""
var tokens = lex(raw_json)
var initial_position = 0
var res = parse(tokens, initial_position)
print(stringify(res))
assert_equal(
any_json_type_to_string(res), '{"number":-1.1}', "Should be -1.1"
)
fn test_negative_floats_in_array() raises -> None:
var raw_json: String = '{"numbers": [-65.613616999999977,43.420273000000009]}'
var tokens = lex(raw_json)
var initial_position = 0
var res = parse(tokens, initial_position)
print(stringify(res))
assert_equal(
any_json_type_to_string(res),
'{"numbers":[-65.613616999999977,43.420273000000009]}',
"Should be [-65.613616999999977,43.420273000000009] but got "
+ any_json_type_to_string(res),
)
fn test_handle_escaped_special_characters() raises -> None:
var raw_json: String = '{"quote": "\\""'
var tokens = lex(raw_json)
var expected: String = '\\"'
assert_equal(
tokens[3]._variant.take[String](), expected, "Should be " + expected
)
fn test_handle_escaped_special_characters_2() raises -> None:
var raw_json: String = """{"quote": "<a href=\\"http://twitter.com/download/iphone\\" rel=\\"nofollow\\">Twitter for iPhone</a>"}"""
var tokens = lex(raw_json)
var expected: String = '"'
assert_equal(
tokens[3]._variant.take[String](), expected, "Should be " + expected
)
fn test_handle_escaped_special_characters_3() raises -> None:
"TODO: Currently failing cause of escaping issues."
var raw_json: String = '{"quote": "\\\\"}'
var tokens = lex(raw_json)
var expected: String = "\\\\"
assert_equal(
tokens[3]._variant.take[String](), expected, "Should be " + expected
)
fn test_handle_empty_string() raises -> None:
"TODO: Currently failing cause of escaping issues."
var raw_json: String = '{"quote":""}'
var tokens = lex(raw_json)
var expected: String = ""
assert_equal(
tokens[3]._variant.take[String](), expected, "Should be " + expected
)
fn run_test(test_fn: fn () raises -> None) raises -> None:
print("Running test: ")
try:
test_fn()
print("Test passed: ")
except e:
print("Test failed: ", e)
fn main() raises:
# run_test(test_negative_float)
run_test(test_negative_floats_in_array)
# run_test(test_handle_escaped_special_characters)
# run_test(test_handle_escaped_special_characters_3)
# run_test(test_handle_empty_string)