1 from test
.test_support
import run_unittest
, open_urlresource
4 from httplib
import HTTPException
7 from unicodedata
import normalize
, unidata_version
9 TESTDATAFILE
= "NormalizationTest.txt"
10 TESTDATAURL
= "http://www.unicode.org/Public/" + unidata_version
+ "/ucd/" + TESTDATAFILE
12 def check_version(testfile
):
13 hdr
= testfile
.readline()
14 return unidata_version
in hdr
16 class RangeError(Exception):
20 return normalize("NFC", str)
23 return normalize("NFKC", str)
26 return normalize("NFD", str)
29 return normalize("NFKD", str)
32 data
= [int(x
, 16) for x
in data
.split(" ")]
34 if x
> sys
.maxunicode
:
36 return u
"".join([unichr(x
) for x
in data
])
38 class NormalizationTest(unittest
.TestCase
):
42 # Hit the exception early
44 testdata
= open_urlresource(TESTDATAURL
, check_version
)
45 except (IOError, HTTPException
):
46 self
.skipTest("Could not retrieve " + TESTDATAURL
)
49 line
= line
.split('#')[0]
53 if line
.startswith("@Part"):
54 part
= line
.split()[0]
57 # XXX we don't support PRI #29 yet, so skip these tests for now
60 c1
,c2
,c3
,c4
,c5
= [unistr(x
) for x
in line
.split(';')[:-1]]
62 # Skip unsupported characters;
63 # try atleast adding c1 if we are in part1
66 c1
= unistr(line
.split(';')[0])
74 self
.assertTrue(c2
== NFC(c1
) == NFC(c2
) == NFC(c3
), line
)
75 self
.assertTrue(c4
== NFC(c4
) == NFC(c5
), line
)
76 self
.assertTrue(c3
== NFD(c1
) == NFD(c2
) == NFD(c3
), line
)
77 self
.assertTrue(c5
== NFD(c4
) == NFD(c5
), line
)
78 self
.assertTrue(c4
== NFKC(c1
) == NFKC(c2
) == \
79 NFKC(c3
) == NFKC(c4
) == NFKC(c5
),
81 self
.assertTrue(c5
== NFKD(c1
) == NFKD(c2
) == \
82 NFKD(c3
) == NFKD(c4
) == NFKD(c5
),
89 # Perform tests for all other data
90 for c
in range(sys
.maxunicode
+1):
94 self
.assertTrue(X
== NFC(X
) == NFD(X
) == NFKC(X
) == NFKD(X
), c
)
96 def test_bug_834676(self
):
97 # Check for bug 834676
98 normalize('NFC', u
'\ud55c\uae00')
102 run_unittest(NormalizationTest
)
104 if __name__
== "__main__":