-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
160 lines (153 loc) · 5.95 KB
/
test.py
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
# -*- coding: utf-8 -*-
from jsonparser import Jsonparser, Parser_exception
import unittest, json, logging, os, filecmp
class Test_parser(unittest.TestCase):
def setUp(self):
self.parser = Jsonparser()
self.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),
('{"\\"\u4e2d":"\\"\\\\/\\b\\f\\n\\r\\t"}', 1),
('{"\\"\u4e2d":"\\"\\/\\\\/\\b\\f\\n\\r\\t\\u4e2d\u4e2d中"}', 1),
('{"中\u4e2d\\u4e2d":"\\r"}', 1),
]
self.json_ok2 = [
('{"a":' + '1' * 310 + '.0' + '}', 2),
('{"a":"abcde,:-+{}[]"}', 2),
('{"a": [1,2,"abc"]}', 2),
('{"a": {"a": {"a": 123}}}', 2),
('{"a": {"a": {"a": [1,2,[3]]}}}', 2),
('{"a": "\\u7f51\\u6613CC\\"\'"}', 3),
('{"d{": "}dd", "a":123}', 2),
('{"a":1e-1, "cc": -123.4}', 2),
('{ "{ab" : "}123", "\\\\a[": "]\\\\"}', 3),
]
self.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),
('{"a":"\\"}', 1),
]
def test_loads_dumps(self):
print('test loads and dumps')
print('*' * 100)
print('test json_ok')
for num, case in enumerate(self.json_ok):
try:
self.parser.loads(case[0])
except Exception as e:
logging.warning('loads error: json_ok %d %s' % (num, case[0]))
raise e
d = json.loads(case[0])
if d != self.parser.dump_dict():
logging.warning('dump_dict error: json_ok %d %s' % (num, case[0]))
raise Exception
js = json.dumps(d)
if js != self.parser.dumps():
logging.warning('dumps error: json_ok %d %s %s %s' % (num, case[0], js, self.parser.dumps()))
raise Exception
try:
self.parser.load_dict(d)
except Exception as e:
logging.warning('load_dict error: json_ok %d %s' % (num, case[0]))
raise e
if d != self.parser._data:
logging.warning('load_dict error: json_ok %d %s' % (num, case[0]))
raise Exception
print('test json_ok2')
for num, case in enumerate(self.json_ok2):
try:
self.parser.loads(case[0])
except Exception as e:
logging.warning('loads error: json_ok2 %d %s' % (num, case[0]))
raise e
d = json.loads(case[0])
if d != self.parser.dump_dict():
logging.warning('dump_dict error: json_ok2 %d %s' % (num, case[0]))
raise Exception
print('test json_ex')
for num, case in enumerate(self.json_ex):
try:
self.parser.loads(case[0])
except Parser_exception as e:
# print(e.info)
continue
logging.warning('loads error: json_ex %d %s' % (num, case[0]))
raise Exception
print('Pass all test cases')
print('*' * 100)
def test_load_file_dump_file(self):
print('test load_file and dump_file')
print('*' * 100)
path = './test_case/json_test'
out_path = './test_case/json_result'
file_list = os.listdir(path)
file_list.sort()
for num, file in enumerate(file_list):
file_dir = os.path.join(path, file)
try:
self.parser.load_file(file_dir)
except Exception as e:
logging.warning('load_file error: %d %s' % (num, file))
raise e
with open(file_dir, 'r') as f_read:
temp = json.load(f_read)
if temp != self.parser.dump_dict():
logging.warning('dumps error: %d %s' % (num, file))
raise Exception
try:
self.parser.dump_file(os.path.join(out_path, file))
except Exception as e:
logging.warning('dump_file error: %d %s' % (num, file))
raise e
self.assertTrue(filecmp.cmp(os.path.join(path, file), os.path.join(out_path, file)))
print('Pass all test cases')
print('*' * 100)
def test_set_val_get_val_update(self):
print('test set_val get_val update')
print('*' * 100)
js = '{"a":true, "b":false, "c": [1, 6.7, "d"]}'
js2 = {'c': 0, 'd': None}
self.parser.loads(js)
d = json.loads(js)
self.assertEqual(self.parser._data, d)
self.assertEqual(self.parser['a'], True)
self.assertEqual(self.parser['c'], d['c'])
self.parser['c'] = 5
self.assertEqual(self.parser['c'], 5)
self.assertFalse(self.parser._data is self.parser.dump_dict())
self.parser.updata(js2)
self.assertEqual(self.parser['c'], 0)
print('Pass all test cases')
print('*' * 100)
if __name__ == '__main__':
unittest.main()