»Core Development>Code coverage>Lib/test/test_email/test__encoded_words.py

Python code coverage for Lib/test/test_email/test__encoded_words.py

#countcontent
1n/aimport unittest
2n/afrom email import _encoded_words as _ew
3n/afrom email import errors
4n/afrom test.test_email import TestEmailBase
5n/a
6n/a
7n/aclass TestDecodeQ(TestEmailBase):
8n/a
9n/a def _test(self, source, ex_result, ex_defects=[]):
10n/a result, defects = _ew.decode_q(source)
11n/a self.assertEqual(result, ex_result)
12n/a self.assertDefectsEqual(defects, ex_defects)
13n/a
14n/a def test_no_encoded(self):
15n/a self._test(b'foobar', b'foobar')
16n/a
17n/a def test_spaces(self):
18n/a self._test(b'foo=20bar=20', b'foo bar ')
19n/a self._test(b'foo_bar_', b'foo bar ')
20n/a
21n/a def test_run_of_encoded(self):
22n/a self._test(b'foo=20=20=21=2Cbar', b'foo !,bar')
23n/a
24n/a
25n/aclass TestDecodeB(TestEmailBase):
26n/a
27n/a def _test(self, source, ex_result, ex_defects=[]):
28n/a result, defects = _ew.decode_b(source)
29n/a self.assertEqual(result, ex_result)
30n/a self.assertDefectsEqual(defects, ex_defects)
31n/a
32n/a def test_simple(self):
33n/a self._test(b'Zm9v', b'foo')
34n/a
35n/a def test_missing_padding(self):
36n/a self._test(b'dmk', b'vi', [errors.InvalidBase64PaddingDefect])
37n/a
38n/a def test_invalid_character(self):
39n/a self._test(b'dm\x01k===', b'vi', [errors.InvalidBase64CharactersDefect])
40n/a
41n/a def test_invalid_character_and_bad_padding(self):
42n/a self._test(b'dm\x01k', b'vi', [errors.InvalidBase64CharactersDefect,
43n/a errors.InvalidBase64PaddingDefect])
44n/a
45n/a
46n/aclass TestDecode(TestEmailBase):
47n/a
48n/a def test_wrong_format_input_raises(self):
49n/a with self.assertRaises(ValueError):
50n/a _ew.decode('=?badone?=')
51n/a with self.assertRaises(ValueError):
52n/a _ew.decode('=?')
53n/a with self.assertRaises(ValueError):
54n/a _ew.decode('')
55n/a
56n/a def _test(self, source, result, charset='us-ascii', lang='', defects=[]):
57n/a res, char, l, d = _ew.decode(source)
58n/a self.assertEqual(res, result)
59n/a self.assertEqual(char, charset)
60n/a self.assertEqual(l, lang)
61n/a self.assertDefectsEqual(d, defects)
62n/a
63n/a def test_simple_q(self):
64n/a self._test('=?us-ascii?q?foo?=', 'foo')
65n/a
66n/a def test_simple_b(self):
67n/a self._test('=?us-ascii?b?dmk=?=', 'vi')
68n/a
69n/a def test_q_case_ignored(self):
70n/a self._test('=?us-ascii?Q?foo?=', 'foo')
71n/a
72n/a def test_b_case_ignored(self):
73n/a self._test('=?us-ascii?B?dmk=?=', 'vi')
74n/a
75n/a def test_non_trivial_q(self):
76n/a self._test('=?latin-1?q?=20F=fcr=20Elise=20?=', ' Für Elise ', 'latin-1')
77n/a
78n/a def test_q_escaped_bytes_preserved(self):
79n/a self._test(b'=?us-ascii?q?=20\xACfoo?='.decode('us-ascii',
80n/a 'surrogateescape'),
81n/a ' \uDCACfoo',
82n/a defects = [errors.UndecodableBytesDefect])
83n/a
84n/a def test_b_undecodable_bytes_ignored_with_defect(self):
85n/a self._test(b'=?us-ascii?b?dm\xACk?='.decode('us-ascii',
86n/a 'surrogateescape'),
87n/a 'vi',
88n/a defects = [
89n/a errors.InvalidBase64CharactersDefect,
90n/a errors.InvalidBase64PaddingDefect])
91n/a
92n/a def test_b_invalid_bytes_ignored_with_defect(self):
93n/a self._test('=?us-ascii?b?dm\x01k===?=',
94n/a 'vi',
95n/a defects = [errors.InvalidBase64CharactersDefect])
96n/a
97n/a def test_b_invalid_bytes_incorrect_padding(self):
98n/a self._test('=?us-ascii?b?dm\x01k?=',
99n/a 'vi',
100n/a defects = [
101n/a errors.InvalidBase64CharactersDefect,
102n/a errors.InvalidBase64PaddingDefect])
103n/a
104n/a def test_b_padding_defect(self):
105n/a self._test('=?us-ascii?b?dmk?=',
106n/a 'vi',
107n/a defects = [errors.InvalidBase64PaddingDefect])
108n/a
109n/a def test_nonnull_lang(self):
110n/a self._test('=?us-ascii*jive?q?test?=', 'test', lang='jive')
111n/a
112n/a def test_unknown_8bit_charset(self):
113n/a self._test('=?unknown-8bit?q?foo=ACbar?=',
114n/a b'foo\xacbar'.decode('ascii', 'surrogateescape'),
115n/a charset = 'unknown-8bit',
116n/a defects = [])
117n/a
118n/a def test_unknown_charset(self):
119n/a self._test('=?foobar?q?foo=ACbar?=',
120n/a b'foo\xacbar'.decode('ascii', 'surrogateescape'),
121n/a charset = 'foobar',
122n/a # XXX Should this be a new Defect instead?
123n/a defects = [errors.CharsetError])
124n/a
125n/a def test_q_nonascii(self):
126n/a self._test('=?utf-8?q?=C3=89ric?=',
127n/a 'Éric',
128n/a charset='utf-8')
129n/a
130n/a
131n/aclass TestEncodeQ(TestEmailBase):
132n/a
133n/a def _test(self, src, expected):
134n/a self.assertEqual(_ew.encode_q(src), expected)
135n/a
136n/a def test_all_safe(self):
137n/a self._test(b'foobar', 'foobar')
138n/a
139n/a def test_spaces(self):
140n/a self._test(b'foo bar ', 'foo_bar_')
141n/a
142n/a def test_run_of_encodables(self):
143n/a self._test(b'foo ,,bar', 'foo__=2C=2Cbar')
144n/a
145n/a
146n/aclass TestEncodeB(TestEmailBase):
147n/a
148n/a def test_simple(self):
149n/a self.assertEqual(_ew.encode_b(b'foo'), 'Zm9v')
150n/a
151n/a def test_padding(self):
152n/a self.assertEqual(_ew.encode_b(b'vi'), 'dmk=')
153n/a
154n/a
155n/aclass TestEncode(TestEmailBase):
156n/a
157n/a def test_q(self):
158n/a self.assertEqual(_ew.encode('foo', 'utf-8', 'q'), '=?utf-8?q?foo?=')
159n/a
160n/a def test_b(self):
161n/a self.assertEqual(_ew.encode('foo', 'utf-8', 'b'), '=?utf-8?b?Zm9v?=')
162n/a
163n/a def test_auto_q(self):
164n/a self.assertEqual(_ew.encode('foo', 'utf-8'), '=?utf-8?q?foo?=')
165n/a
166n/a def test_auto_q_if_short_mostly_safe(self):
167n/a self.assertEqual(_ew.encode('vi.', 'utf-8'), '=?utf-8?q?vi=2E?=')
168n/a
169n/a def test_auto_b_if_enough_unsafe(self):
170n/a self.assertEqual(_ew.encode('.....', 'utf-8'), '=?utf-8?b?Li4uLi4=?=')
171n/a
172n/a def test_auto_b_if_long_unsafe(self):
173n/a self.assertEqual(_ew.encode('vi.vi.vi.vi.vi.', 'utf-8'),
174n/a '=?utf-8?b?dmkudmkudmkudmkudmku?=')
175n/a
176n/a def test_auto_q_if_long_mostly_safe(self):
177n/a self.assertEqual(_ew.encode('vi vi vi.vi ', 'utf-8'),
178n/a '=?utf-8?q?vi_vi_vi=2Evi_?=')
179n/a
180n/a def test_utf8_default(self):
181n/a self.assertEqual(_ew.encode('foo'), '=?utf-8?q?foo?=')
182n/a
183n/a def test_lang(self):
184n/a self.assertEqual(_ew.encode('foo', lang='jive'), '=?utf-8*jive?q?foo?=')
185n/a
186n/a def test_unknown_8bit(self):
187n/a self.assertEqual(_ew.encode('foo\uDCACbar', charset='unknown-8bit'),
188n/a '=?unknown-8bit?q?foo=ACbar?=')
189n/a
190n/a
191n/aif __name__ == '__main__':
192n/a unittest.main()