»Core Development>Code coverage>Lib/test/test_html.py

Python code coverage for Lib/test/test_html.py

#countcontent
1n/a"""
2n/aTests for the html module functions.
3n/a"""
4n/a
5n/aimport html
6n/aimport unittest
7n/a
8n/a
9n/aclass HtmlTests(unittest.TestCase):
10n/a def test_escape(self):
11n/a self.assertEqual(
12n/a html.escape('\'<script>"&foo;"</script>\''),
13n/a '&#x27;&lt;script&gt;&quot;&amp;foo;&quot;&lt;/script&gt;&#x27;')
14n/a self.assertEqual(
15n/a html.escape('\'<script>"&foo;"</script>\'', False),
16n/a '\'&lt;script&gt;"&amp;foo;"&lt;/script&gt;\'')
17n/a
18n/a def test_unescape(self):
19n/a numeric_formats = ['&#%d', '&#%d;', '&#x%x', '&#x%x;']
20n/a errmsg = 'unescape(%r) should have returned %r'
21n/a def check(text, expected):
22n/a self.assertEqual(html.unescape(text), expected,
23n/a msg=errmsg % (text, expected))
24n/a def check_num(num, expected):
25n/a for format in numeric_formats:
26n/a text = format % num
27n/a self.assertEqual(html.unescape(text), expected,
28n/a msg=errmsg % (text, expected))
29n/a # check text with no character references
30n/a check('no character references', 'no character references')
31n/a # check & followed by invalid chars
32n/a check('&\n&\t& &&', '&\n&\t& &&')
33n/a # check & followed by numbers and letters
34n/a check('&0 &9 &a &0; &9; &a;', '&0 &9 &a &0; &9; &a;')
35n/a # check incomplete entities at the end of the string
36n/a for x in ['&', '&#', '&#x', '&#X', '&#y', '&#xy', '&#Xy']:
37n/a check(x, x)
38n/a check(x+';', x+';')
39n/a # check several combinations of numeric character references,
40n/a # possibly followed by different characters
41n/a formats = ['&#%d', '&#%07d', '&#%d;', '&#%07d;',
42n/a '&#x%x', '&#x%06x', '&#x%x;', '&#x%06x;',
43n/a '&#x%X', '&#x%06X', '&#X%x;', '&#X%06x;']
44n/a for num, char in zip([65, 97, 34, 38, 0x2603, 0x101234],
45n/a ['A', 'a', '"', '&', '\u2603', '\U00101234']):
46n/a for s in formats:
47n/a check(s % num, char)
48n/a for end in [' ', 'X']:
49n/a check((s+end) % num, char+end)
50n/a # check invalid code points
51n/a for cp in [0xD800, 0xDB00, 0xDC00, 0xDFFF, 0x110000]:
52n/a check_num(cp, '\uFFFD')
53n/a # check more invalid code points
54n/a for cp in [0x1, 0xb, 0xe, 0x7f, 0xfffe, 0xffff, 0x10fffe, 0x10ffff]:
55n/a check_num(cp, '')
56n/a # check invalid numbers
57n/a for num, ch in zip([0x0d, 0x80, 0x95, 0x9d], '\r\u20ac\u2022\x9d'):
58n/a check_num(num, ch)
59n/a # check small numbers
60n/a check_num(0, '\uFFFD')
61n/a check_num(9, '\t')
62n/a # check a big number
63n/a check_num(1000000000000000000, '\uFFFD')
64n/a # check that multiple trailing semicolons are handled correctly
65n/a for e in ['&quot;;', '&#34;;', '&#x22;;', '&#X22;;']:
66n/a check(e, '";')
67n/a # check that semicolons in the middle don't create problems
68n/a for e in ['&quot;quot;', '&#34;quot;', '&#x22;quot;', '&#X22;quot;']:
69n/a check(e, '"quot;')
70n/a # check triple adjacent charrefs
71n/a for e in ['&quot', '&#34', '&#x22', '&#X22']:
72n/a check(e*3, '"""')
73n/a check((e+';')*3, '"""')
74n/a # check that the case is respected
75n/a for e in ['&amp', '&amp;', '&AMP', '&AMP;']:
76n/a check(e, '&')
77n/a for e in ['&Amp', '&Amp;']:
78n/a check(e, e)
79n/a # check that non-existent named entities are returned unchanged
80n/a check('&svadilfari;', '&svadilfari;')
81n/a # the following examples are in the html5 specs
82n/a check('&notit', '¬it')
83n/a check('&notit;', '¬it;')
84n/a check('&notin', '¬in')
85n/a check('&notin;', '∉')
86n/a # a similar example with a long name
87n/a check('&notReallyAnExistingNamedCharacterReference;',
88n/a '¬ReallyAnExistingNamedCharacterReference;')
89n/a # longest valid name
90n/a check('&CounterClockwiseContourIntegral;', '∳')
91n/a # check a charref that maps to two unicode chars
92n/a check('&acE;', '\u223E\u0333')
93n/a check('&acE', '&acE')
94n/a # see #12888
95n/a check('&#123; ' * 1050, '{ ' * 1050)
96n/a # see #15156
97n/a check('&Eacuteric&Eacute;ric&alphacentauri&alpha;centauri',
98n/a 'ÉricÉric&alphacentauriαcentauri')
99n/a check('&co;', '&co;')
100n/a
101n/a
102n/aif __name__ == '__main__':
103n/a unittest.main()