/Lib/test/test_unicodedata.py

http://unladen-swallow.googlecode.com/ · Python · 287 lines · 204 code · 47 blank · 36 comment · 9 complexity · e0e00058a4ebef752051f5969416111e MD5 · raw file

  1. """ Test script for the unicodedata module.
  2. Written by Marc-Andre Lemburg (mal@lemburg.com).
  3. (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
  4. """
  5. import sys
  6. import unittest
  7. import hashlib
  8. import subprocess
  9. import test.test_support
  10. encoding = 'utf-8'
  11. ### Run tests
  12. class UnicodeMethodsTest(unittest.TestCase):
  13. # update this, if the database changes
  14. expectedchecksum = '6ec65b65835614ec00634c674bba0e50cd32c189'
  15. def test_method_checksum(self):
  16. h = hashlib.sha1()
  17. for i in range(65536):
  18. char = unichr(i)
  19. data = [
  20. # Predicates (single char)
  21. u"01"[char.isalnum()],
  22. u"01"[char.isalpha()],
  23. u"01"[char.isdecimal()],
  24. u"01"[char.isdigit()],
  25. u"01"[char.islower()],
  26. u"01"[char.isnumeric()],
  27. u"01"[char.isspace()],
  28. u"01"[char.istitle()],
  29. u"01"[char.isupper()],
  30. # Predicates (multiple chars)
  31. u"01"[(char + u'abc').isalnum()],
  32. u"01"[(char + u'abc').isalpha()],
  33. u"01"[(char + u'123').isdecimal()],
  34. u"01"[(char + u'123').isdigit()],
  35. u"01"[(char + u'abc').islower()],
  36. u"01"[(char + u'123').isnumeric()],
  37. u"01"[(char + u' \t').isspace()],
  38. u"01"[(char + u'abc').istitle()],
  39. u"01"[(char + u'ABC').isupper()],
  40. # Mappings (single char)
  41. char.lower(),
  42. char.upper(),
  43. char.title(),
  44. # Mappings (multiple chars)
  45. (char + u'abc').lower(),
  46. (char + u'ABC').upper(),
  47. (char + u'abc').title(),
  48. (char + u'ABC').title(),
  49. ]
  50. h.update(u''.join(data).encode(encoding))
  51. result = h.hexdigest()
  52. self.assertEqual(result, self.expectedchecksum)
  53. class UnicodeDatabaseTest(unittest.TestCase):
  54. def setUp(self):
  55. # In case unicodedata is not available, this will raise an ImportError,
  56. # but the other test cases will still be run
  57. import unicodedata
  58. self.db = unicodedata
  59. def tearDown(self):
  60. del self.db
  61. class UnicodeFunctionsTest(UnicodeDatabaseTest):
  62. # update this, if the database changes
  63. expectedchecksum = '3136d5afd787dc2bcb1bdcac95e385349fbebbca'
  64. def test_function_checksum(self):
  65. data = []
  66. h = hashlib.sha1()
  67. for i in range(0x10000):
  68. char = unichr(i)
  69. data = [
  70. # Properties
  71. str(self.db.digit(char, -1)),
  72. str(self.db.numeric(char, -1)),
  73. str(self.db.decimal(char, -1)),
  74. self.db.category(char),
  75. self.db.bidirectional(char),
  76. self.db.decomposition(char),
  77. str(self.db.mirrored(char)),
  78. str(self.db.combining(char)),
  79. ]
  80. h.update(''.join(data))
  81. result = h.hexdigest()
  82. self.assertEqual(result, self.expectedchecksum)
  83. def test_digit(self):
  84. self.assertEqual(self.db.digit(u'A', None), None)
  85. self.assertEqual(self.db.digit(u'9'), 9)
  86. self.assertEqual(self.db.digit(u'\u215b', None), None)
  87. self.assertEqual(self.db.digit(u'\u2468'), 9)
  88. self.assertEqual(self.db.digit(u'\U00020000', None), None)
  89. self.assertRaises(TypeError, self.db.digit)
  90. self.assertRaises(TypeError, self.db.digit, u'xx')
  91. self.assertRaises(ValueError, self.db.digit, u'x')
  92. def test_numeric(self):
  93. self.assertEqual(self.db.numeric(u'A',None), None)
  94. self.assertEqual(self.db.numeric(u'9'), 9)
  95. self.assertEqual(self.db.numeric(u'\u215b'), 0.125)
  96. self.assertEqual(self.db.numeric(u'\u2468'), 9.0)
  97. self.assertEqual(self.db.numeric(u'\U00020000', None), None)
  98. self.assertRaises(TypeError, self.db.numeric)
  99. self.assertRaises(TypeError, self.db.numeric, u'xx')
  100. self.assertRaises(ValueError, self.db.numeric, u'x')
  101. def test_decimal(self):
  102. self.assertEqual(self.db.decimal(u'A',None), None)
  103. self.assertEqual(self.db.decimal(u'9'), 9)
  104. self.assertEqual(self.db.decimal(u'\u215b', None), None)
  105. self.assertEqual(self.db.decimal(u'\u2468', None), None)
  106. self.assertEqual(self.db.decimal(u'\U00020000', None), None)
  107. self.assertRaises(TypeError, self.db.decimal)
  108. self.assertRaises(TypeError, self.db.decimal, u'xx')
  109. self.assertRaises(ValueError, self.db.decimal, u'x')
  110. def test_category(self):
  111. self.assertEqual(self.db.category(u'\uFFFE'), 'Cn')
  112. self.assertEqual(self.db.category(u'a'), 'Ll')
  113. self.assertEqual(self.db.category(u'A'), 'Lu')
  114. self.assertEqual(self.db.category(u'\U00020000'), 'Lo')
  115. self.assertRaises(TypeError, self.db.category)
  116. self.assertRaises(TypeError, self.db.category, u'xx')
  117. def test_bidirectional(self):
  118. self.assertEqual(self.db.bidirectional(u'\uFFFE'), '')
  119. self.assertEqual(self.db.bidirectional(u' '), 'WS')
  120. self.assertEqual(self.db.bidirectional(u'A'), 'L')
  121. self.assertEqual(self.db.bidirectional(u'\U00020000'), 'L')
  122. self.assertRaises(TypeError, self.db.bidirectional)
  123. self.assertRaises(TypeError, self.db.bidirectional, u'xx')
  124. def test_decomposition(self):
  125. self.assertEqual(self.db.decomposition(u'\uFFFE'),'')
  126. self.assertEqual(self.db.decomposition(u'\u00bc'), '<fraction> 0031 2044 0034')
  127. self.assertRaises(TypeError, self.db.decomposition)
  128. self.assertRaises(TypeError, self.db.decomposition, u'xx')
  129. def test_mirrored(self):
  130. self.assertEqual(self.db.mirrored(u'\uFFFE'), 0)
  131. self.assertEqual(self.db.mirrored(u'a'), 0)
  132. self.assertEqual(self.db.mirrored(u'\u2201'), 1)
  133. self.assertEqual(self.db.mirrored(u'\U00020000'), 0)
  134. self.assertRaises(TypeError, self.db.mirrored)
  135. self.assertRaises(TypeError, self.db.mirrored, u'xx')
  136. def test_combining(self):
  137. self.assertEqual(self.db.combining(u'\uFFFE'), 0)
  138. self.assertEqual(self.db.combining(u'a'), 0)
  139. self.assertEqual(self.db.combining(u'\u20e1'), 230)
  140. self.assertEqual(self.db.combining(u'\U00020000'), 0)
  141. self.assertRaises(TypeError, self.db.combining)
  142. self.assertRaises(TypeError, self.db.combining, u'xx')
  143. def test_normalize(self):
  144. self.assertRaises(TypeError, self.db.normalize)
  145. self.assertRaises(ValueError, self.db.normalize, 'unknown', u'xx')
  146. self.assertEqual(self.db.normalize('NFKC', u''), u'')
  147. # The rest can be found in test_normalization.py
  148. # which requires an external file.
  149. def test_east_asian_width(self):
  150. eaw = self.db.east_asian_width
  151. self.assertRaises(TypeError, eaw, 'a')
  152. self.assertRaises(TypeError, eaw, u'')
  153. self.assertRaises(TypeError, eaw, u'ra')
  154. self.assertEqual(eaw(u'\x1e'), 'N')
  155. self.assertEqual(eaw(u'\x20'), 'Na')
  156. self.assertEqual(eaw(u'\uC894'), 'W')
  157. self.assertEqual(eaw(u'\uFF66'), 'H')
  158. self.assertEqual(eaw(u'\uFF1F'), 'F')
  159. self.assertEqual(eaw(u'\u2010'), 'A')
  160. self.assertEqual(eaw(u'\U00020000'), 'W')
  161. class UnicodeMiscTest(UnicodeDatabaseTest):
  162. def test_failed_import_during_compiling(self):
  163. # Issue 4367
  164. # Decoding \N escapes requires the unicodedata module. If it can't be
  165. # imported, we shouldn't segfault.
  166. # This program should raise a SyntaxError in the eval.
  167. code = "import sys;" \
  168. "sys.modules['unicodedata'] = None;" \
  169. """eval("u'\N{SOFT HYPHEN}'")"""
  170. args = [sys.executable, "-c", code]
  171. # We use a subprocess because the unicodedata module may already have
  172. # been loaded in this process.
  173. popen = subprocess.Popen(args, stderr=subprocess.PIPE)
  174. popen.wait()
  175. self.assertEqual(popen.returncode, 1)
  176. error = "SyntaxError: (unicode error) \N escapes not supported " \
  177. "(can't load unicodedata module)"
  178. self.assertTrue(error in popen.stderr.read())
  179. def test_decimal_numeric_consistent(self):
  180. # Test that decimal and numeric are consistent,
  181. # i.e. if a character has a decimal value,
  182. # its numeric value should be the same.
  183. count = 0
  184. for i in xrange(0x10000):
  185. c = unichr(i)
  186. dec = self.db.decimal(c, -1)
  187. if dec != -1:
  188. self.assertEqual(dec, self.db.numeric(c))
  189. count += 1
  190. self.assert_(count >= 10) # should have tested at least the ASCII digits
  191. def test_digit_numeric_consistent(self):
  192. # Test that digit and numeric are consistent,
  193. # i.e. if a character has a digit value,
  194. # its numeric value should be the same.
  195. count = 0
  196. for i in xrange(0x10000):
  197. c = unichr(i)
  198. dec = self.db.digit(c, -1)
  199. if dec != -1:
  200. self.assertEqual(dec, self.db.numeric(c))
  201. count += 1
  202. self.assert_(count >= 10) # should have tested at least the ASCII digits
  203. def test_bug_1704793(self):
  204. self.assertEquals(self.db.lookup("GOTHIC LETTER FAIHU"), u'\U00010346')
  205. def test_ucd_510(self):
  206. import unicodedata
  207. # In UCD 5.1.0, a mirrored property changed wrt. UCD 3.2.0
  208. self.assert_(unicodedata.mirrored(u"\u0f3a"))
  209. self.assert_(not unicodedata.ucd_3_2_0.mirrored(u"\u0f3a"))
  210. # Also, we now have two ways of representing
  211. # the upper-case mapping: as delta, or as absolute value
  212. self.assert_(u"a".upper()==u'A')
  213. self.assert_(u"\u1d79".upper()==u'\ua77d')
  214. self.assert_(u".".upper()==u".")
  215. def test_bug_5828(self):
  216. self.assertEqual(u"\u1d79".lower(), u"\u1d79")
  217. # Only U+0000 should have U+0000 as its upper/lower/titlecase variant
  218. self.assertEqual(
  219. [
  220. c for c in range(sys.maxunicode+1)
  221. if u"\x00" in unichr(c).lower()+unichr(c).upper()+unichr(c).title()
  222. ],
  223. [0]
  224. )
  225. def test_bug_4971(self):
  226. # LETTER DZ WITH CARON: DZ, Dz, dz
  227. self.assertEqual(u"\u01c4".title(), u"\u01c5")
  228. self.assertEqual(u"\u01c5".title(), u"\u01c5")
  229. self.assertEqual(u"\u01c6".title(), u"\u01c5")
  230. def test_main():
  231. test.test_support.run_unittest(
  232. UnicodeMiscTest,
  233. UnicodeMethodsTest,
  234. UnicodeFunctionsTest
  235. )
  236. if __name__ == "__main__":
  237. test_main()