/Lib/email/test/test_email_codecs.py

http://unladen-swallow.googlecode.com/ · Python · 77 lines · 52 code · 12 blank · 13 comment · 3 complexity · 9000d4ac6bf83d26dfad6e4616f923b8 MD5 · raw file

  1. # Copyright (C) 2002-2006 Python Software Foundation
  2. # Contact: email-sig@python.org
  3. # email package unit tests for (optional) Asian codecs
  4. import unittest
  5. from test.test_support import TestSkipped, run_unittest
  6. from email.test.test_email import TestEmailBase
  7. from email.charset import Charset
  8. from email.header import Header, decode_header
  9. from email.message import Message
  10. # We're compatible with Python 2.3, but it doesn't have the built-in Asian
  11. # codecs, so we have to skip all these tests.
  12. try:
  13. unicode('foo', 'euc-jp')
  14. except LookupError:
  15. raise TestSkipped
  16. class TestEmailAsianCodecs(TestEmailBase):
  17. def test_japanese_codecs(self):
  18. eq = self.ndiffAssertEqual
  19. j = Charset("euc-jp")
  20. g = Charset("iso-8859-1")
  21. h = Header("Hello World!")
  22. jhello = '\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc\xa5\xeb\xa5\xc9\xa1\xaa'
  23. ghello = 'Gr\xfc\xdf Gott!'
  24. h.append(jhello, j)
  25. h.append(ghello, g)
  26. # BAW: This used to -- and maybe should -- fold the two iso-8859-1
  27. # chunks into a single encoded word. However it doesn't violate the
  28. # standard to have them as two encoded chunks and maybe it's
  29. # reasonable <wink> for each .append() call to result in a separate
  30. # encoded word.
  31. eq(h.encode(), """\
  32. Hello World! =?iso-2022-jp?b?GyRCJU8lbSE8JW8hPCVrJUkhKhsoQg==?=
  33. =?iso-8859-1?q?Gr=FC=DF?= =?iso-8859-1?q?_Gott!?=""")
  34. eq(decode_header(h.encode()),
  35. [('Hello World!', None),
  36. ('\x1b$B%O%m!<%o!<%k%I!*\x1b(B', 'iso-2022-jp'),
  37. ('Gr\xfc\xdf Gott!', 'iso-8859-1')])
  38. 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'
  39. h = Header(long, j, header_name="Subject")
  40. # test a very long header
  41. enc = h.encode()
  42. # TK: splitting point may differ by codec design and/or Header encoding
  43. eq(enc , """\
  44. =?iso-2022-jp?b?dGVzdC1qYSAbJEIkWEVqOUYkNSRsJD8lYSE8JWskTztKGyhC?=
  45. =?iso-2022-jp?b?GyRCMnE8VCROPjVHJyRyQlQkQyRGJCQkXiQ5GyhC?=""")
  46. # TK: full decode comparison
  47. eq(h.__unicode__().encode('euc-jp'), long)
  48. def test_payload_encoding(self):
  49. jhello = '\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc\xa5\xeb\xa5\xc9\xa1\xaa'
  50. jcode = 'euc-jp'
  51. msg = Message()
  52. msg.set_payload(jhello, jcode)
  53. ustr = unicode(msg.get_payload(), msg.get_content_charset())
  54. self.assertEqual(jhello, ustr.encode(jcode))
  55. def suite():
  56. suite = unittest.TestSuite()
  57. suite.addTest(unittest.makeSuite(TestEmailAsianCodecs))
  58. return suite
  59. def test_main():
  60. run_unittest(TestEmailAsianCodecs)
  61. if __name__ == '__main__':
  62. unittest.main(defaultTest='suite')