ยปCore Development>Code coverage>Lib/test/test_email/test_asian_codecs.py

Python code coverage for Lib/test/test_email/test_asian_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/a
7n/afrom test.test_email import TestEmailBase
8n/afrom email.charset import Charset
9n/afrom email.header import Header, decode_header
10n/afrom email.message import Message
11n/a
12n/a# We're compatible with Python 2.3, but it doesn't have the built-in Asian
13n/a# codecs, so we have to skip all these tests.
14n/atry:
15n/a str(b'foo', 'euc-jp')
16n/aexcept LookupError:
17n/a raise unittest.SkipTest
18n/a
19n/a
20n/a
21n/aclass TestEmailAsianCodecs(TestEmailBase):
22n/a def test_japanese_codecs(self):
23n/a eq = self.ndiffAssertEqual
24n/a jcode = "euc-jp"
25n/a gcode = "iso-8859-1"
26n/a j = Charset(jcode)
27n/a g = Charset(gcode)
28n/a h = Header("Hello World!")
29n/a jhello = str(b'\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc'
30n/a b'\xa5\xeb\xa5\xc9\xa1\xaa', jcode)
31n/a ghello = str(b'Gr\xfc\xdf Gott!', gcode)
32n/a h.append(jhello, j)
33n/a h.append(ghello, g)
34n/a # BAW: This used to -- and maybe should -- fold the two iso-8859-1
35n/a # chunks into a single encoded word. However it doesn't violate the
36n/a # standard to have them as two encoded chunks and maybe it's
37n/a # reasonable <wink> for each .append() call to result in a separate
38n/a # encoded word.
39n/a eq(h.encode(), """\
40n/aHello World! =?iso-2022-jp?b?GyRCJU8lbSE8JW8hPCVrJUkhKhsoQg==?=
41n/a =?iso-8859-1?q?Gr=FC=DF_Gott!?=""")
42n/a eq(decode_header(h.encode()),
43n/a [(b'Hello World! ', None),
44n/a (b'\x1b$B%O%m!<%o!<%k%I!*\x1b(B', 'iso-2022-jp'),
45n/a (b'Gr\xfc\xdf Gott!', gcode)])
46n/a subject_bytes = (b'test-ja \xa4\xd8\xc5\xea\xb9\xc6\xa4\xb5'
47n/a b'\xa4\xec\xa4\xbf\xa5\xe1\xa1\xbc\xa5\xeb\xa4\xcf\xbb\xca\xb2'
48n/a b'\xf1\xbc\xd4\xa4\xce\xbe\xb5\xc7\xa7\xa4\xf2\xc2\xd4\xa4\xc3'
49n/a b'\xa4\xc6\xa4\xa4\xa4\xde\xa4\xb9')
50n/a subject = str(subject_bytes, jcode)
51n/a h = Header(subject, j, header_name="Subject")
52n/a # test a very long header
53n/a enc = h.encode()
54n/a # TK: splitting point may differ by codec design and/or Header encoding
55n/a eq(enc , """\
56n/a=?iso-2022-jp?b?dGVzdC1qYSAbJEIkWEVqOUYkNSRsJD8lYSE8JWskTztKGyhC?=
57n/a =?iso-2022-jp?b?GyRCMnE8VCROPjVHJyRyQlQkQyRGJCQkXiQ5GyhC?=""")
58n/a # TK: full decode comparison
59n/a eq(str(h).encode(jcode), subject_bytes)
60n/a
61n/a def test_payload_encoding_utf8(self):
62n/a jhello = str(b'\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc'
63n/a b'\xa5\xeb\xa5\xc9\xa1\xaa', 'euc-jp')
64n/a msg = Message()
65n/a msg.set_payload(jhello, 'utf-8')
66n/a ustr = msg.get_payload(decode=True).decode(msg.get_content_charset())
67n/a self.assertEqual(jhello, ustr)
68n/a
69n/a def test_payload_encoding(self):
70n/a jcode = 'euc-jp'
71n/a jhello = str(b'\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc'
72n/a b'\xa5\xeb\xa5\xc9\xa1\xaa', jcode)
73n/a msg = Message()
74n/a msg.set_payload(jhello, jcode)
75n/a ustr = msg.get_payload(decode=True).decode(msg.get_content_charset())
76n/a self.assertEqual(jhello, ustr)
77n/a
78n/a
79n/a
80n/aif __name__ == '__main__':
81n/a unittest.main()