home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-python-addon-1.4.9-installer.exe / test_email_codecs.py < prev    next >
Encoding:
Python Source  |  2003-03-06  |  2.4 KB  |  67 lines

  1. # Copyright (C) 2002 Python Software Foundation
  2. # email package unit tests for (optional) Asian codecs
  3.  
  4. import unittest
  5. from test.test_support import TestSkipped, run_unittest
  6.  
  7. from email.test.test_email import TestEmailBase
  8. from email.Charset import Charset
  9. from email.Header import Header, decode_header
  10.  
  11. # See if we have the Japanese codecs package installed
  12. try:
  13.     unicode('foo', 'japanese.iso-2022-jp')
  14. except LookupError:
  15.     raise TestSkipped, 'Optional Japanese codecs not installed'
  16.  
  17.  
  18.  
  19. class TestEmailAsianCodecs(TestEmailBase):
  20.     def test_japanese_codecs(self):
  21.         eq = self.ndiffAssertEqual
  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.         # BAW: This used to -- and maybe should -- fold the two iso-8859-1
  30.         # chunks into a single encoded word.  However it doesn't violate the
  31.         # standard to have them as two encoded chunks and maybe it's
  32.         # reasonable <wink> for each .append() call to result in a separate
  33.         # encoded word.
  34.         eq(h.encode(), """\
  35. Hello World! =?iso-2022-jp?b?GyRCJU8lbSE8JW8hPCVrJUkhKhsoQg==?=
  36.  =?iso-8859-1?q?Gr=FC=DF?= =?iso-8859-1?q?_Gott!?=""")
  37.         eq(decode_header(h.encode()),
  38.            [('Hello World!', None),
  39.             ('\x1b$B%O%m!<%o!<%k%I!*\x1b(B', 'iso-2022-jp'),
  40.             ('Gr\xfc\xdf Gott!', 'iso-8859-1')])
  41.         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'
  42.         h = Header(long, j, header_name="Subject")
  43.         # test a very long header
  44.         enc = h.encode()
  45.         # TK: splitting point may differ by codec design and/or Header encoding
  46.         eq(enc , """\
  47. =?iso-2022-jp?b?dGVzdC1qYSAbJEIkWEVqOUYkNSRsJD8lYSE8JWskTztKGyhC?=
  48.  =?iso-2022-jp?b?GyRCMnE8VCROPjVHJyRyQlQkQyRGJCQkXiQ5GyhC?=""")
  49.         # TK: full decode comparison
  50.         eq(h.__unicode__().encode('euc-jp'), long)
  51.  
  52.  
  53.  
  54. def suite():
  55.     suite = unittest.TestSuite()
  56.     suite.addTest(unittest.makeSuite(TestEmailAsianCodecs))
  57.     return suite
  58.  
  59.  
  60. def test_main():
  61.     run_unittest(TestEmailAsianCodecs)
  62.  
  63.  
  64.  
  65. if __name__ == '__main__':
  66.     unittest.main(defaultTest='suite')
  67.