2007-04-05 17:08:56 -03:00
|
|
|
from test.test_support import run_unittest, open_urlresource
|
|
|
|
import unittest
|
|
|
|
|
2009-11-26 08:36:30 -04:00
|
|
|
from httplib import HTTPException
|
2002-11-23 18:08:15 -04:00
|
|
|
import sys
|
2002-11-24 14:53:11 -04:00
|
|
|
import os
|
2008-09-10 16:16:35 -03:00
|
|
|
from unicodedata import normalize, unidata_version
|
2002-11-24 14:53:11 -04:00
|
|
|
|
2010-03-15 15:08:58 -03:00
|
|
|
TESTDATAFILE = "NormalizationTest.txt"
|
2008-09-10 16:16:35 -03:00
|
|
|
TESTDATAURL = "http://www.unicode.org/Public/" + unidata_version + "/ucd/" + TESTDATAFILE
|
|
|
|
|
2010-03-15 15:08:58 -03:00
|
|
|
def check_version(testfile):
|
|
|
|
hdr = testfile.readline()
|
|
|
|
return unidata_version in hdr
|
2002-11-23 18:08:15 -04:00
|
|
|
|
2006-03-24 04:02:51 -04:00
|
|
|
class RangeError(Exception):
|
2002-11-23 18:08:15 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
def NFC(str):
|
|
|
|
return normalize("NFC", str)
|
|
|
|
|
|
|
|
def NFKC(str):
|
|
|
|
return normalize("NFKC", str)
|
|
|
|
|
|
|
|
def NFD(str):
|
|
|
|
return normalize("NFD", str)
|
|
|
|
|
|
|
|
def NFKD(str):
|
|
|
|
return normalize("NFKD", str)
|
|
|
|
|
|
|
|
def unistr(data):
|
|
|
|
data = [int(x, 16) for x in data.split(" ")]
|
|
|
|
for x in data:
|
|
|
|
if x > sys.maxunicode:
|
|
|
|
raise RangeError
|
|
|
|
return u"".join([unichr(x) for x in data])
|
2007-04-25 03:30:05 -03:00
|
|
|
|
2007-04-05 17:08:56 -03:00
|
|
|
class NormalizationTest(unittest.TestCase):
|
|
|
|
def test_main(self):
|
2010-03-15 15:08:58 -03:00
|
|
|
part = None
|
2007-04-05 17:08:56 -03:00
|
|
|
part1_data = {}
|
2009-11-01 17:26:14 -04:00
|
|
|
# Hit the exception early
|
|
|
|
try:
|
2010-03-15 15:08:58 -03:00
|
|
|
testdata = open_urlresource(TESTDATAURL, check_version)
|
2009-11-26 08:36:30 -04:00
|
|
|
except (IOError, HTTPException):
|
2009-11-01 17:26:14 -04:00
|
|
|
self.skipTest("Could not retrieve " + TESTDATAURL)
|
2010-03-15 15:08:58 -03:00
|
|
|
for line in testdata:
|
2007-04-05 17:08:56 -03:00
|
|
|
if '#' in line:
|
|
|
|
line = line.split('#')[0]
|
|
|
|
line = line.strip()
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
if line.startswith("@Part"):
|
|
|
|
part = line.split()[0]
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
c1,c2,c3,c4,c5 = [unistr(x) for x in line.split(';')[:-1]]
|
|
|
|
except RangeError:
|
|
|
|
# Skip unsupported characters;
|
2013-08-17 10:56:09 -03:00
|
|
|
# try at least adding c1 if we are in part1
|
2007-04-05 17:08:56 -03:00
|
|
|
if part == "@Part1":
|
|
|
|
try:
|
|
|
|
c1 = unistr(line.split(';')[0])
|
|
|
|
except RangeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
part1_data[c1] = 1
|
|
|
|
continue
|
2002-11-23 18:08:15 -04:00
|
|
|
|
2007-04-05 17:08:56 -03:00
|
|
|
# Perform tests
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(c2 == NFC(c1) == NFC(c2) == NFC(c3), line)
|
|
|
|
self.assertTrue(c4 == NFC(c4) == NFC(c5), line)
|
|
|
|
self.assertTrue(c3 == NFD(c1) == NFD(c2) == NFD(c3), line)
|
|
|
|
self.assertTrue(c5 == NFD(c4) == NFD(c5), line)
|
|
|
|
self.assertTrue(c4 == NFKC(c1) == NFKC(c2) == \
|
2007-04-05 17:08:56 -03:00
|
|
|
NFKC(c3) == NFKC(c4) == NFKC(c5),
|
|
|
|
line)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(c5 == NFKD(c1) == NFKD(c2) == \
|
2007-04-05 17:08:56 -03:00
|
|
|
NFKD(c3) == NFKD(c4) == NFKD(c5),
|
|
|
|
line)
|
2002-11-24 14:53:11 -04:00
|
|
|
|
2007-04-05 17:08:56 -03:00
|
|
|
# Record part 1 data
|
|
|
|
if part == "@Part1":
|
|
|
|
part1_data[c1] = 1
|
2002-11-23 18:08:15 -04:00
|
|
|
|
2007-04-05 17:08:56 -03:00
|
|
|
# Perform tests for all other data
|
|
|
|
for c in range(sys.maxunicode+1):
|
|
|
|
X = unichr(c)
|
|
|
|
if X in part1_data:
|
|
|
|
continue
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(X == NFC(X) == NFD(X) == NFKC(X) == NFKD(X), c)
|
2002-11-23 22:35:35 -04:00
|
|
|
|
2007-04-05 17:08:56 -03:00
|
|
|
def test_bug_834676(self):
|
|
|
|
# Check for bug 834676
|
|
|
|
normalize('NFC', u'\ud55c\uae00')
|
2002-11-23 18:08:15 -04:00
|
|
|
|
|
|
|
|
2007-04-05 17:08:56 -03:00
|
|
|
def test_main():
|
|
|
|
run_unittest(NormalizationTest)
|
2003-11-06 16:47:57 -04:00
|
|
|
|
2002-11-24 14:53:11 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|