Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1 | # Copyright (C) 2002 Python Software Foundation |
| 2 | # email package unit tests for (optional) Asian codecs |
| 3 | |
| 4 | import unittest |
Barry Warsaw | c9ad32c | 2002-04-15 22:14:06 +0000 | [diff] [blame] | 5 | import test_support |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 6 | |
| 7 | from email.Charset import Charset |
| 8 | from email.Header import Header, decode_header |
| 9 | |
| 10 | |
| 11 | # See if we have the Japanese codecs package installed |
| 12 | try: |
| 13 | unicode('foo', 'japanese.iso-2022-jp') |
| 14 | except LookupError: |
Barry Warsaw | c9ad32c | 2002-04-15 22:14:06 +0000 | [diff] [blame] | 15 | raise test_support.TestSkipped, 'Optional Japanese codecs not installed' |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | |
| 19 | class TestEmailAsianCodecs(unittest.TestCase): |
| 20 | def test_japanese_codecs(self): |
| 21 | eq = self.assertEqual |
| 22 | j = Charset("euc-jp") |
| 23 | g = Charset("iso-8859-1") |
| 24 | h = Header("Hello World!") |
| 25 | jhello = '\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc\xa5\xeb\xa5\xc9\xa1\xaa' |
| 26 | ghello = 'Gr\xfc\xdf Gott!' |
| 27 | h.append(jhello, j) |
| 28 | h.append(ghello, g) |
| 29 | eq(h.encode(), 'Hello World! =?iso-2022-jp?b?GyRCJU8lbSE8JW8hPCVrJUkhKhsoQg==?=\n =?iso-8859-1?q?Gr=FC=DF_Gott!?=') |
| 30 | eq(decode_header(h.encode()), |
| 31 | [('Hello World!', None), |
| 32 | ('\x1b$B%O%m!<%o!<%k%I!*\x1b(B', 'iso-2022-jp'), |
| 33 | ('Gr\xfc\xdf Gott!', 'iso-8859-1')]) |
| 34 | long = 'test-ja \xa4\xd8\xc5\xea\xb9\xc6\xa4\xb5\xa4\xec\xa4\xbf\xa5\xe1\xa1\xbc\xa5\xeb\xa4\xcf\xbb\xca\xb2\xf1\xbc\xd4\xa4\xce\xbe\xb5\xc7\xa7\xa4\xf2\xc2\xd4\xa4\xc3\xa4\xc6\xa4\xa4\xa4\xde\xa4\xb9' |
| 35 | h = Header(long, j, header_name="Subject") |
| 36 | # test a very long header |
| 37 | enc = h.encode() |
| 38 | eq(enc, '=?iso-2022-jp?b?dGVzdC1qYSAbJEIkWEVqOUYkNSRsJD8lYRsoQg==?=\n =?iso-2022-jp?b?GyRCITwlayRPO0oycTxUJE4+NRsoQg==?=\n =?iso-2022-jp?b?GyRCRyckckJUJEMkRiQkJF4kORsoQg==?=') |
| 39 | eq(decode_header(enc), [("test-ja \x1b$B$XEj9F$5$l$?%a\x1b(B\x1b$B!<%k$O;J2q<T$N>5\x1b(B\x1b$BG'$rBT$C$F$$$^$9\x1b(B", 'iso-2022-jp')]) |
| 40 | |
| 41 | |
| 42 | |
| 43 | def suite(): |
| 44 | suite = unittest.TestSuite() |
| 45 | suite.addTest(unittest.makeSuite(TestEmailAsianCodecs)) |
| 46 | return suite |
| 47 | |
| 48 | |
Barry Warsaw | c9ad32c | 2002-04-15 22:14:06 +0000 | [diff] [blame] | 49 | def test_main(): |
| 50 | test_support.run_unittest(TestEmailAsianCodecs) |
| 51 | |
| 52 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 53 | |
| 54 | if __name__ == '__main__': |
| 55 | unittest.main(defaultTest='suite') |