| 
									
										
										
										
											2004-01-17 14:29:29 +00:00
										 |  |  | #!/usr/bin/env python | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # test_multibytecodec.py | 
					
						
							|  |  |  | #   Unit test for multibytecodec itself | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | # $CJKCodecs: test_multibytecodec.py,v 1.8 2004/06/19 06:09:55 perky Exp $ | 
					
						
							| 
									
										
										
										
											2004-01-17 14:29:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | from test import test_support | 
					
						
							|  |  |  | from test import test_multibytecodec_support | 
					
						
							|  |  |  | import unittest, StringIO, codecs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Test_StreamWriter(unittest.TestCase): | 
					
						
							|  |  |  |     if len(u'\U00012345') == 2: # UCS2 | 
					
						
							|  |  |  |         def test_gb18030(self): | 
					
						
							|  |  |  |             s= StringIO.StringIO() | 
					
						
							|  |  |  |             c = codecs.lookup('gb18030')[3](s) | 
					
						
							|  |  |  |             c.write(u'123') | 
					
						
							|  |  |  |             self.assertEqual(s.getvalue(), '123') | 
					
						
							|  |  |  |             c.write(u'\U00012345') | 
					
						
							|  |  |  |             self.assertEqual(s.getvalue(), '123\x907\x959') | 
					
						
							|  |  |  |             c.write(u'\U00012345'[0]) | 
					
						
							|  |  |  |             self.assertEqual(s.getvalue(), '123\x907\x959') | 
					
						
							|  |  |  |             c.write(u'\U00012345'[1] + u'\U00012345' + u'\uac00\u00ac') | 
					
						
							|  |  |  |             self.assertEqual(s.getvalue(), | 
					
						
							|  |  |  |                     '123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851') | 
					
						
							|  |  |  |             c.write(u'\U00012345'[0]) | 
					
						
							|  |  |  |             self.assertEqual(s.getvalue(), | 
					
						
							|  |  |  |                     '123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851') | 
					
						
							|  |  |  |             self.assertRaises(UnicodeError, c.reset) | 
					
						
							|  |  |  |             self.assertEqual(s.getvalue(), | 
					
						
							|  |  |  |                     '123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # standard utf-8 codecs has broken StreamReader | 
					
						
							|  |  |  |         if test_multibytecodec_support.__cjkcodecs__: | 
					
						
							|  |  |  |             def test_utf_8(self): | 
					
						
							|  |  |  |                 s= StringIO.StringIO() | 
					
						
							|  |  |  |                 c = codecs.lookup('utf-8')[3](s) | 
					
						
							|  |  |  |                 c.write(u'123') | 
					
						
							|  |  |  |                 self.assertEqual(s.getvalue(), '123') | 
					
						
							|  |  |  |                 c.write(u'\U00012345') | 
					
						
							|  |  |  |                 self.assertEqual(s.getvalue(), '123\xf0\x92\x8d\x85') | 
					
						
							|  |  |  |                 c.write(u'\U00012345'[0]) | 
					
						
							|  |  |  |                 self.assertEqual(s.getvalue(), '123\xf0\x92\x8d\x85') | 
					
						
							|  |  |  |                 c.write(u'\U00012345'[1] + u'\U00012345' + u'\uac00\u00ac') | 
					
						
							|  |  |  |                 self.assertEqual(s.getvalue(), | 
					
						
							|  |  |  |                     '123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85' | 
					
						
							|  |  |  |                     '\xea\xb0\x80\xc2\xac') | 
					
						
							|  |  |  |                 c.write(u'\U00012345'[0]) | 
					
						
							|  |  |  |                 self.assertEqual(s.getvalue(), | 
					
						
							|  |  |  |                     '123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85' | 
					
						
							|  |  |  |                     '\xea\xb0\x80\xc2\xac') | 
					
						
							|  |  |  |                 c.reset() | 
					
						
							|  |  |  |                 self.assertEqual(s.getvalue(), | 
					
						
							|  |  |  |                     '123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85' | 
					
						
							|  |  |  |                     '\xea\xb0\x80\xc2\xac\xed\xa0\x88') | 
					
						
							|  |  |  |                 c.write(u'\U00012345'[1]) | 
					
						
							|  |  |  |                 self.assertEqual(s.getvalue(), | 
					
						
							|  |  |  |                     '123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85' | 
					
						
							|  |  |  |                     '\xea\xb0\x80\xc2\xac\xed\xa0\x88\xed\xbd\x85') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     else: # UCS4 | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_nullcoding(self): | 
					
						
							| 
									
										
										
										
											2004-01-20 09:11:48 +00:00
										 |  |  |         self.assertEqual(''.decode('gb18030'), u'') | 
					
						
							|  |  |  |         self.assertEqual(unicode('', 'gb18030'), u'') | 
					
						
							|  |  |  |         self.assertEqual(u''.encode('gb18030'), '') | 
					
						
							| 
									
										
										
										
											2004-01-17 14:29:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_str_decode(self): | 
					
						
							| 
									
										
										
										
											2004-01-20 09:11:48 +00:00
										 |  |  |         self.assertEqual('abcd'.encode('gb18030'), 'abcd') | 
					
						
							| 
									
										
										
										
											2004-01-17 14:29:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  |     def test_streamwriter_strwrite(self): | 
					
						
							|  |  |  |         s = StringIO.StringIO() | 
					
						
							|  |  |  |         wr = codecs.getwriter('gb18030')(s) | 
					
						
							|  |  |  |         wr.write('abcd') | 
					
						
							|  |  |  |         self.assertEqual(s.getvalue(), 'abcd') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-13 10:20:08 +00:00
										 |  |  | class Test_ISO2022(unittest.TestCase): | 
					
						
							|  |  |  |     def test_g2(self): | 
					
						
							|  |  |  |         iso2022jp2 = '\x1b(B:hu4:unit\x1b.A\x1bNi de famille' | 
					
						
							|  |  |  |         uni = u':hu4:unit\xe9 de famille' | 
					
						
							|  |  |  |         self.assertEqual(iso2022jp2.decode('iso2022-jp-2'), uni) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-01-17 14:29:29 +00:00
										 |  |  | def test_main(): | 
					
						
							|  |  |  |     suite = unittest.TestSuite() | 
					
						
							|  |  |  |     suite.addTest(unittest.makeSuite(Test_StreamWriter)) | 
					
						
							| 
									
										
										
										
											2006-03-13 10:20:08 +00:00
										 |  |  |     suite.addTest(unittest.makeSuite(Test_ISO2022)) | 
					
						
							| 
									
										
										
										
											2004-01-17 14:29:29 +00:00
										 |  |  |     test_support.run_suite(suite) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     test_main() |