ยปCore Development>Code coverage>Lib/email/test/test_email_codecs.py

Python code coverage for Lib/email/test/test_email_codecs.py

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