home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 May / maximum-cd-2010-05.iso / DiscContents / boxee-0.9.20.10711.exe / system / python / local / simplejson / tests / test_unicode.py < prev   
Encoding:
Python Source  |  2009-07-20  |  2.3 KB  |  64 lines

  1. from unittest import TestCase
  2.  
  3. import simplejson as json
  4.  
  5. class 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'"%s"' % (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'["%s"]' % (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.             s = '"\\u%04x"' % (i,)
  55.             self.assertEquals(json.loads(s), u)
  56.  
  57.     def test_default_encoding(self):
  58.         self.assertEquals(json.loads(u'{"a": "\xe9"}'.encode('utf-8')),
  59.             {'a': u'\xe9'})
  60.  
  61.     def test_unicode_preservation(self):
  62.         self.assertEquals(type(json.loads(u'""')), unicode)
  63.         self.assertEquals(type(json.loads(u'"a"')), unicode)
  64.         self.assertEquals(type(json.loads(u'["a"]')[0]), unicode)