ยปCore Development>Code coverage>Lib/test/test_json/test_decode.py

Python code coverage for Lib/test/test_json/test_decode.py

#countcontent
1n/aimport decimal
2n/afrom io import StringIO, BytesIO
3n/afrom collections import OrderedDict
4n/afrom test.test_json import PyTest, CTest
5n/a
6n/a
7n/aclass TestDecode:
8n/a def test_decimal(self):
9n/a rval = self.loads('1.1', parse_float=decimal.Decimal)
10n/a self.assertTrue(isinstance(rval, decimal.Decimal))
11n/a self.assertEqual(rval, decimal.Decimal('1.1'))
12n/a
13n/a def test_float(self):
14n/a rval = self.loads('1', parse_int=float)
15n/a self.assertTrue(isinstance(rval, float))
16n/a self.assertEqual(rval, 1.0)
17n/a
18n/a def test_empty_objects(self):
19n/a self.assertEqual(self.loads('{}'), {})
20n/a self.assertEqual(self.loads('[]'), [])
21n/a self.assertEqual(self.loads('""'), "")
22n/a
23n/a def test_object_pairs_hook(self):
24n/a s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
25n/a p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
26n/a ("qrt", 5), ("pad", 6), ("hoy", 7)]
27n/a self.assertEqual(self.loads(s), eval(s))
28n/a self.assertEqual(self.loads(s, object_pairs_hook=lambda x: x), p)
29n/a self.assertEqual(self.json.load(StringIO(s),
30n/a object_pairs_hook=lambda x: x), p)
31n/a od = self.loads(s, object_pairs_hook=OrderedDict)
32n/a self.assertEqual(od, OrderedDict(p))
33n/a self.assertEqual(type(od), OrderedDict)
34n/a # the object_pairs_hook takes priority over the object_hook
35n/a self.assertEqual(self.loads(s, object_pairs_hook=OrderedDict,
36n/a object_hook=lambda x: None),
37n/a OrderedDict(p))
38n/a # check that empty object literals work (see #17368)
39n/a self.assertEqual(self.loads('{}', object_pairs_hook=OrderedDict),
40n/a OrderedDict())
41n/a self.assertEqual(self.loads('{"empty": {}}',
42n/a object_pairs_hook=OrderedDict),
43n/a OrderedDict([('empty', OrderedDict())]))
44n/a
45n/a def test_decoder_optimizations(self):
46n/a # Several optimizations were made that skip over calls to
47n/a # the whitespace regex, so this test is designed to try and
48n/a # exercise the uncommon cases. The array cases are already covered.
49n/a rval = self.loads('{ "key" : "value" , "k":"v" }')
50n/a self.assertEqual(rval, {"key":"value", "k":"v"})
51n/a
52n/a def check_keys_reuse(self, source, loads):
53n/a rval = loads(source)
54n/a (a, b), (c, d) = sorted(rval[0]), sorted(rval[1])
55n/a self.assertIs(a, c)
56n/a self.assertIs(b, d)
57n/a
58n/a def test_keys_reuse(self):
59n/a s = '[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]'
60n/a self.check_keys_reuse(s, self.loads)
61n/a self.check_keys_reuse(s, self.json.decoder.JSONDecoder().decode)
62n/a
63n/a def test_extra_data(self):
64n/a s = '[1, 2, 3]5'
65n/a msg = 'Extra data'
66n/a self.assertRaisesRegex(self.JSONDecodeError, msg, self.loads, s)
67n/a
68n/a def test_invalid_escape(self):
69n/a s = '["abc\\y"]'
70n/a msg = 'escape'
71n/a self.assertRaisesRegex(self.JSONDecodeError, msg, self.loads, s)
72n/a
73n/a def test_invalid_input_type(self):
74n/a msg = 'the JSON object must be str'
75n/a for value in [1, 3.14, [], {}, None]:
76n/a self.assertRaisesRegex(TypeError, msg, self.loads, value)
77n/a
78n/a def test_string_with_utf8_bom(self):
79n/a # see #18958
80n/a bom_json = "[1,2,3]".encode('utf-8-sig').decode('utf-8')
81n/a with self.assertRaises(self.JSONDecodeError) as cm:
82n/a self.loads(bom_json)
83n/a self.assertIn('BOM', str(cm.exception))
84n/a with self.assertRaises(self.JSONDecodeError) as cm:
85n/a self.json.load(StringIO(bom_json))
86n/a self.assertIn('BOM', str(cm.exception))
87n/a # make sure that the BOM is not detected in the middle of a string
88n/a bom_in_str = '"{}"'.format(''.encode('utf-8-sig').decode('utf-8'))
89n/a self.assertEqual(self.loads(bom_in_str), '\ufeff')
90n/a self.assertEqual(self.json.load(StringIO(bom_in_str)), '\ufeff')
91n/a
92n/a def test_negative_index(self):
93n/a d = self.json.JSONDecoder()
94n/a self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000)
95n/a
96n/aclass TestPyDecode(TestDecode, PyTest): pass
97n/aclass TestCDecode(TestDecode, CTest): pass