0001 """ Test script for the unicodedata module. 0002 0003 Written by Marc-Andre Lemburg (mal@lemburg.com). 0004 0005 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. 0006 0007 """#" 0008 import unittest, test.test_support 0009 import sha 0010 0011 encoding = 'utf-8' 0012 0013 0014 ### Run tests 0015 0016 class UnicodeMethodsTest(unittest.TestCase): 0017 0018 # update this, if the database changes 0019 expectedchecksum = 'a37276dc2c158bef6dfd908ad34525c97180fad9' 0020 0021 def test_method_checksum(self): 0022 h = sha.sha() 0023 for i in range(65536): 0024 char = unichr(i) 0025 data = [ 0026 # Predicates (single char) 0027 u"01"[char.isalnum()], 0028 u"01"[char.isalpha()], 0029 u"01"[char.isdecimal()], 0030 u"01"[char.isdigit()], 0031 u"01"[char.islower()], 0032 u"01"[char.isnumeric()], 0033 u"01"[char.isspace()], 0034 u"01"[char.istitle()], 0035 u"01"[char.isupper()], 0036 0037 # Predicates (multiple chars) 0038 u"01"[(char + u'abc').isalnum()], 0039 u"01"[(char + u'abc').isalpha()], 0040 u"01"[(char + u'123').isdecimal()], 0041 u"01"[(char + u'123').isdigit()], 0042 u"01"[(char + u'abc').islower()], 0043 u"01"[(char + u'123').isnumeric()], 0044 u"01"[(char + u' \t').isspace()], 0045 u"01"[(char + u'abc').istitle()], 0046 u"01"[(char + u'ABC').isupper()], 0047 0048 # Mappings (single char) 0049 char.lower(), 0050 char.upper(), 0051 char.title(), 0052 0053 # Mappings (multiple chars) 0054 (char + u'abc').lower(), 0055 (char + u'ABC').upper(), 0056 (char + u'abc').title(), 0057 (char + u'ABC').title(), 0058 0059 ] 0060 h.update(u''.join(data).encode(encoding)) 0061 result = h.hexdigest() 0062 self.assertEqual(result, self.expectedchecksum) 0063 0064 class UnicodeDatabaseTest(unittest.TestCase): 0065 0066 def setUp(self): 0067 # In case unicodedata is not available, this will raise an ImportError, 0068 # but the other test cases will still be run 0069 import unicodedata 0070 self.db = unicodedata 0071 0072 def tearDown(self): 0073 del self.db 0074 0075 class UnicodeFunctionsTest(UnicodeDatabaseTest): 0076 0077 # update this, if the database changes 0078 expectedchecksum = 'cfe20a967a450ebc82ca68c3e4eed344164e11af' 0079 0080 def test_function_checksum(self): 0081 data = [] 0082 h = sha.sha() 0083 0084 for i in range(0x10000): 0085 char = unichr(i) 0086 data = [ 0087 # Properties 0088 str(self.db.digit(char, -1)), 0089 str(self.db.numeric(char, -1)), 0090 str(self.db.decimal(char, -1)), 0091 self.db.category(char), 0092 self.db.bidirectional(char), 0093 self.db.decomposition(char), 0094 str(self.db.mirrored(char)), 0095 str(self.db.combining(char)), 0096 ] 0097 h.update(''.join(data)) 0098 result = h.hexdigest() 0099 self.assertEqual(result, self.expectedchecksum) 0100 0101 def test_digit(self): 0102 self.assertEqual(self.db.digit(u'A', None), None) 0103 self.assertEqual(self.db.digit(u'9'), 9) 0104 self.assertEqual(self.db.digit(u'\u215b', None), None) 0105 self.assertEqual(self.db.digit(u'\u2468'), 9) 0106 0107 self.assertRaises(TypeError, self.db.digit) 0108 self.assertRaises(TypeError, self.db.digit, u'xx') 0109 self.assertRaises(ValueError, self.db.digit, u'x') 0110 0111 def test_numeric(self): 0112 self.assertEqual(self.db.numeric(u'A',None), None) 0113 self.assertEqual(self.db.numeric(u'9'), 9) 0114 self.assertEqual(self.db.numeric(u'\u215b'), 0.125) 0115 self.assertEqual(self.db.numeric(u'\u2468'), 9.0) 0116 0117 self.assertRaises(TypeError, self.db.numeric) 0118 self.assertRaises(TypeError, self.db.numeric, u'xx') 0119 self.assertRaises(ValueError, self.db.numeric, u'x') 0120 0121 def test_decimal(self): 0122 self.assertEqual(self.db.decimal(u'A',None), None) 0123 self.assertEqual(self.db.decimal(u'9'), 9) 0124 self.assertEqual(self.db.decimal(u'\u215b', None), None) 0125 self.assertEqual(self.db.decimal(u'\u2468', None), None) 0126 0127 self.assertRaises(TypeError, self.db.decimal) 0128 self.assertRaises(TypeError, self.db.decimal, u'xx') 0129 self.assertRaises(ValueError, self.db.decimal, u'x') 0130 0131 def test_category(self): 0132 self.assertEqual(self.db.category(u'\uFFFE'), 'Cn') 0133 self.assertEqual(self.db.category(u'a'), 'Ll') 0134 self.assertEqual(self.db.category(u'A'), 'Lu') 0135 0136 self.assertRaises(TypeError, self.db.category) 0137 self.assertRaises(TypeError, self.db.category, u'xx') 0138 0139 def test_bidirectional(self): 0140 self.assertEqual(self.db.bidirectional(u'\uFFFE'), '') 0141 self.assertEqual(self.db.bidirectional(u' '), 'WS') 0142 self.assertEqual(self.db.bidirectional(u'A'), 'L') 0143 0144 self.assertRaises(TypeError, self.db.bidirectional) 0145 self.assertRaises(TypeError, self.db.bidirectional, u'xx') 0146 0147 def test_decomposition(self): 0148 self.assertEqual(self.db.decomposition(u'\uFFFE'),'') 0149 self.assertEqual(self.db.decomposition(u'\u00bc'), '<fraction> 0031 2044 0034') 0150 0151 self.assertRaises(TypeError, self.db.decomposition) 0152 self.assertRaises(TypeError, self.db.decomposition, u'xx') 0153 0154 def test_mirrored(self): 0155 self.assertEqual(self.db.mirrored(u'\uFFFE'), 0) 0156 self.assertEqual(self.db.mirrored(u'a'), 0) 0157 self.assertEqual(self.db.mirrored(u'\u2201'), 1) 0158 0159 self.assertRaises(TypeError, self.db.mirrored) 0160 self.assertRaises(TypeError, self.db.mirrored, u'xx') 0161 0162 def test_combining(self): 0163 self.assertEqual(self.db.combining(u'\uFFFE'), 0) 0164 self.assertEqual(self.db.combining(u'a'), 0) 0165 self.assertEqual(self.db.combining(u'\u20e1'), 230) 0166 0167 self.assertRaises(TypeError, self.db.combining) 0168 self.assertRaises(TypeError, self.db.combining, u'xx') 0169 0170 def test_normalize(self): 0171 self.assertRaises(TypeError, self.db.normalize) 0172 self.assertRaises(ValueError, self.db.normalize, 'unknown', u'xx') 0173 self.assertEqual(self.db.normalize('NFKC', u''), u'') 0174 # The rest can be found in test_normalization.py 0175 # which requires an external file. 0176 0177 def test_east_asian_width(self): 0178 eaw = self.db.east_asian_width 0179 self.assertRaises(TypeError, eaw, 'a') 0180 self.assertRaises(TypeError, eaw, u'') 0181 self.assertRaises(TypeError, eaw, u'ra') 0182 self.assertEqual(eaw(u'\x1e'), 'N') 0183 self.assertEqual(eaw(u'\x20'), 'Na') 0184 self.assertEqual(eaw(u'\uC894'), 'W') 0185 self.assertEqual(eaw(u'\uFF66'), 'H') 0186 self.assertEqual(eaw(u'\uFF1F'), 'F') 0187 self.assertEqual(eaw(u'\u2010'), 'A') 0188 0189 class UnicodeMiscTest(UnicodeDatabaseTest): 0190 0191 def test_decimal_numeric_consistent(self): 0192 # Test that decimal and numeric are consistent, 0193 # i.e. if a character has a decimal value, 0194 # it's numeric value should be the same. 0195 count = 0 0196 for i in xrange(0x10000): 0197 c = unichr(i) 0198 dec = self.db.decimal(c, -1) 0199 if dec != -1: 0200 self.assertEqual(dec, self.db.numeric(c)) 0201 count += 1 0202 self.assert_(count >= 10) # should have tested at least the ASCII digits 0203 0204 def test_digit_numeric_consistent(self): 0205 # Test that digit and numeric are consistent, 0206 # i.e. if a character has a digit value, 0207 # it's numeric value should be the same. 0208 count = 0 0209 for i in xrange(0x10000): 0210 c = unichr(i) 0211 dec = self.db.digit(c, -1) 0212 if dec != -1: 0213 self.assertEqual(dec, self.db.numeric(c)) 0214 count += 1 0215 self.assert_(count >= 10) # should have tested at least the ASCII digits 0216 0217 def test_main(): 0218 test.test_support.run_unittest( 0219 UnicodeMiscTest, 0220 UnicodeMethodsTest, 0221 UnicodeFunctionsTest 0222 ) 0223 0224 if __name__ == "__main__": 0225 test_main() 0226
Generated by PyXR 0.9.4