-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
115 lines (102 loc) · 2.48 KB
/
test.py
File metadata and controls
115 lines (102 loc) · 2.48 KB
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
# vim: set et ts=4 sw=4
# coding: utf8
# !/usr/bin/python
import json
import jsonparser
import logging
from jsonparser import InvalidJSONStringFormatException
json_ok = [
('{}', 1),
('{"":""}', 1),
('{"a":123}', 1),
('{"a":-123}', 1),
('{"a":1.23}', 1),
('{"a":1e1}', 1),
('{"a":true,"b":false}', 1),
('{"a":null}', 1),
('{"a":[]}', 1),
('{"a":{}}', 1),
(' {"a:": 123}', 1),
('{ "a " : 123}', 1),
('{ "a" : 123 }', 1),
('{"true": "null"}', 1),
('{"":"\\t\\n"}', 1),
('{"\\"":"\\""}', 1),
]
json_ok2 = [
('{"a":"abcde,:-+{}[]"}', 2),
('{"a": [1,2,"abc"]}', 2),
('{"d{": "}dd", "a":123}', 2),
('{"a": {"a": {"a": 123}}}', 2),
('{"a": {"a": {"a": [1,2,[3]]}}}', 2),
('{"a": "\\u7f51\\u6613CC\\"\'"}', 3),
('{"a":1e-1, "cc": -123.4}', 2),
('{ "{ab" : "}123", "\\\\a[": "]\\\\"}', 3), ]
json_ex = [
# exceptions
('{"a":[}', 2),
('{"a":"}', 2),
('{"a":True}', 1),
('{"a":Null}', 1),
('{"a":foobar}', 2),
("{'a':1}", 3),
('{1:1}', 2),
('{true:1}', 2),
('{"a":{}', 2),
('{"a":-}', 1),
('{"a":[,]}', 2),
('{"a":.1}', 1),
('{"a":+123}', 1),
('{"a":1..1}', 1),
('{"a":--1}', 1),
('{"a":"""}', 1),
('{"a":"\\"}', 1),
]
def test_ok_1():
print('Test OK 1')
print('-' * 50)
for (test, ptr) in json_ok:
#try:
jsonparser.loads(test)
#except Exception as e:
# logging.warning('%s', test)
# raise e
# break
c = json.loads(test)
if c != jsonparser.dump_dict():
print(c)
print(jsonparser.dump_dict())
else:
print('Passed')
def test_ok_2():
print('Test OK 2')
print('-' * 50)
for (test, ptr) in json_ok2:
try:
jsonparser.loads(test)
except Exception as e:
logging.warning('%s', test)
raise e
break
c = json.loads(test)
if c != jsonparser.dump_dict():
print(c)
print(jsonparser.dump_dict())
else:
print('Passed')
def test_ex():
print('Test Exception Case')
print('-' * 50)
for (test, ptr) in json_ex:
try:
jsonparser.loads(test)
except InvalidJSONStringFormatException as e:
print('Passed')
pass
else:
logging.warning('%s', test)
break
if __name__ == "__main__":
test_ok_1()
test_ok_2()
test_ex()