PageRenderTime 133ms CodeModel.GetById 28ms app.highlight 33ms RepoModel.GetById 17ms app.codeStats 0ms

/Lib/json/tests/test_unicode.py

http://unladen-swallow.googlecode.com/
Python | 55 lines | 45 code | 10 blank | 0 comment | 2 complexity | 8bb53bbe6c9bb71f38c28b3efbd78f06 MD5 | raw file
 1from unittest import TestCase
 2
 3import json
 4
 5class TestUnicode(TestCase):
 6    def test_encoding1(self):
 7        encoder = json.JSONEncoder(encoding='utf-8')
 8        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
 9        s = u.encode('utf-8')
10        ju = encoder.encode(u)
11        js = encoder.encode(s)
12        self.assertEquals(ju, js)
13
14    def test_encoding2(self):
15        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
16        s = u.encode('utf-8')
17        ju = json.dumps(u, encoding='utf-8')
18        js = json.dumps(s, encoding='utf-8')
19        self.assertEquals(ju, js)
20
21    def test_encoding3(self):
22        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
23        j = json.dumps(u)
24        self.assertEquals(j, '"\\u03b1\\u03a9"')
25
26    def test_encoding4(self):
27        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
28        j = json.dumps([u])
29        self.assertEquals(j, '["\\u03b1\\u03a9"]')
30
31    def test_encoding5(self):
32        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
33        j = json.dumps(u, ensure_ascii=False)
34        self.assertEquals(j, u'"{0}"'.format(u))
35
36    def test_encoding6(self):
37        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
38        j = json.dumps([u], ensure_ascii=False)
39        self.assertEquals(j, u'["{0}"]'.format(u))
40
41    def test_big_unicode_encode(self):
42        u = u'\U0001d120'
43        self.assertEquals(json.dumps(u), '"\\ud834\\udd20"')
44        self.assertEquals(json.dumps(u, ensure_ascii=False), u'"\U0001d120"')
45
46    def test_big_unicode_decode(self):
47        u = u'z\U0001d120x'
48        self.assertEquals(json.loads('"' + u + '"'), u)
49        self.assertEquals(json.loads('"z\\ud834\\udd20x"'), u)
50
51    def test_unicode_decode(self):
52        for i in range(0, 0xd7ff):
53            u = unichr(i)
54            js = '"\\u{0:04x}"'.format(i)
55            self.assertEquals(json.loads(js), u)