]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.10/Lib/json/tests/test_unicode.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Lib / json / tests / test_unicode.py
CommitLineData
3257aa99
DM
1from collections import OrderedDict\r
2from json.tests import PyTest, CTest\r
3\r
4\r
5class TestUnicode(object):\r
6 def test_encoding1(self):\r
7 encoder = self.json.JSONEncoder(encoding='utf-8')\r
8 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'\r
9 s = u.encode('utf-8')\r
10 ju = encoder.encode(u)\r
11 js = encoder.encode(s)\r
12 self.assertEqual(ju, js)\r
13\r
14 def test_encoding2(self):\r
15 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'\r
16 s = u.encode('utf-8')\r
17 ju = self.dumps(u, encoding='utf-8')\r
18 js = self.dumps(s, encoding='utf-8')\r
19 self.assertEqual(ju, js)\r
20\r
21 def test_encoding3(self):\r
22 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'\r
23 j = self.dumps(u)\r
24 self.assertEqual(j, '"\\u03b1\\u03a9"')\r
25\r
26 def test_encoding4(self):\r
27 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'\r
28 j = self.dumps([u])\r
29 self.assertEqual(j, '["\\u03b1\\u03a9"]')\r
30\r
31 def test_encoding5(self):\r
32 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'\r
33 j = self.dumps(u, ensure_ascii=False)\r
34 self.assertEqual(j, u'"{0}"'.format(u))\r
35\r
36 def test_encoding6(self):\r
37 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'\r
38 j = self.dumps([u], ensure_ascii=False)\r
39 self.assertEqual(j, u'["{0}"]'.format(u))\r
40\r
41 def test_big_unicode_encode(self):\r
42 u = u'\U0001d120'\r
43 self.assertEqual(self.dumps(u), '"\\ud834\\udd20"')\r
44 self.assertEqual(self.dumps(u, ensure_ascii=False), u'"\U0001d120"')\r
45\r
46 def test_big_unicode_decode(self):\r
47 u = u'z\U0001d120x'\r
48 self.assertEqual(self.loads('"' + u + '"'), u)\r
49 self.assertEqual(self.loads('"z\\ud834\\udd20x"'), u)\r
50\r
51 def test_unicode_decode(self):\r
52 for i in range(0, 0xd7ff):\r
53 u = unichr(i)\r
54 s = '"\\u{0:04x}"'.format(i)\r
55 self.assertEqual(self.loads(s), u)\r
56\r
57 def test_object_pairs_hook_with_unicode(self):\r
58 s = u'{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'\r
59 p = [(u"xkd", 1), (u"kcw", 2), (u"art", 3), (u"hxm", 4),\r
60 (u"qrt", 5), (u"pad", 6), (u"hoy", 7)]\r
61 self.assertEqual(self.loads(s), eval(s))\r
62 self.assertEqual(self.loads(s, object_pairs_hook = lambda x: x), p)\r
63 od = self.loads(s, object_pairs_hook = OrderedDict)\r
64 self.assertEqual(od, OrderedDict(p))\r
65 self.assertEqual(type(od), OrderedDict)\r
66 # the object_pairs_hook takes priority over the object_hook\r
67 self.assertEqual(self.loads(s,\r
68 object_pairs_hook = OrderedDict,\r
69 object_hook = lambda x: None),\r
70 OrderedDict(p))\r
71\r
72 def test_default_encoding(self):\r
73 self.assertEqual(self.loads(u'{"a": "\xe9"}'.encode('utf-8')),\r
74 {'a': u'\xe9'})\r
75\r
76 def test_unicode_preservation(self):\r
77 self.assertEqual(type(self.loads(u'""')), unicode)\r
78 self.assertEqual(type(self.loads(u'"a"')), unicode)\r
79 self.assertEqual(type(self.loads(u'["a"]')[0]), unicode)\r
80 # Issue 10038.\r
81 self.assertEqual(type(self.loads('"foo"')), unicode)\r
82\r
83 def test_bad_encoding(self):\r
84 self.assertRaises(UnicodeEncodeError, self.loads, '"a"', u"rat\xe9")\r
85 self.assertRaises(TypeError, self.loads, '"a"', 1)\r
86\r
87\r
88class TestPyUnicode(TestUnicode, PyTest): pass\r
89class TestCUnicode(TestUnicode, CTest): pass\r