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

Python code coverage for Lib/test/test_normalization.py

#countcontent
1n/afrom test.support import open_urlresource
2n/aimport unittest
3n/a
4n/afrom http.client import HTTPException
5n/aimport sys
6n/afrom unicodedata import normalize, unidata_version
7n/a
8n/aTESTDATAFILE = "NormalizationTest.txt"
9n/aTESTDATAURL = "http://www.pythontest.net/unicode/" + unidata_version + "/" + TESTDATAFILE
10n/a
11n/adef check_version(testfile):
12n/a hdr = testfile.readline()
13n/a return unidata_version in hdr
14n/a
15n/aclass RangeError(Exception):
16n/a pass
17n/a
18n/adef NFC(str):
19n/a return normalize("NFC", str)
20n/a
21n/adef NFKC(str):
22n/a return normalize("NFKC", str)
23n/a
24n/adef NFD(str):
25n/a return normalize("NFD", str)
26n/a
27n/adef NFKD(str):
28n/a return normalize("NFKD", str)
29n/a
30n/adef unistr(data):
31n/a data = [int(x, 16) for x in data.split(" ")]
32n/a for x in data:
33n/a if x > sys.maxunicode:
34n/a raise RangeError
35n/a return "".join([chr(x) for x in data])
36n/a
37n/aclass NormalizationTest(unittest.TestCase):
38n/a def test_main(self):
39n/a part = None
40n/a part1_data = {}
41n/a # Hit the exception early
42n/a try:
43n/a testdata = open_urlresource(TESTDATAURL, encoding="utf-8",
44n/a check=check_version)
45n/a except (OSError, HTTPException):
46n/a self.skipTest("Could not retrieve " + TESTDATAURL)
47n/a self.addCleanup(testdata.close)
48n/a for line in testdata:
49n/a if '#' in line:
50n/a line = line.split('#')[0]
51n/a line = line.strip()
52n/a if not line:
53n/a continue
54n/a if line.startswith("@Part"):
55n/a part = line.split()[0]
56n/a continue
57n/a try:
58n/a c1,c2,c3,c4,c5 = [unistr(x) for x in line.split(';')[:-1]]
59n/a except RangeError:
60n/a # Skip unsupported characters;
61n/a # try at least adding c1 if we are in part1
62n/a if part == "@Part1":
63n/a try:
64n/a c1 = unistr(line.split(';')[0])
65n/a except RangeError:
66n/a pass
67n/a else:
68n/a part1_data[c1] = 1
69n/a continue
70n/a
71n/a # Perform tests
72n/a self.assertTrue(c2 == NFC(c1) == NFC(c2) == NFC(c3), line)
73n/a self.assertTrue(c4 == NFC(c4) == NFC(c5), line)
74n/a self.assertTrue(c3 == NFD(c1) == NFD(c2) == NFD(c3), line)
75n/a self.assertTrue(c5 == NFD(c4) == NFD(c5), line)
76n/a self.assertTrue(c4 == NFKC(c1) == NFKC(c2) == \
77n/a NFKC(c3) == NFKC(c4) == NFKC(c5),
78n/a line)
79n/a self.assertTrue(c5 == NFKD(c1) == NFKD(c2) == \
80n/a NFKD(c3) == NFKD(c4) == NFKD(c5),
81n/a line)
82n/a
83n/a # Record part 1 data
84n/a if part == "@Part1":
85n/a part1_data[c1] = 1
86n/a
87n/a # Perform tests for all other data
88n/a for c in range(sys.maxunicode+1):
89n/a X = chr(c)
90n/a if X in part1_data:
91n/a continue
92n/a self.assertTrue(X == NFC(X) == NFD(X) == NFKC(X) == NFKD(X), c)
93n/a
94n/a def test_bug_834676(self):
95n/a # Check for bug 834676
96n/a normalize('NFC', '\ud55c\uae00')
97n/a
98n/a
99n/aif __name__ == "__main__":
100n/a unittest.main()