-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.py
136 lines (108 loc) · 5.03 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
import unittest
from partialjson.json_parser import JSONParser
class TestJSONParser(unittest.TestCase):
def setUp(self):
self.parser_strict = JSONParser(strict=True)
self.parser_non_strict = JSONParser(strict=False)
# Test for parser_strict
def test_parser_strict_incomplete_object(self):
with self.assertRaises(Exception):
self.parser_strict.parse(
'{"x": "1st line\\n2nd line', '{"x": "1st line\\n2nd line"}'
)
def test_parser_strict_incomplete_string(self):
with self.assertRaises(Exception):
self.parser_strict.parse(
'{"x": "1st line\\n2nd line"', '{"x": "1st line\\n2nd line"}'
)
def test_parser_strict_complete_string(self):
self.assertEqual(
self.parser_strict.parse('{"x": "1st line\\n2nd line"}').get("x"),
"1st line\n2nd line",
)
def test_parser_strict_incomplete_object(self):
self.assertEqual(
self.parser_strict.parse('{"x": "1st line\\n2nd line').get("x"),
"1st line\n2nd line",
)
def test_parser_strict_incomplete_string(self):
self.assertEqual(
self.parser_strict.parse('{"x": "1st line\\n2nd line"').get("x"),
"1st line\n2nd line",
)
# Test for parser_non_strict
def test_parser_non_strict_complete_string(self):
self.assertEqual(
self.parser_non_strict.parse('{"x": "1st line\\n2nd line"}').get("x"),
"1st line\n2nd line",
)
# Existing tests can remain unchanged...
# Number Tests
def test_positive_integer(self):
self.assertEqual(self.parser_strict.parse("42"), 42)
def test_negative_integer(self):
self.assertEqual(self.parser_strict.parse("-42"), -42)
def test_positive_float(self):
self.assertEqual(self.parser_strict.parse("12.34"), 12.34)
def test_negative_float(self):
self.assertEqual(self.parser_strict.parse("-12.34"), -12.34)
def test_incomplete_positive_float(self):
self.assertEqual(self.parser_strict.parse("12."), 12)
def test_incomplete_negative_float(self):
self.assertEqual(self.parser_strict.parse("-12."), -12)
def test_invalid_number(self):
with self.assertRaises(Exception):
self.parser_strict.parse("1.2.3.4")
# String Tests
def test_string(self):
self.assertEqual(self.parser_strict.parse('"I am text"'), "I am text")
self.assertEqual(self.parser_strict.parse('"I\'m text"'), "I'm text")
self.assertEqual(self.parser_strict.parse('"I\\"m text"'), 'I"m text')
def test_incomplete_string(self):
with self.assertRaises(Exception):
self.parser_strict.parse('"I am text', "I am text")
self.parser_strict.parse("\"I'm text", "I'm text")
self.parser_strict.parse('"I\\"m text', "I\\m text")
# Boolean Tests
def test_boolean(self):
self.assertEqual(self.parser_strict.parse("true"), True)
self.assertEqual(self.parser_strict.parse("false"), False)
# Array Tests
def test_empty_array(self):
self.assertEqual(self.parser_strict.parse("[]"), [])
def test_number_array(self):
self.assertEqual(self.parser_strict.parse("[1,2,3]"), [1, 2, 3])
def test_incomplete_array(self):
self.assertEqual(self.parser_strict.parse("[1,2,3"), [1, 2, 3])
self.assertEqual(self.parser_strict.parse("[1,2,"), [1, 2])
self.assertEqual(self.parser_strict.parse("[1,2"), [1, 2])
self.assertEqual(self.parser_strict.parse("[1,"), [1])
self.assertEqual(self.parser_strict.parse("[1"), [1])
self.assertEqual(self.parser_strict.parse("["), [])
# Object Tests
def test_simple_object(self):
o = {"a": "apple", "b": "banana"}
self.assertEqual(self.parser_strict.parse('{"a":"apple","b":"banana"}'), o)
self.assertEqual(self.parser_strict.parse('{"a": "apple","b": "banana"}'), o)
self.assertEqual(self.parser_strict.parse('{"a" : "apple", "b" : "banana"}'), o)
# Invalid Inputs
def test_invalid_input(self):
with self.assertRaises(Exception):
self.parser_strict.parse(":atom")
# Extra Space
def test_extra_space(self):
self.assertEqual(self.parser_strict.parse(" [1] "), [1])
self.assertEqual(self.parser_strict.parse(" [1 "), [1])
# Unicode Tests
def test_incomplete_unicode_escape(self):
with self.assertRaises(Exception):
self.parser_strict.parse('{"a":"\\', '{"a":""')
self.parser_strict.parse('{"a":"\\u', '{"a":""')
self.parser_strict.parse('{"a":"\\u1', '{"a":""')
self.parser_strict.parse('{"a":"\\u123', '{"a":"ሴ"')
def test_complete_unicode_escape(self):
self.assertEqual(self.parser_strict.parse('{"a":"\\u0041"}').get("a"), "A")
self.assertEqual(self.parser_strict.parse('{"a":"\\u00E9"}').get("a"), "é")
self.assertEqual(self.parser_strict.parse('{"a":"\\u20AC"}').get("a"), "€")
if __name__ == "__main__":
unittest.main()