11from test .test_support import verbose , TestFailed , TestSkipped , verify
22import sys
3+ import os
34from unicodedata import normalize
4- try :
5- data = open ("NormalizationTest.txt" , "r" ).readlines ()
6- except IOError :
7- raise TestSkipped ("NormalizationTest.txt not found, download from "
8- "http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt" )
5+
6+ TESTDATAFILE = "NormalizationTest.txt"
7+ skip_expected = not os .path .exists (TESTDATAFILE )
98
109class RangeError :
1110 pass
@@ -29,40 +28,52 @@ def unistr(data):
2928 raise RangeError
3029 return u"" .join ([unichr (x ) for x in data ])
3130
32- part1_data = {}
33- for line in data :
34- if '#' in line :
35- line = line .split ('#' )[0 ]
36- line = line .strip ()
37- if not line :
38- continue
39- if line .startswith ("@Part" ):
40- part = line
41- continue
42- try :
43- c1 ,c2 ,c3 ,c4 ,c5 = [unistr (x ) for x in line .split (';' )[:- 1 ]]
44- except RangeError :
45- # Skip unsupported characters
46- continue
31+ def test_main ():
32+ if skip_expected :
33+ raise TestSkipped (TESTDATAFILE + " not found, download from " +
34+ "http://www.unicode.org/Public/UNIDATA/" + TESTDATAFILE )
35+
36+ data = open (TESTDATAFILE ).readlines ()
37+
38+ part1_data = {}
39+ for line in data :
40+ if '#' in line :
41+ line = line .split ('#' )[0 ]
42+ line = line .strip ()
43+ if not line :
44+ continue
45+ if line .startswith ("@Part" ):
46+ part = line
47+ continue
48+ try :
49+ c1 ,c2 ,c3 ,c4 ,c5 = [unistr (x ) for x in line .split (';' )[:- 1 ]]
50+ except RangeError :
51+ # Skip unsupported characters
52+ continue
53+
54+ if verbose :
55+ print line
4756
48- if verbose :
49- print line
57+ # Perform tests
58+ verify (c2 == NFC (c1 ) == NFC (c2 ) == NFC (c3 ), line )
59+ verify (c4 == NFC (c4 ) == NFC (c5 ), line )
60+ verify (c3 == NFD (c1 ) == NFD (c2 ) == NFD (c3 ), line )
61+ verify (c5 == NFD (c4 ) == NFD (c5 ), line )
62+ verify (c4 == NFKC (c1 ) == NFKC (c2 ) == NFKC (c3 ) == NFKC (c4 ) == NFKC (c5 ),
63+ line )
64+ verify (c5 == NFKD (c1 ) == NFKD (c2 ) == NFKD (c3 ) == NFKD (c4 ) == NFKD (c5 ),
65+ line )
5066
51- # Perform tests
52- verify (c2 == NFC (c1 ) == NFC (c2 ) == NFC (c3 ), line )
53- verify (c4 == NFC (c4 ) == NFC (c5 ), line )
54- verify (c3 == NFD (c1 ) == NFD (c2 ) == NFD (c3 ), line )
55- verify (c5 == NFD (c4 ) == NFD (c5 ), line )
56- verify (c4 == NFKC (c1 ) == NFKC (c2 ) == NFKC (c3 ) == NFKC (c4 ) == NFKC (c5 ), line )
57- verify (c5 == NFKD (c1 ) == NFKD (c2 ) == NFKD (c3 ) == NFKD (c4 ) == NFKD (c5 ), line )
67+ # Record part 1 data
68+ if part == "@Part1" :
69+ part1_data [c1 ] = 1
5870
59- # Record part 1 data
60- if part == "@Part1" :
61- part1_data [c1 ] = 1
71+ # Perform tests for all other data
72+ for c in range (sys .maxunicode + 1 ):
73+ X = unichr (c )
74+ if X in part1_data :
75+ continue
76+ assert X == NFC (X ) == NFD (X ) == NFKC (X ) == NFKD (X ), c
6277
63- # Perform tests for all other data
64- for c in range (sys .maxunicode + 1 ):
65- X = unichr (c )
66- if X in part1_data :
67- continue
68- assert X == NFC (X ) == NFD (X ) == NFKC (X ) == NFKD (X ), c
78+ if __name__ == "__main__" :
79+ test_main ()
0 commit comments