Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # test_multibytecodec.py |
| 4 | # Unit test for multibytecodec itself |
| 5 | # |
| 6 | # $CJKCodecs: test_multibytecodec.py,v 1.5 2004/01/06 02:26:28 perky Exp $ |
| 7 | |
| 8 | from test import test_support |
| 9 | from test import test_multibytecodec_support |
| 10 | import unittest, StringIO, codecs |
| 11 | |
| 12 | class Test_StreamWriter(unittest.TestCase): |
| 13 | if len(u'\U00012345') == 2: # UCS2 |
| 14 | def test_gb18030(self): |
| 15 | s= StringIO.StringIO() |
| 16 | c = codecs.lookup('gb18030')[3](s) |
| 17 | c.write(u'123') |
| 18 | self.assertEqual(s.getvalue(), '123') |
| 19 | c.write(u'\U00012345') |
| 20 | self.assertEqual(s.getvalue(), '123\x907\x959') |
| 21 | c.write(u'\U00012345'[0]) |
| 22 | self.assertEqual(s.getvalue(), '123\x907\x959') |
| 23 | c.write(u'\U00012345'[1] + u'\U00012345' + u'\uac00\u00ac') |
| 24 | self.assertEqual(s.getvalue(), |
| 25 | '123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851') |
| 26 | c.write(u'\U00012345'[0]) |
| 27 | self.assertEqual(s.getvalue(), |
| 28 | '123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851') |
| 29 | self.assertRaises(UnicodeError, c.reset) |
| 30 | self.assertEqual(s.getvalue(), |
| 31 | '123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851') |
| 32 | |
| 33 | # standard utf-8 codecs has broken StreamReader |
| 34 | if test_multibytecodec_support.__cjkcodecs__: |
| 35 | def test_utf_8(self): |
| 36 | s= StringIO.StringIO() |
| 37 | c = codecs.lookup('utf-8')[3](s) |
| 38 | c.write(u'123') |
| 39 | self.assertEqual(s.getvalue(), '123') |
| 40 | c.write(u'\U00012345') |
| 41 | self.assertEqual(s.getvalue(), '123\xf0\x92\x8d\x85') |
| 42 | c.write(u'\U00012345'[0]) |
| 43 | self.assertEqual(s.getvalue(), '123\xf0\x92\x8d\x85') |
| 44 | c.write(u'\U00012345'[1] + u'\U00012345' + u'\uac00\u00ac') |
| 45 | self.assertEqual(s.getvalue(), |
| 46 | '123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85' |
| 47 | '\xea\xb0\x80\xc2\xac') |
| 48 | c.write(u'\U00012345'[0]) |
| 49 | self.assertEqual(s.getvalue(), |
| 50 | '123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85' |
| 51 | '\xea\xb0\x80\xc2\xac') |
| 52 | c.reset() |
| 53 | self.assertEqual(s.getvalue(), |
| 54 | '123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85' |
| 55 | '\xea\xb0\x80\xc2\xac\xed\xa0\x88') |
| 56 | c.write(u'\U00012345'[1]) |
| 57 | self.assertEqual(s.getvalue(), |
| 58 | '123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85' |
| 59 | '\xea\xb0\x80\xc2\xac\xed\xa0\x88\xed\xbd\x85') |
| 60 | |
| 61 | else: # UCS4 |
| 62 | pass |
| 63 | |
| 64 | def test_nullcoding(self): |
Hye-Shik Chang | a5e719e | 2004-01-20 09:11:48 +0000 | [diff] [blame] | 65 | self.assertEqual(''.decode('gb18030'), u'') |
| 66 | self.assertEqual(unicode('', 'gb18030'), u'') |
| 67 | self.assertEqual(u''.encode('gb18030'), '') |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 68 | |
| 69 | def test_str_decode(self): |
Hye-Shik Chang | a5e719e | 2004-01-20 09:11:48 +0000 | [diff] [blame] | 70 | self.assertEqual('abcd'.encode('gb18030'), 'abcd') |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 71 | |
| 72 | def test_main(): |
| 73 | suite = unittest.TestSuite() |
| 74 | suite.addTest(unittest.makeSuite(Test_StreamWriter)) |
| 75 | test_support.run_suite(suite) |
| 76 | |
| 77 | if __name__ == "__main__": |
| 78 | test_main() |