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

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

#countcontent
1n/aimport codecs
2n/afrom collections import OrderedDict
3n/afrom test.test_json import PyTest, CTest
4n/a
5n/a
6n/aclass TestUnicode:
7n/a # test_encoding1 and test_encoding2 from 2.x are irrelevant (only str
8n/a # is supported as input, not bytes).
9n/a
10n/a def test_encoding3(self):
11n/a u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
12n/a j = self.dumps(u)
13n/a self.assertEqual(j, '"\\u03b1\\u03a9"')
14n/a
15n/a def test_encoding4(self):
16n/a u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
17n/a j = self.dumps([u])
18n/a self.assertEqual(j, '["\\u03b1\\u03a9"]')
19n/a
20n/a def test_encoding5(self):
21n/a u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
22n/a j = self.dumps(u, ensure_ascii=False)
23n/a self.assertEqual(j, '"{0}"'.format(u))
24n/a
25n/a def test_encoding6(self):
26n/a u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
27n/a j = self.dumps([u], ensure_ascii=False)
28n/a self.assertEqual(j, '["{0}"]'.format(u))
29n/a
30n/a def test_big_unicode_encode(self):
31n/a u = '\U0001d120'
32n/a self.assertEqual(self.dumps(u), '"\\ud834\\udd20"')
33n/a self.assertEqual(self.dumps(u, ensure_ascii=False), '"\U0001d120"')
34n/a
35n/a def test_big_unicode_decode(self):
36n/a u = 'z\U0001d120x'
37n/a self.assertEqual(self.loads('"' + u + '"'), u)
38n/a self.assertEqual(self.loads('"z\\ud834\\udd20x"'), u)
39n/a
40n/a def test_unicode_decode(self):
41n/a for i in range(0, 0xd7ff):
42n/a u = chr(i)
43n/a s = '"\\u{0:04x}"'.format(i)
44n/a self.assertEqual(self.loads(s), u)
45n/a
46n/a def test_unicode_preservation(self):
47n/a self.assertEqual(type(self.loads('""')), str)
48n/a self.assertEqual(type(self.loads('"a"')), str)
49n/a self.assertEqual(type(self.loads('["a"]')[0]), str)
50n/a
51n/a def test_bytes_encode(self):
52n/a self.assertRaises(TypeError, self.dumps, b"hi")
53n/a self.assertRaises(TypeError, self.dumps, [b"hi"])
54n/a
55n/a def test_bytes_decode(self):
56n/a for encoding, bom in [
57n/a ('utf-8', codecs.BOM_UTF8),
58n/a ('utf-16be', codecs.BOM_UTF16_BE),
59n/a ('utf-16le', codecs.BOM_UTF16_LE),
60n/a ('utf-32be', codecs.BOM_UTF32_BE),
61n/a ('utf-32le', codecs.BOM_UTF32_LE),
62n/a ]:
63n/a data = ["a\xb5\u20ac\U0001d120"]
64n/a encoded = self.dumps(data).encode(encoding)
65n/a self.assertEqual(self.loads(bom + encoded), data)
66n/a self.assertEqual(self.loads(encoded), data)
67n/a self.assertRaises(UnicodeDecodeError, self.loads, b'["\x80"]')
68n/a # RFC-7159 and ECMA-404 extend JSON to allow documents that
69n/a # consist of only a string, which can present a special case
70n/a # not covered by the encoding detection patterns specified in
71n/a # RFC-4627 for utf-16-le (XX 00 XX 00).
72n/a self.assertEqual(self.loads('"\u2600"'.encode('utf-16-le')),
73n/a '\u2600')
74n/a # Encoding detection for small (<4) bytes objects
75n/a # is implemented as a special case. RFC-7159 and ECMA-404
76n/a # allow single codepoint JSON documents which are only two
77n/a # bytes in utf-16 encodings w/o BOM.
78n/a self.assertEqual(self.loads(b'5\x00'), 5)
79n/a self.assertEqual(self.loads(b'\x007'), 7)
80n/a self.assertEqual(self.loads(b'57'), 57)
81n/a
82n/a def test_object_pairs_hook_with_unicode(self):
83n/a s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
84n/a p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
85n/a ("qrt", 5), ("pad", 6), ("hoy", 7)]
86n/a self.assertEqual(self.loads(s), eval(s))
87n/a self.assertEqual(self.loads(s, object_pairs_hook = lambda x: x), p)
88n/a od = self.loads(s, object_pairs_hook = OrderedDict)
89n/a self.assertEqual(od, OrderedDict(p))
90n/a self.assertEqual(type(od), OrderedDict)
91n/a # the object_pairs_hook takes priority over the object_hook
92n/a self.assertEqual(self.loads(s, object_pairs_hook = OrderedDict,
93n/a object_hook = lambda x: None),
94n/a OrderedDict(p))
95n/a
96n/a
97n/aclass TestPyUnicode(TestUnicode, PyTest): pass
98n/aclass TestCUnicode(TestUnicode, CTest): pass