+++ /dev/null
-""" Test script for the unicodedata module.\r
-\r
- Written by Marc-Andre Lemburg (mal@lemburg.com).\r
-\r
- (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.\r
-\r
-"""\r
-\r
-import sys\r
-import unittest\r
-import hashlib\r
-import subprocess\r
-import test.test_support\r
-\r
-encoding = 'utf-8'\r
-\r
-\r
-### Run tests\r
-\r
-class UnicodeMethodsTest(unittest.TestCase):\r
-\r
- # update this, if the database changes\r
- expectedchecksum = '4504dffd035baea02c5b9de82bebc3d65e0e0baf'\r
-\r
- def test_method_checksum(self):\r
- h = hashlib.sha1()\r
- for i in range(0x10000):\r
- char = unichr(i)\r
- data = [\r
- # Predicates (single char)\r
- u"01"[char.isalnum()],\r
- u"01"[char.isalpha()],\r
- u"01"[char.isdecimal()],\r
- u"01"[char.isdigit()],\r
- u"01"[char.islower()],\r
- u"01"[char.isnumeric()],\r
- u"01"[char.isspace()],\r
- u"01"[char.istitle()],\r
- u"01"[char.isupper()],\r
-\r
- # Predicates (multiple chars)\r
- u"01"[(char + u'abc').isalnum()],\r
- u"01"[(char + u'abc').isalpha()],\r
- u"01"[(char + u'123').isdecimal()],\r
- u"01"[(char + u'123').isdigit()],\r
- u"01"[(char + u'abc').islower()],\r
- u"01"[(char + u'123').isnumeric()],\r
- u"01"[(char + u' \t').isspace()],\r
- u"01"[(char + u'abc').istitle()],\r
- u"01"[(char + u'ABC').isupper()],\r
-\r
- # Mappings (single char)\r
- char.lower(),\r
- char.upper(),\r
- char.title(),\r
-\r
- # Mappings (multiple chars)\r
- (char + u'abc').lower(),\r
- (char + u'ABC').upper(),\r
- (char + u'abc').title(),\r
- (char + u'ABC').title(),\r
-\r
- ]\r
- h.update(u''.join(data).encode(encoding))\r
- result = h.hexdigest()\r
- self.assertEqual(result, self.expectedchecksum)\r
-\r
-class UnicodeDatabaseTest(unittest.TestCase):\r
-\r
- def setUp(self):\r
- # In case unicodedata is not available, this will raise an ImportError,\r
- # but the other test cases will still be run\r
- import unicodedata\r
- self.db = unicodedata\r
-\r
- def tearDown(self):\r
- del self.db\r
-\r
-class UnicodeFunctionsTest(UnicodeDatabaseTest):\r
-\r
- # update this, if the database changes\r
- expectedchecksum = '6ccf1b1a36460d2694f9b0b0f0324942fe70ede6'\r
-\r
- def test_function_checksum(self):\r
- data = []\r
- h = hashlib.sha1()\r
-\r
- for i in range(0x10000):\r
- char = unichr(i)\r
- data = [\r
- # Properties\r
- str(self.db.digit(char, -1)),\r
- str(self.db.numeric(char, -1)),\r
- str(self.db.decimal(char, -1)),\r
- self.db.category(char),\r
- self.db.bidirectional(char),\r
- self.db.decomposition(char),\r
- str(self.db.mirrored(char)),\r
- str(self.db.combining(char)),\r
- ]\r
- h.update(''.join(data))\r
- result = h.hexdigest()\r
- self.assertEqual(result, self.expectedchecksum)\r
-\r
- def test_digit(self):\r
- self.assertEqual(self.db.digit(u'A', None), None)\r
- self.assertEqual(self.db.digit(u'9'), 9)\r
- self.assertEqual(self.db.digit(u'\u215b', None), None)\r
- self.assertEqual(self.db.digit(u'\u2468'), 9)\r
- self.assertEqual(self.db.digit(u'\U00020000', None), None)\r
-\r
- self.assertRaises(TypeError, self.db.digit)\r
- self.assertRaises(TypeError, self.db.digit, u'xx')\r
- self.assertRaises(ValueError, self.db.digit, u'x')\r
-\r
- def test_numeric(self):\r
- self.assertEqual(self.db.numeric(u'A',None), None)\r
- self.assertEqual(self.db.numeric(u'9'), 9)\r
- self.assertEqual(self.db.numeric(u'\u215b'), 0.125)\r
- self.assertEqual(self.db.numeric(u'\u2468'), 9.0)\r
- self.assertEqual(self.db.numeric(u'\ua627'), 7.0)\r
- self.assertEqual(self.db.numeric(u'\U00020000', None), None)\r
-\r
- self.assertRaises(TypeError, self.db.numeric)\r
- self.assertRaises(TypeError, self.db.numeric, u'xx')\r
- self.assertRaises(ValueError, self.db.numeric, u'x')\r
-\r
- def test_decimal(self):\r
- self.assertEqual(self.db.decimal(u'A',None), None)\r
- self.assertEqual(self.db.decimal(u'9'), 9)\r
- self.assertEqual(self.db.decimal(u'\u215b', None), None)\r
- self.assertEqual(self.db.decimal(u'\u2468', None), None)\r
- self.assertEqual(self.db.decimal(u'\U00020000', None), None)\r
-\r
- self.assertRaises(TypeError, self.db.decimal)\r
- self.assertRaises(TypeError, self.db.decimal, u'xx')\r
- self.assertRaises(ValueError, self.db.decimal, u'x')\r
-\r
- def test_category(self):\r
- self.assertEqual(self.db.category(u'\uFFFE'), 'Cn')\r
- self.assertEqual(self.db.category(u'a'), 'Ll')\r
- self.assertEqual(self.db.category(u'A'), 'Lu')\r
- self.assertEqual(self.db.category(u'\U00020000'), 'Lo')\r
-\r
- self.assertRaises(TypeError, self.db.category)\r
- self.assertRaises(TypeError, self.db.category, u'xx')\r
-\r
- def test_bidirectional(self):\r
- self.assertEqual(self.db.bidirectional(u'\uFFFE'), '')\r
- self.assertEqual(self.db.bidirectional(u' '), 'WS')\r
- self.assertEqual(self.db.bidirectional(u'A'), 'L')\r
- self.assertEqual(self.db.bidirectional(u'\U00020000'), 'L')\r
-\r
- self.assertRaises(TypeError, self.db.bidirectional)\r
- self.assertRaises(TypeError, self.db.bidirectional, u'xx')\r
-\r
- def test_decomposition(self):\r
- self.assertEqual(self.db.decomposition(u'\uFFFE'),'')\r
- self.assertEqual(self.db.decomposition(u'\u00bc'), '<fraction> 0031 2044 0034')\r
-\r
- self.assertRaises(TypeError, self.db.decomposition)\r
- self.assertRaises(TypeError, self.db.decomposition, u'xx')\r
-\r
- def test_mirrored(self):\r
- self.assertEqual(self.db.mirrored(u'\uFFFE'), 0)\r
- self.assertEqual(self.db.mirrored(u'a'), 0)\r
- self.assertEqual(self.db.mirrored(u'\u2201'), 1)\r
- self.assertEqual(self.db.mirrored(u'\U00020000'), 0)\r
-\r
- self.assertRaises(TypeError, self.db.mirrored)\r
- self.assertRaises(TypeError, self.db.mirrored, u'xx')\r
-\r
- def test_combining(self):\r
- self.assertEqual(self.db.combining(u'\uFFFE'), 0)\r
- self.assertEqual(self.db.combining(u'a'), 0)\r
- self.assertEqual(self.db.combining(u'\u20e1'), 230)\r
- self.assertEqual(self.db.combining(u'\U00020000'), 0)\r
-\r
- self.assertRaises(TypeError, self.db.combining)\r
- self.assertRaises(TypeError, self.db.combining, u'xx')\r
-\r
- def test_normalize(self):\r
- self.assertRaises(TypeError, self.db.normalize)\r
- self.assertRaises(ValueError, self.db.normalize, 'unknown', u'xx')\r
- self.assertEqual(self.db.normalize('NFKC', u''), u'')\r
- # The rest can be found in test_normalization.py\r
- # which requires an external file.\r
-\r
- def test_pr29(self):\r
- # http://www.unicode.org/review/pr-29.html\r
- # See issues #1054943 and #10254.\r
- composed = (u"\u0b47\u0300\u0b3e", u"\u1100\u0300\u1161",\r
- u'Li\u030dt-s\u1e73\u0301',\r
- u'\u092e\u093e\u0930\u094d\u0915 \u091c\u093c'\r
- + u'\u0941\u0915\u0947\u0930\u092c\u0930\u094d\u0917',\r
- u'\u0915\u093f\u0930\u094d\u0917\u093f\u091c\u093c'\r
- + 'u\u0938\u094d\u0924\u093e\u0928')\r
- for text in composed:\r
- self.assertEqual(self.db.normalize('NFC', text), text)\r
-\r
- def test_issue10254(self):\r
- # Crash reported in #10254\r
- a = u'C\u0338' * 20 + u'C\u0327'\r
- b = u'C\u0338' * 20 + u'\xC7'\r
- self.assertEqual(self.db.normalize('NFC', a), b)\r
-\r
- def test_east_asian_width(self):\r
- eaw = self.db.east_asian_width\r
- self.assertRaises(TypeError, eaw, 'a')\r
- self.assertRaises(TypeError, eaw, u'')\r
- self.assertRaises(TypeError, eaw, u'ra')\r
- self.assertEqual(eaw(u'\x1e'), 'N')\r
- self.assertEqual(eaw(u'\x20'), 'Na')\r
- self.assertEqual(eaw(u'\uC894'), 'W')\r
- self.assertEqual(eaw(u'\uFF66'), 'H')\r
- self.assertEqual(eaw(u'\uFF1F'), 'F')\r
- self.assertEqual(eaw(u'\u2010'), 'A')\r
- self.assertEqual(eaw(u'\U00020000'), 'W')\r
-\r
-class UnicodeMiscTest(UnicodeDatabaseTest):\r
-\r
- def test_failed_import_during_compiling(self):\r
- # Issue 4367\r
- # Decoding \N escapes requires the unicodedata module. If it can't be\r
- # imported, we shouldn't segfault.\r
-\r
- # This program should raise a SyntaxError in the eval.\r
- code = "import sys;" \\r
- "sys.modules['unicodedata'] = None;" \\r
- """eval("u'\N{SOFT HYPHEN}'")"""\r
- args = [sys.executable, "-c", code]\r
- # We use a subprocess because the unicodedata module may already have\r
- # been loaded in this process.\r
- popen = subprocess.Popen(args, stderr=subprocess.PIPE)\r
- popen.wait()\r
- self.assertEqual(popen.returncode, 1)\r
- error = "SyntaxError: (unicode error) \N escapes not supported " \\r
- "(can't load unicodedata module)"\r
- self.assertIn(error, popen.stderr.read())\r
-\r
- def test_decimal_numeric_consistent(self):\r
- # Test that decimal and numeric are consistent,\r
- # i.e. if a character has a decimal value,\r
- # its numeric value should be the same.\r
- count = 0\r
- for i in xrange(0x10000):\r
- c = unichr(i)\r
- dec = self.db.decimal(c, -1)\r
- if dec != -1:\r
- self.assertEqual(dec, self.db.numeric(c))\r
- count += 1\r
- self.assertTrue(count >= 10) # should have tested at least the ASCII digits\r
-\r
- def test_digit_numeric_consistent(self):\r
- # Test that digit and numeric are consistent,\r
- # i.e. if a character has a digit value,\r
- # its numeric value should be the same.\r
- count = 0\r
- for i in xrange(0x10000):\r
- c = unichr(i)\r
- dec = self.db.digit(c, -1)\r
- if dec != -1:\r
- self.assertEqual(dec, self.db.numeric(c))\r
- count += 1\r
- self.assertTrue(count >= 10) # should have tested at least the ASCII digits\r
-\r
- def test_bug_1704793(self):\r
- self.assertEqual(self.db.lookup("GOTHIC LETTER FAIHU"), u'\U00010346')\r
-\r
- def test_ucd_510(self):\r
- import unicodedata\r
- # In UCD 5.1.0, a mirrored property changed wrt. UCD 3.2.0\r
- self.assertTrue(unicodedata.mirrored(u"\u0f3a"))\r
- self.assertTrue(not unicodedata.ucd_3_2_0.mirrored(u"\u0f3a"))\r
- # Also, we now have two ways of representing\r
- # the upper-case mapping: as delta, or as absolute value\r
- self.assertTrue(u"a".upper()==u'A')\r
- self.assertTrue(u"\u1d79".upper()==u'\ua77d')\r
- self.assertTrue(u".".upper()==u".")\r
-\r
- def test_bug_5828(self):\r
- self.assertEqual(u"\u1d79".lower(), u"\u1d79")\r
- # Only U+0000 should have U+0000 as its upper/lower/titlecase variant\r
- self.assertEqual(\r
- [\r
- c for c in range(sys.maxunicode+1)\r
- if u"\x00" in unichr(c).lower()+unichr(c).upper()+unichr(c).title()\r
- ],\r
- [0]\r
- )\r
-\r
- def test_bug_4971(self):\r
- # LETTER DZ WITH CARON: DZ, Dz, dz\r
- self.assertEqual(u"\u01c4".title(), u"\u01c5")\r
- self.assertEqual(u"\u01c5".title(), u"\u01c5")\r
- self.assertEqual(u"\u01c6".title(), u"\u01c5")\r
-\r
- def test_linebreak_7643(self):\r
- for i in range(0x10000):\r
- lines = (unichr(i) + u'A').splitlines()\r
- if i in (0x0a, 0x0b, 0x0c, 0x0d, 0x85,\r
- 0x1c, 0x1d, 0x1e, 0x2028, 0x2029):\r
- self.assertEqual(len(lines), 2,\r
- r"\u%.4x should be a linebreak" % i)\r
- else:\r
- self.assertEqual(len(lines), 1,\r
- r"\u%.4x should not be a linebreak" % i)\r
-\r
-def test_main():\r
- test.test_support.run_unittest(\r
- UnicodeMiscTest,\r
- UnicodeMethodsTest,\r
- UnicodeFunctionsTest\r
- )\r
-\r
-if __name__ == "__main__":\r
- test_main()\r