Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c0d8a1

Browse files
committed
#5723: Improve json tests to be executed with and without accelerations.
1 parent fec3ad1 commit 3c0d8a1

19 files changed

Lines changed: 249 additions & 195 deletions

Lib/json/decoder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
import struct
77

8-
from json.scanner import make_scanner
8+
from json import scanner
99
try:
1010
from _json import scanstring as c_scanstring
1111
except ImportError:
@@ -334,7 +334,7 @@ def __init__(self, object_hook=None, parse_float=None,
334334
self.parse_object = JSONObject
335335
self.parse_array = JSONArray
336336
self.parse_string = scanstring
337-
self.scan_once = make_scanner(self)
337+
self.scan_once = scanner.make_scanner(self)
338338

339339

340340
def decode(self, s, _w=WHITESPACE.match):

Lib/json/tests/__init__.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,46 @@
11
import os
22
import sys
3-
import unittest
3+
import json
44
import doctest
5+
import unittest
6+
7+
from test import support
8+
9+
# import json with and without accelerations
10+
cjson = support.import_fresh_module('json', fresh=['_json'])
11+
pyjson = support.import_fresh_module('json', blocked=['_json'])
12+
13+
# create two base classes that will be used by the other tests
14+
class PyTest(unittest.TestCase):
15+
json = pyjson
16+
loads = staticmethod(pyjson.loads)
17+
dumps = staticmethod(pyjson.dumps)
18+
19+
@unittest.skipUnless(cjson, 'requires _json')
20+
class CTest(unittest.TestCase):
21+
if cjson is not None:
22+
json = cjson
23+
loads = staticmethod(cjson.loads)
24+
dumps = staticmethod(cjson.dumps)
25+
26+
# test PyTest and CTest checking if the functions come from the right module
27+
class TestPyTest(PyTest):
28+
def test_pyjson(self):
29+
self.assertEqual(self.json.scanner.make_scanner.__module__,
30+
'json.scanner')
31+
self.assertEqual(self.json.decoder.scanstring.__module__,
32+
'json.decoder')
33+
self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
34+
'json.encoder')
35+
36+
class TestCTest(CTest):
37+
def test_cjson(self):
38+
self.assertEqual(self.json.scanner.make_scanner.__module__, '_json')
39+
self.assertEqual(self.json.decoder.scanstring.__module__, '_json')
40+
self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json')
41+
self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
42+
'_json')
43+
544

645
here = os.path.dirname(__file__)
746

@@ -17,12 +56,11 @@ def test_suite():
1756
return suite
1857

1958
def additional_tests():
20-
import json
21-
import json.encoder
22-
import json.decoder
2359
suite = unittest.TestSuite()
2460
for mod in (json, json.encoder, json.decoder):
2561
suite.addTest(doctest.DocTestSuite(mod))
62+
suite.addTest(TestPyTest('test_pyjson'))
63+
suite.addTest(TestCTest('test_cjson'))
2664
return suite
2765

2866
def main():

Lib/json/tests/test_decode.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11
import decimal
2-
from unittest import TestCase
32
from io import StringIO
4-
5-
import json
63
from collections import OrderedDict
4+
from json.tests import PyTest, CTest
5+
76

8-
class TestDecode(TestCase):
7+
class TestDecode:
98
def test_decimal(self):
10-
rval = json.loads('1.1', parse_float=decimal.Decimal)
9+
rval = self.loads('1.1', parse_float=decimal.Decimal)
1110
self.assertTrue(isinstance(rval, decimal.Decimal))
1211
self.assertEqual(rval, decimal.Decimal('1.1'))
1312

1413
def test_float(self):
15-
rval = json.loads('1', parse_int=float)
14+
rval = self.loads('1', parse_int=float)
1615
self.assertTrue(isinstance(rval, float))
1716
self.assertEqual(rval, 1.0)
1817

1918
def test_empty_objects(self):
20-
self.assertEqual(json.loads('{}'), {})
21-
self.assertEqual(json.loads('[]'), [])
22-
self.assertEqual(json.loads('""'), "")
19+
self.assertEqual(self.loads('{}'), {})
20+
self.assertEqual(self.loads('[]'), [])
21+
self.assertEqual(self.loads('""'), "")
2322

2423
def test_object_pairs_hook(self):
2524
s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
2625
p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
2726
("qrt", 5), ("pad", 6), ("hoy", 7)]
28-
self.assertEqual(json.loads(s), eval(s))
29-
self.assertEqual(json.loads(s, object_pairs_hook = lambda x: x), p)
30-
self.assertEqual(json.load(StringIO(s),
27+
self.assertEqual(self.loads(s), eval(s))
28+
self.assertEqual(self.loads(s, object_pairs_hook = lambda x: x), p)
29+
self.assertEqual(self.json.load(StringIO(s),
3130
object_pairs_hook=lambda x: x), p)
32-
od = json.loads(s, object_pairs_hook = OrderedDict)
31+
od = self.loads(s, object_pairs_hook = OrderedDict)
3332
self.assertEqual(od, OrderedDict(p))
3433
self.assertEqual(type(od), OrderedDict)
3534
# the object_pairs_hook takes priority over the object_hook
36-
self.assertEqual(json.loads(s,
35+
self.assertEqual(self.loads(s,
3736
object_pairs_hook = OrderedDict,
3837
object_hook = lambda x: None),
3938
OrderedDict(p))
@@ -42,5 +41,9 @@ def test_decoder_optimizations(self):
4241
# Several optimizations were made that skip over calls to
4342
# the whitespace regex, so this test is designed to try and
4443
# exercise the uncommon cases. The array cases are already covered.
45-
rval = json.loads('{ "key" : "value" , "k":"v" }')
44+
rval = self.loads('{ "key" : "value" , "k":"v" }')
4645
self.assertEqual(rval, {"key":"value", "k":"v"})
46+
47+
48+
class TestPyDecode(TestDecode, PyTest): pass
49+
class TestCDecode(TestDecode, CTest): pass

Lib/json/tests/test_default.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
from unittest import TestCase
1+
from json.tests import PyTest, CTest
22

3-
import json
43

5-
class TestDefault(TestCase):
4+
class TestDefault:
65
def test_default(self):
76
self.assertEqual(
8-
json.dumps(type, default=repr),
9-
json.dumps(repr(type)))
7+
self.dumps(type, default=repr),
8+
self.dumps(repr(type)))
9+
10+
11+
class TestPyDefault(TestDefault, PyTest): pass
12+
class TestCDefault(TestDefault, CTest): pass

Lib/json/tests/test_dump.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
from unittest import TestCase
21
from io import StringIO
2+
from json.tests import PyTest, CTest
33

4-
import json
54

6-
class TestDump(TestCase):
5+
class TestDump:
76
def test_dump(self):
87
sio = StringIO()
9-
json.dump({}, sio)
8+
self.json.dump({}, sio)
109
self.assertEqual(sio.getvalue(), '{}')
1110

1211
def test_dumps(self):
13-
self.assertEqual(json.dumps({}), '{}')
12+
self.assertEqual(self.dumps({}), '{}')
1413

1514
def test_encode_truefalse(self):
16-
self.assertEqual(json.dumps(
15+
self.assertEqual(self.dumps(
1716
{True: False, False: True}, sort_keys=True),
1817
'{"false": true, "true": false}')
19-
self.assertEqual(json.dumps(
18+
self.assertEqual(self.dumps(
2019
{2: 3.0, 4.0: 5, False: 1, 6: True}, sort_keys=True),
2120
'{"false": 1, "2": 3.0, "4.0": 5, "6": true}')
21+
22+
23+
class TestPyDump(TestDump, PyTest): pass
24+
class TestCDump(TestDump, CTest): pass
Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from unittest import TestCase
2-
3-
import json.encoder
4-
from json import dumps
51
from collections import OrderedDict
2+
from json.tests import PyTest, CTest
3+
64

75
CASES = [
86
('/\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\x08\x0c\n\r\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?', '"/\\\\\\"\\ucafe\\ubabe\\uab98\\ufcde\\ubcda\\uef4a\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"'),
@@ -21,30 +19,26 @@
2119
('\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'),
2220
]
2321

24-
class TestEncodeBaseStringAscii(TestCase):
25-
def test_py_encode_basestring_ascii(self):
26-
self._test_encode_basestring_ascii(json.encoder.py_encode_basestring_ascii)
27-
28-
def test_c_encode_basestring_ascii(self):
29-
if not json.encoder.c_encode_basestring_ascii:
30-
return
31-
self._test_encode_basestring_ascii(json.encoder.c_encode_basestring_ascii)
32-
33-
def _test_encode_basestring_ascii(self, encode_basestring_ascii):
34-
fname = encode_basestring_ascii.__name__
22+
class TestEncodeBasestringAscii:
23+
def test_encode_basestring_ascii(self):
24+
fname = self.json.encoder.encode_basestring_ascii.__name__
3525
for input_string, expect in CASES:
36-
result = encode_basestring_ascii(input_string)
26+
result = self.json.encoder.encode_basestring_ascii(input_string)
3727
self.assertEqual(result, expect,
3828
'{0!r} != {1!r} for {2}({3!r})'.format(
3929
result, expect, fname, input_string))
4030

4131
def test_ordered_dict(self):
4232
# See issue 6105
4333
items = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
44-
s = json.dumps(OrderedDict(items))
34+
s = self.dumps(OrderedDict(items))
4535
self.assertEqual(s, '{"one": 1, "two": 2, "three": 3, "four": 4, "five": 5}')
4636

4737
def test_sorted_dict(self):
4838
items = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
49-
s = json.dumps(dict(items), sort_keys=True)
39+
s = self.dumps(dict(items), sort_keys=True)
5040
self.assertEqual(s, '{"five": 5, "four": 4, "one": 1, "three": 3, "two": 2}')
41+
42+
43+
class TestPyEncodeBasestringAscii(TestEncodeBasestringAscii, PyTest): pass
44+
class TestCEncodeBasestringAscii(TestEncodeBasestringAscii, CTest): pass

Lib/json/tests/test_fail.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from unittest import TestCase
2-
3-
import json
1+
from json.tests import PyTest, CTest
42

53
# Fri Dec 30 18:57:26 2005
64
JSONDOCS = [
@@ -61,15 +59,15 @@
6159
18: "spec doesn't specify any nesting limitations",
6260
}
6361

64-
class TestFail(TestCase):
62+
class TestFail:
6563
def test_failures(self):
6664
for idx, doc in enumerate(JSONDOCS):
6765
idx = idx + 1
6866
if idx in SKIPS:
69-
json.loads(doc)
67+
self.loads(doc)
7068
continue
7169
try:
72-
json.loads(doc)
70+
self.loads(doc)
7371
except ValueError:
7472
pass
7573
else:
@@ -79,7 +77,11 @@ def test_non_string_keys_dict(self):
7977
data = {'a' : 1, (1, 2) : 2}
8078

8179
#This is for c encoder
82-
self.assertRaises(TypeError, json.dumps, data)
80+
self.assertRaises(TypeError, self.dumps, data)
8381

8482
#This is for python encoder
85-
self.assertRaises(TypeError, json.dumps, data, indent=True)
83+
self.assertRaises(TypeError, self.dumps, data, indent=True)
84+
85+
86+
class TestPyFail(TestFail, PyTest): pass
87+
class TestCFail(TestFail, CTest): pass

Lib/json/tests/test_float.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import math
2-
from unittest import TestCase
2+
from json.tests import PyTest, CTest
33

4-
import json
54

6-
class TestFloat(TestCase):
5+
class TestFloat:
76
def test_floats(self):
87
for num in [1617161771.7650001, math.pi, math.pi**100, math.pi**-100, 3.1]:
9-
self.assertEqual(float(json.dumps(num)), num)
10-
self.assertEqual(json.loads(json.dumps(num)), num)
8+
self.assertEqual(float(self.dumps(num)), num)
9+
self.assertEqual(self.loads(self.dumps(num)), num)
1110

1211
def test_ints(self):
1312
for num in [1, 1<<32, 1<<64]:
14-
self.assertEqual(json.dumps(num), str(num))
15-
self.assertEqual(int(json.dumps(num)), num)
13+
self.assertEqual(self.dumps(num), str(num))
14+
self.assertEqual(int(self.dumps(num)), num)
15+
16+
17+
class TestPyFloat(TestFloat, PyTest): pass
18+
class TestCFloat(TestFloat, CTest): pass

Lib/json/tests/test_indent.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from unittest import TestCase
2-
3-
import json
41
import textwrap
52
from io import StringIO
3+
from json.tests import PyTest, CTest
4+
65

7-
class TestIndent(TestCase):
6+
class TestIndent:
87
def test_indent(self):
98
h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
109
{'nifty': 87}, {'field': 'yes', 'morefield': False} ]
@@ -31,11 +30,11 @@ def test_indent(self):
3130
]""")
3231

3332

34-
d1 = json.dumps(h)
35-
d2 = json.dumps(h, indent=2, sort_keys=True, separators=(',', ': '))
33+
d1 = self.dumps(h)
34+
d2 = self.dumps(h, indent=2, sort_keys=True, separators=(',', ': '))
3635

37-
h1 = json.loads(d1)
38-
h2 = json.loads(d2)
36+
h1 = self.loads(d1)
37+
h2 = self.loads(d2)
3938

4039
self.assertEqual(h1, h)
4140
self.assertEqual(h2, h)
@@ -44,14 +43,18 @@ def test_indent(self):
4443
def test_indent0(self):
4544
h = {3: 1}
4645
def check(indent, expected):
47-
d1 = json.dumps(h, indent=indent)
46+
d1 = self.dumps(h, indent=indent)
4847
self.assertEqual(d1, expected)
4948

5049
sio = StringIO()
51-
json.dump(h, sio, indent=indent)
50+
self.json.dump(h, sio, indent=indent)
5251
self.assertEqual(sio.getvalue(), expected)
5352

5453
# indent=0 should emit newlines
5554
check(0, '{\n"3": 1\n}')
5655
# indent=None is more compact
5756
check(None, '{"3": 1}')
57+
58+
59+
class TestPyIndent(TestIndent, PyTest): pass
60+
class TestCIndent(TestIndent, CTest): pass

Lib/json/tests/test_pass1.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from unittest import TestCase
1+
from json.tests import PyTest, CTest
22

3-
import json
43

54
# from http://json.org/JSON_checker/test/pass1.json
65
JSON = r'''
@@ -62,15 +61,19 @@
6261
,"rosebud"]
6362
'''
6463

65-
class TestPass1(TestCase):
64+
class TestPass1:
6665
def test_parse(self):
6766
# test in/out equivalence and parsing
68-
res = json.loads(JSON)
69-
out = json.dumps(res)
70-
self.assertEqual(res, json.loads(out))
67+
res = self.loads(JSON)
68+
out = self.dumps(res)
69+
self.assertEqual(res, self.loads(out))
7170
try:
72-
json.dumps(res, allow_nan=False)
71+
self.dumps(res, allow_nan=False)
7372
except ValueError:
7473
pass
7574
else:
7675
self.fail("23456789012E666 should be out of range")
76+
77+
78+
class TestPyPass1(TestPass1, PyTest): pass
79+
class TestCPass1(TestPass1, CTest): pass

0 commit comments

Comments
 (0)