Add Python JSON tests (#407)

This commit is contained in:
Gautham 2022-05-14 01:32:15 +05:30 committed by GitHub
parent d25a67f4eb
commit f6df29cc3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 536 additions and 345 deletions

View file

@ -12,6 +12,7 @@ pyjson = support.import_fresh_module('json', blocked=['_json'])
cjson.JSONDecodeError = cjson.decoder.JSONDecodeError = json.JSONDecodeError
# create two base classes that will be used by the other tests
@unittest.skipIf(cjson, '_json is available, no need for pure-python tests')
class PyTest(unittest.TestCase):
json = pyjson
loads = staticmethod(pyjson.loads)

View file

@ -70,11 +70,11 @@ class TestRecursion:
def test_highly_nested_objects_decoding(self):
# test that loading highly-nested objects doesn't segfault when C
# accelerations are used. See #12017
with self.assertRaises(RecursionError):
with self.assertRaises((RecursionError, MemoryError)):
self.loads('{"a":' * 100000 + '1' + '}' * 100000)
with self.assertRaises(RecursionError):
with self.assertRaises((RecursionError, MemoryError)):
self.loads('{"a":' * 100000 + '[1]' + '}' * 100000)
with self.assertRaises(RecursionError):
with self.assertRaises((RecursionError, MemoryError)):
self.loads('[' * 100000 + '1' + ']' * 100000)
def test_highly_nested_objects_encoding(self):
@ -82,9 +82,9 @@ class TestRecursion:
l, d = [], {}
for x in range(100000):
l, d = [l], {'k':d}
with self.assertRaises(RecursionError):
with self.assertRaises((RecursionError, MemoryError)):
self.dumps(l)
with self.assertRaises(RecursionError):
with self.assertRaises((RecursionError, MemoryError)):
self.dumps(d)
def test_endless_recursion(self):
@ -94,7 +94,7 @@ class TestRecursion:
"""If check_circular is False, this will keep adding another list."""
return [o]
with self.assertRaises(RecursionError):
with self.assertRaises((RecursionError, MemoryError)):
EndlessJSONEncoder(check_circular=False).encode(5j)

View file

@ -6,6 +6,8 @@ from subprocess import Popen, PIPE
from test import support
from test.support.script_helper import assert_python_ok
if __name__ == "PYOBJ.COM":
import json.tool
class TestTool(unittest.TestCase):
data = """