forked from AGProjects/python-cjson
-
Notifications
You must be signed in to change notification settings - Fork 2
/
jsontest.py
337 lines (260 loc) · 11.2 KB
/
jsontest.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
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
#!/usr/bin/python
## this test suite is an almost verbatim copy of the jsontest.py test suite
## found in json-py available from http://sourceforge.net/projects/json-py/
##
## Copyright (C) 2005 Patrick D. Logan
## Contact mailto:patrickdlogan@stardecisions.com
##
## This library is free software; you can redistribute it and/or
## modify it under the terms of the GNU Lesser General Public
## License as published by the Free Software Foundation; either
## version 2.1 of the License, or (at your option) any later version.
##
## This library is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public
## License along with this library; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import unittest
import cjson
_exception = cjson.DecodeError
# The object tests should be order-independent. They're not.
# i.e. they should test for existence of keys and values
# with read/write invariance.
def _removeWhitespace(str):
return str.replace(" ", "")
class JsonTest(unittest.TestCase):
def testReadEmptyObject(self):
obj = cjson.decode("{}")
self.assertEqual({}, obj)
def testWriteEmptyObject(self):
s = cjson.encode({})
self.assertEqual("{}", _removeWhitespace(s))
def testReadStringValue(self):
obj = cjson.decode('{ "name" : "Patrick" }')
self.assertEqual({ "name" : "Patrick" }, obj)
def testReadEscapedQuotationMark(self):
obj = cjson.decode(r'"\""')
self.assertEqual(r'"', obj)
# def testReadEscapedSolidus(self):
# obj = cjson.decode(r'"\/"')
# self.assertEqual(r'/', obj)
def testReadEscapedReverseSolidus(self):
obj = cjson.decode(r'"\\"')
self.assertEqual("\\", obj)
def testReadEscapedBackspace(self):
obj = cjson.decode(r'"\b"')
self.assertEqual("\b", obj)
def testReadEscapedFormfeed(self):
obj = cjson.decode(r'"\f"')
self.assertEqual("\f", obj)
def testReadEscapedNewline(self):
obj = cjson.decode(r'"\n"')
self.assertEqual("\n", obj)
def testReadEscapedCarriageReturn(self):
obj = cjson.decode(r'"\r"')
self.assertEqual("\r", obj)
def testReadEscapedHorizontalTab(self):
obj = cjson.decode(r'"\t"')
self.assertEqual("\t", obj)
def testReadEscapedHexCharacter(self):
obj = cjson.decode(r'"\u000A"')
self.assertEqual("\n", obj)
obj = cjson.decode(r'"\u1001"')
self.assertEqual(u'\u1001', obj)
def testWriteEscapedQuotationMark(self):
s = cjson.encode(r'"')
self.assertEqual(r'"\""', _removeWhitespace(s))
def testWriteEscapedSolidus(self):
s = cjson.encode(r'/')
#self.assertEqual(r'"\/"', _removeWhitespace(s))
self.assertEqual('"/"', _removeWhitespace(s))
def testWriteNonEscapedSolidus(self):
s = cjson.encode(r'/')
self.assertEqual(r'"/"', _removeWhitespace(s))
def testWriteEscapedReverseSolidus(self):
s = cjson.encode("\\")
self.assertEqual(r'"\\"', _removeWhitespace(s))
def testWriteEscapedBackspace(self):
s = cjson.encode("\b")
self.assertEqual(r'"\b"', _removeWhitespace(s))
def testWriteEscapedFormfeed(self):
s = cjson.encode("\f")
self.assertEqual(r'"\f"', _removeWhitespace(s))
def testWriteEscapedNewline(self):
s = cjson.encode("\n")
self.assertEqual(r'"\n"', _removeWhitespace(s))
def testWriteEscapedCarriageReturn(self):
s = cjson.encode("\r")
self.assertEqual(r'"\r"', _removeWhitespace(s))
def testWriteEscapedHorizontalTab(self):
s = cjson.encode("\t")
self.assertEqual(r'"\t"', _removeWhitespace(s))
def testWriteEscapedHexCharacter(self):
s = cjson.encode(u'\u1001')
self.assertEqual(r'"\u1001"', _removeWhitespace(s))
def testReadBadEscapedHexCharacter(self):
self.assertRaises(_exception, self.doReadBadEscapedHexCharacter)
def doReadBadEscapedHexCharacter(self):
cjson.decode('"\u10K5"')
def testReadBadObjectKey(self):
self.assertRaises(_exception, self.doReadBadObjectKey)
def doReadBadObjectKey(self):
cjson.decode('{ 44 : "age" }')
def testReadBadArray(self):
self.assertRaises(_exception, self.doReadBadArray)
def doReadBadArray(self):
cjson.decode('[1,2,3,,]')
def testReadBadObjectSyntax(self):
self.assertRaises(_exception, self.doReadBadObjectSyntax)
def doReadBadObjectSyntax(self):
cjson.decode('{"age", 44}')
def testWriteStringValue(self):
s = cjson.encode({ "name" : "Patrick" })
self.assertEqual('{"name":"Patrick"}', _removeWhitespace(s))
def testReadIntegerValue(self):
obj = cjson.decode('{ "age" : 44 }')
self.assertEqual({ "age" : 44 }, obj)
def testReadNegativeIntegerValue(self):
obj = cjson.decode('{ "key" : -44 }')
self.assertEqual({ "key" : -44 }, obj)
def testReadFloatValue(self):
obj = cjson.decode('{ "age" : 44.5 }')
self.assertEqual({ "age" : 44.5 }, obj)
def testReadNegativeFloatValue(self):
obj = cjson.decode(' { "key" : -44.5 } ')
self.assertEqual({ "key" : -44.5 }, obj)
def testReadBadNumber(self):
self.assertRaises(_exception, self.doReadBadNumber)
def doReadBadNumber(self):
cjson.decode('-44.4.4')
def testReadSmallObject(self):
obj = cjson.decode('{ "name" : "Patrick", "age":44} ')
self.assertEqual({ "age" : 44, "name" : "Patrick" }, obj)
def testReadEmptyArray(self):
obj = cjson.decode('[]')
self.assertEqual([], obj)
def testWriteEmptyArray(self):
self.assertEqual("[]", _removeWhitespace(cjson.encode([])))
def testReadSmallArray(self):
obj = cjson.decode(' [ "a" , "b", "c" ] ')
self.assertEqual(["a", "b", "c"], obj)
def testWriteSmallArray(self):
self.assertEqual('[1,2,3,4]', _removeWhitespace(cjson.encode([1, 2, 3, 4])))
def testWriteSmallObject(self):
s = cjson.encode({ "name" : "Patrick", "age": 44 })
self.assertEqual('{"age":44,"name":"Patrick"}', _removeWhitespace(s))
def testWriteFloat(self):
n = 3.44556677
self.assertEqual(repr(n), _removeWhitespace(cjson.encode(n)))
def testReadTrue(self):
self.assertEqual(True, cjson.decode("true"))
def testReadFalse(self):
self.assertEqual(False, cjson.decode("false"))
def testReadNull(self):
self.assertEqual(None, cjson.decode("null"))
def testWriteTrue(self):
self.assertEqual("true", _removeWhitespace(cjson.encode(True)))
def testWriteFalse(self):
self.assertEqual("false", _removeWhitespace(cjson.encode(False)))
def testWriteNull(self):
self.assertEqual("null", _removeWhitespace(cjson.encode(None)))
def testReadArrayOfSymbols(self):
self.assertEqual([True, False, None], cjson.decode(" [ true, false,null] "))
def testWriteArrayOfSymbolsFromList(self):
self.assertEqual("[true,false,null]", _removeWhitespace(cjson.encode([True, False, None])))
def testWriteArrayOfSymbolsFromTuple(self):
self.assertEqual("[true,false,null]", _removeWhitespace(cjson.encode((True, False, None))))
def testReadComplexObject(self):
src = '''
{ "name": "Patrick", "age" : 44, "Employed?" : true, "Female?" : false, "grandchildren":null }
'''
obj = cjson.decode(src)
self.assertEqual({"name":"Patrick","age":44,"Employed?":True,"Female?":False,"grandchildren":None}, obj)
def testReadLongArray(self):
src = '''[ "used",
"abused",
"confused",
true, false, null,
1,
2,
[3, 4, 5]]
'''
obj = cjson.decode(src)
self.assertEqual(["used","abused","confused", True, False, None,
1,2,[3,4,5]], obj)
def testReadIncompleteArray(self):
self.assertRaises(_exception, self.doReadIncompleteArray)
def doReadIncompleteArray(self):
cjson.decode('[')
def testReadComplexArray(self):
src = '''
[
{ "name": "Patrick", "age" : 44,
"Employed?" : true, "Female?" : false,
"grandchildren":null },
"used",
"abused",
"confused",
1,
2,
[3, 4, 5]
]
'''
obj = cjson.decode(src)
self.assertEqual([{"name":"Patrick","age":44,"Employed?":True,"Female?":False,"grandchildren":None},
"used","abused","confused",
1,2,[3,4,5]], obj)
def testWriteComplexArray(self):
obj = [{"name":"Patrick","age":44,"Employed?":True,"Female?":False,"grandchildren":None},
"used","abused","confused",
1,2,[3,4,5]]
self.assertEqual('[{"Female?":false,"age":44,"name":"Patrick","grandchildren":null,"Employed?":true},"used","abused","confused",1,2,[3,4,5]]',
_removeWhitespace(cjson.encode(obj)))
def testReadWriteCopies(self):
orig_obj = {'a':' " '}
json_str = cjson.encode(orig_obj)
copy_obj = cjson.decode(json_str)
self.assertEqual(orig_obj, copy_obj)
self.assertEqual(True, orig_obj == copy_obj)
self.assertEqual(False, orig_obj is copy_obj)
def testStringEncoding(self):
s = cjson.encode([1, 2, 3])
self.assertEqual(unicode("[1,2,3]", "utf-8"), _removeWhitespace(s))
def testReadEmptyObjectAtEndOfArray(self):
self.assertEqual(["a","b","c",{}],
cjson.decode('["a","b","c",{}]'))
def testReadEmptyObjectMidArray(self):
self.assertEqual(["a","b",{},"c"],
cjson.decode('["a","b",{},"c"]'))
def testReadClosingObjectBracket(self):
self.assertEqual({"a":[1,2,3]}, cjson.decode('{"a":[1,2,3]}'))
def testEmptyObjectInList(self):
obj = cjson.decode('[{}]')
self.assertEqual([{}], obj)
def testObjectWithEmptyList(self):
obj = cjson.decode('{"test": [] }')
self.assertEqual({"test":[]}, obj)
def testObjectWithNonEmptyList(self):
obj = cjson.decode('{"test": [3, 4, 5] }')
self.assertEqual({"test":[3, 4, 5]}, obj)
def testWriteLong(self):
self.assertEqual("12345678901234567890", cjson.encode(12345678901234567890))
def testWriteLongUnicode(self):
# This test causes a buffer overrun in cjson 1.0.5, on UCS4 builds.
# The string length is only resized for wide unicode characters if
# there is less than 12 bytes of space left. Padding with
# narrow-but-escaped characters prevents string resizing.
# Note that u'\U0001D11E\u1234' also breaks, but sometimes goes
# undetected.
s = cjson.encode(u'\U0001D11E\U0001D11E\U0001D11E\U0001D11E'
u'\u1234\u1234\u1234\u1234\u1234\u1234')
self.assertEqual(r'"\U0001d11e\U0001d11e\U0001d11e\U0001d11e'
r'\u1234\u1234\u1234\u1234\u1234\u1234"', s)
def main():
unittest.main()
if __name__ == '__main__':
main()