blob: 3a133e823336bcc6fbb30c59633426671c73c347 [file] [log] [blame]
Barry Warsaw409a4c02002-04-10 21:01:31 +00001# Copyright (C) 2002 Python Software Foundation
2# email package unit tests for (optional) Asian codecs
3
4import unittest
Barry Warsawc9ad32c2002-04-15 22:14:06 +00005import test_support
Barry Warsaw409a4c02002-04-10 21:01:31 +00006
7from email.Charset import Charset
8from email.Header import Header, decode_header
9
10
11# See if we have the Japanese codecs package installed
12try:
13 unicode('foo', 'japanese.iso-2022-jp')
14except LookupError:
Barry Warsawc9ad32c2002-04-15 22:14:06 +000015 raise test_support.TestSkipped, 'Optional Japanese codecs not installed'
Barry Warsaw409a4c02002-04-10 21:01:31 +000016
17
18
19class 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
43def suite():
44 suite = unittest.TestSuite()
45 suite.addTest(unittest.makeSuite(TestEmailAsianCodecs))
46 return suite
47
48
Barry Warsawc9ad32c2002-04-15 22:14:06 +000049def test_main():
50 test_support.run_unittest(TestEmailAsianCodecs)
51
52
Barry Warsaw409a4c02002-04-10 21:01:31 +000053
54if __name__ == '__main__':
55 unittest.main(defaultTest='suite')