-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_is_json.py
More file actions
160 lines (130 loc) · 5.57 KB
/
Copy pathtest_is_json.py
File metadata and controls
160 lines (130 loc) · 5.57 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
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
import json
from unittest import TestCase
from string_utils import is_json
class IsJsonTestCase(TestCase):
def test_non_string_objects_are_properly_handled(self):
# noinspection PyTypeChecker
self.assertFalse(is_json({'a': 1}))
# noinspection PyTypeChecker
self.assertFalse(is_json(None))
# noinspection PyTypeChecker
self.assertFalse(is_json([1, 2, 3]))
# noinspection PyTypeChecker
self.assertFalse(is_json(500))
# noinspection PyTypeChecker
self.assertFalse(is_json(True))
# noinspection PyTypeChecker
self.assertFalse(is_json({1, 2}))
def test_empty_string_are_invalid(self):
self.assertFalse(is_json(''))
self.assertFalse(is_json(' '))
def test_json_object_can_be_empty(self):
self.assertTrue(is_json('{}'))
def test_json_array_can_be_empty(self):
self.assertTrue(is_json('[]'))
def test_external_spaces_are_ignored(self):
self.assertTrue(is_json('{"foo":"bar"}'))
self.assertTrue(is_json(' { "foo": "bar" } '))
self.assertTrue(is_json('''
{
"foo": "bar"
}
'''))
self.assertTrue(is_json('''
[
1, 2, 3
]
'''))
def test_attributes_quotes_are_mandatory(self):
self.assertFalse(is_json('{foo: 1}'))
def test_quotes_should_be_double_quotes(self):
self.assertFalse(is_json("{'foo': 1}"))
self.assertFalse(is_json("['boo', 'bar']"))
def test_string_values_should_be_wrapped_by_double_quotes(self):
self.assertFalse(is_json('{"foo": hello}'))
self.assertFalse(is_json('{"foo": \'hello\'}'))
self.assertTrue(is_json('{"foo": "hello"}'))
def test_boolean_should_be_lowercase(self):
self.assertFalse(is_json('{"bool": True}'))
self.assertFalse(is_json('{"bool": FALSE}'))
self.assertTrue(is_json('{"bool": true}'))
self.assertIsInstance(json.loads('{"bool": true}'), dict)
self.assertTrue(is_json('{"bool": false}'))
self.assertIsInstance(json.loads('{"bool": false}'), dict)
def test_null_should_be_lowercase(self):
self.assertFalse(is_json('{"null": NULL}'))
self.assertFalse(is_json('{"null": Null}'))
self.assertTrue(is_json('{"null": null}'))
self.assertIsInstance(json.loads('{"null": null}'), dict)
def test_int_number_can_be_any_length(self):
self.assertTrue(is_json('{"number": 1}'))
self.assertTrue(is_json('{"number": 99}'))
self.assertTrue(is_json('{"number": 1000}'))
self.assertTrue(is_json('{"number": 1234567890}'))
def test_float_numbers_should_use_dot_as_separator(self):
self.assertFalse(is_json('{"float": 4,5}'))
self.assertTrue(is_json('{"float": 4.5}'))
self.assertIsInstance(json.loads('{"float": 4.5}'), dict)
def test_negative_numbers_should_be_start_with_minus(self):
self.assertFalse(is_json('{"number": - 2}'))
self.assertFalse(is_json('{"number": - 2.5}'))
self.assertTrue(is_json('{"number": -2}'))
self.assertTrue(is_json('{"number": -2.5}'))
def test_array_can_be_empty(self):
self.assertTrue(is_json('{"array": []}'))
self.assertTrue(is_json('{"array": [ ]}'))
def test_object_can_be_empty(self):
self.assertTrue(is_json('{"obj": {}}'))
self.assertTrue(is_json('{"obj": { }}'))
def test_cannot_have_trailing_comma_in_array(self):
self.assertFalse(is_json('{"numbers": [1,2,3,]}'))
def test_cannot_have_multiple_comma_in_array(self):
self.assertFalse(is_json('{"numbers": [1,2,,3]}'))
def test_cannot_have_trailing_comma_in_object(self):
self.assertFalse(is_json('{"numbers": {"a": 1, "b": 2,}}'))
def test_cannot_have_multiple_comma_in_object(self):
self.assertFalse(is_json('{"numbers": {"a": 1,, "b": 2}}'))
def test_string_can_contain_escaped_quotes(self):
s = '{"string": "Look: \\"escaped string here!\\""}'
self.assertTrue(is_json(s))
self.assertIsInstance(json.loads(s), dict)
def test_array_is_json(self):
self.assertTrue(is_json('[1,2,3]'))
self.assertTrue(is_json('[]'))
self.assertTrue(is_json('["foo", "bar"]'))
self.assertTrue(is_json('[true]'))
self.assertTrue(is_json('[false]'))
self.assertTrue(is_json('[{"a": "b"}]'))
def test_complete_json_case(self):
string = '''
{
"books": [
{
"title": "Book title 1",
"author": "FirstName LastName",
"tags": ["tech", "programming", "python"],
"available": true,
"pageCount": 516,
"rating": 4.5,
"comments": [
{
"author": "FirstName LastName",
"content": "Nice book!"
}
]
},
{
"title": "Book title 2",
"author": "FirstName LastName",
"tags": ["tech", "programming", "javascript"],
"available": true,
"rating": 4,
"pageCount": 422,
"comments": [
]
}
]
}
'''
self.assertTrue(is_json(string))
self.assertIsInstance(json.loads(string), dict)