Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | #!/usr/bin/env python |
2 | # | |
3 | # test_multibytecodec.py | |
4 | # Unit test for multibytecodec itself | |
5 | # | |
6 | # $CJKCodecs: test_multibytecodec.py,v 1.8 2004/06/19 06:09:55 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): | |
65 | self.assertEqual(''.decode('gb18030'), u'') | |
66 | self.assertEqual(unicode('', 'gb18030'), u'') | |
67 | self.assertEqual(u''.encode('gb18030'), '') | |
68 | ||
69 | def test_str_decode(self): | |
70 | self.assertEqual('abcd'.encode('gb18030'), 'abcd') | |
71 | ||
72 | def test_streamwriter_strwrite(self): | |
73 | s = StringIO.StringIO() | |
74 | wr = codecs.getwriter('gb18030')(s) | |
75 | wr.write('abcd') | |
76 | self.assertEqual(s.getvalue(), 'abcd') | |
77 | ||
78 | def test_main(): | |
79 | suite = unittest.TestSuite() | |
80 | suite.addTest(unittest.makeSuite(Test_StreamWriter)) | |
81 | test_support.run_suite(suite) | |
82 | ||
83 | if __name__ == "__main__": | |
84 | test_main() |