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