1# To fully test this module, we would need a copy of the stringprep tables. 2# Since we don't have them, this test checks only a few codepoints. 3 4import unittest 5from test import test_support 6 7from stringprep import * 8 9class StringprepTests(unittest.TestCase): 10 def test(self): 11 self.assertTrue(in_table_a1(u"\u0221")) 12 self.assertFalse(in_table_a1(u"\u0222")) 13 14 self.assertTrue(in_table_b1(u"\u00ad")) 15 self.assertFalse(in_table_b1(u"\u00ae")) 16 17 self.assertTrue(map_table_b2(u"\u0041"), u"\u0061") 18 self.assertTrue(map_table_b2(u"\u0061"), u"\u0061") 19 20 self.assertTrue(map_table_b3(u"\u0041"), u"\u0061") 21 self.assertTrue(map_table_b3(u"\u0061"), u"\u0061") 22 23 self.assertTrue(in_table_c11(u"\u0020")) 24 self.assertFalse(in_table_c11(u"\u0021")) 25 26 self.assertTrue(in_table_c12(u"\u00a0")) 27 self.assertFalse(in_table_c12(u"\u00a1")) 28 29 self.assertTrue(in_table_c12(u"\u00a0")) 30 self.assertFalse(in_table_c12(u"\u00a1")) 31 32 self.assertTrue(in_table_c11_c12(u"\u00a0")) 33 self.assertFalse(in_table_c11_c12(u"\u00a1")) 34 35 self.assertTrue(in_table_c21(u"\u001f")) 36 self.assertFalse(in_table_c21(u"\u0020")) 37 38 self.assertTrue(in_table_c22(u"\u009f")) 39 self.assertFalse(in_table_c22(u"\u00a0")) 40 41 self.assertTrue(in_table_c21_c22(u"\u009f")) 42 self.assertFalse(in_table_c21_c22(u"\u00a0")) 43 44 self.assertTrue(in_table_c3(u"\ue000")) 45 self.assertFalse(in_table_c3(u"\uf900")) 46 47 self.assertTrue(in_table_c4(u"\uffff")) 48 self.assertFalse(in_table_c4(u"\u0000")) 49 50 self.assertTrue(in_table_c5(u"\ud800")) 51 self.assertFalse(in_table_c5(u"\ud7ff")) 52 53 self.assertTrue(in_table_c6(u"\ufff9")) 54 self.assertFalse(in_table_c6(u"\ufffe")) 55 56 self.assertTrue(in_table_c7(u"\u2ff0")) 57 self.assertFalse(in_table_c7(u"\u2ffc")) 58 59 self.assertTrue(in_table_c8(u"\u0340")) 60 self.assertFalse(in_table_c8(u"\u0342")) 61 62 # C.9 is not in the bmp 63 # self.assertTrue(in_table_c9(u"\U000E0001")) 64 # self.assertFalse(in_table_c8(u"\U000E0002")) 65 66 self.assertTrue(in_table_d1(u"\u05be")) 67 self.assertFalse(in_table_d1(u"\u05bf")) 68 69 self.assertTrue(in_table_d2(u"\u0041")) 70 self.assertFalse(in_table_d2(u"\u0040")) 71 72 # This would generate a hash of all predicates. However, running 73 # it is quite expensive, and only serves to detect changes in the 74 # unicode database. Instead, stringprep.py asserts the version of 75 # the database. 76 77 # import hashlib 78 # predicates = [k for k in dir(stringprep) if k.startswith("in_table")] 79 # predicates.sort() 80 # for p in predicates: 81 # f = getattr(stringprep, p) 82 # # Collect all BMP code points 83 # data = ["0"] * 0x10000 84 # for i in range(0x10000): 85 # if f(unichr(i)): 86 # data[i] = "1" 87 # data = "".join(data) 88 # h = hashlib.sha1() 89 # h.update(data) 90 # print p, h.hexdigest() 91 92def test_main(): 93 test_support.run_unittest(StringprepTests) 94 95if __name__ == '__main__': 96 test_main() 97