diff options
Diffstat (limited to 'Lib/test/json_tests/test_decode.py')
-rw-r--r-- | Lib/test/json_tests/test_decode.py | 55 |
1 files changed, 20 insertions, 35 deletions
diff --git a/Lib/test/json_tests/test_decode.py b/Lib/test/json_tests/test_decode.py index 07dadfe..9fbaa3b 100644 --- a/Lib/test/json_tests/test_decode.py +++ b/Lib/test/json_tests/test_decode.py @@ -1,55 +1,38 @@ import decimal -from unittest import TestCase from io import StringIO -from contextlib import contextmanager - -import json -import json.decoder -import json.scanner from collections import OrderedDict +from test.json_tests import PyTest, CTest -@contextmanager -def use_python_scanner(): - py_scanner = json.scanner.py_make_scanner - old_scanner = json.decoder.make_scanner - json.decoder.make_scanner = py_scanner - try: - yield - finally: - json.decoder.make_scanner = old_scanner - - -class TestDecode(TestCase): +class TestDecode: def test_decimal(self): - rval = json.loads('1.1', parse_float=decimal.Decimal) + rval = self.loads('1.1', parse_float=decimal.Decimal) self.assertTrue(isinstance(rval, decimal.Decimal)) self.assertEqual(rval, decimal.Decimal('1.1')) def test_float(self): - rval = json.loads('1', parse_int=float) + rval = self.loads('1', parse_int=float) self.assertTrue(isinstance(rval, float)) self.assertEqual(rval, 1.0) def test_empty_objects(self): - self.assertEqual(json.loads('{}'), {}) - self.assertEqual(json.loads('[]'), []) - self.assertEqual(json.loads('""'), "") + self.assertEqual(self.loads('{}'), {}) + self.assertEqual(self.loads('[]'), []) + self.assertEqual(self.loads('""'), "") def test_object_pairs_hook(self): s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}' p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4), ("qrt", 5), ("pad", 6), ("hoy", 7)] - self.assertEqual(json.loads(s), eval(s)) - self.assertEqual(json.loads(s, object_pairs_hook = lambda x: x), p) - self.assertEqual(json.load(StringIO(s), - object_pairs_hook=lambda x: x), p) - od = json.loads(s, object_pairs_hook = OrderedDict) + self.assertEqual(self.loads(s), eval(s)) + self.assertEqual(self.loads(s, object_pairs_hook = lambda x: x), p) + self.assertEqual(self.json.load(StringIO(s), + object_pairs_hook=lambda x: x), p) + od = self.loads(s, object_pairs_hook = OrderedDict) self.assertEqual(od, OrderedDict(p)) self.assertEqual(type(od), OrderedDict) # the object_pairs_hook takes priority over the object_hook - self.assertEqual(json.loads(s, - object_pairs_hook = OrderedDict, + self.assertEqual(self.loads(s, object_pairs_hook = OrderedDict, object_hook = lambda x: None), OrderedDict(p)) @@ -57,7 +40,7 @@ class TestDecode(TestCase): # Several optimizations were made that skip over calls to # the whitespace regex, so this test is designed to try and # exercise the uncommon cases. The array cases are already covered. - rval = json.loads('{ "key" : "value" , "k":"v" }') + rval = self.loads('{ "key" : "value" , "k":"v" }') self.assertEqual(rval, {"key":"value", "k":"v"}) def check_keys_reuse(self, source, loads): @@ -68,7 +51,9 @@ class TestDecode(TestCase): def test_keys_reuse(self): s = '[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]' - self.check_keys_reuse(s, json.loads) - # Disabled: the pure Python version of json simply doesn't work - with use_python_scanner(): - self.check_keys_reuse(s, json.decoder.JSONDecoder().decode) + self.check_keys_reuse(s, self.loads) + self.check_keys_reuse(s, self.json.decoder.JSONDecoder().decode) + + +class TestPyDecode(TestDecode, PyTest): pass +class TestCDecode(TestDecode, CTest): pass |