test_stringprep.py 3.12 KB
Newer Older
Martin v. Löwis's avatar
Martin v. Löwis committed
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 code points.
Martin v. Löwis's avatar
Martin v. Löwis committed
3

4
import unittest
5
from test import support
Martin v. Löwis's avatar
Martin v. Löwis committed
6 7 8

from stringprep import *

9 10
class StringprepTests(unittest.TestCase):
    def test(self):
11 12
        self.assertTrue(in_table_a1("\u0221"))
        self.assertFalse(in_table_a1("\u0222"))
Martin v. Löwis's avatar
Martin v. Löwis committed
13

14 15
        self.assertTrue(in_table_b1("\u00ad"))
        self.assertFalse(in_table_b1("\u00ae"))
Martin v. Löwis's avatar
Martin v. Löwis committed
16

17 18
        self.assertTrue(map_table_b2("\u0041"), "\u0061")
        self.assertTrue(map_table_b2("\u0061"), "\u0061")
Martin v. Löwis's avatar
Martin v. Löwis committed
19

20 21
        self.assertTrue(map_table_b3("\u0041"), "\u0061")
        self.assertTrue(map_table_b3("\u0061"), "\u0061")
Martin v. Löwis's avatar
Martin v. Löwis committed
22

23 24
        self.assertTrue(in_table_c11("\u0020"))
        self.assertFalse(in_table_c11("\u0021"))
Martin v. Löwis's avatar
Martin v. Löwis committed
25

26 27
        self.assertTrue(in_table_c12("\u00a0"))
        self.assertFalse(in_table_c12("\u00a1"))
Martin v. Löwis's avatar
Martin v. Löwis committed
28

29 30
        self.assertTrue(in_table_c12("\u00a0"))
        self.assertFalse(in_table_c12("\u00a1"))
Martin v. Löwis's avatar
Martin v. Löwis committed
31

32 33
        self.assertTrue(in_table_c11_c12("\u00a0"))
        self.assertFalse(in_table_c11_c12("\u00a1"))
Martin v. Löwis's avatar
Martin v. Löwis committed
34

35 36
        self.assertTrue(in_table_c21("\u001f"))
        self.assertFalse(in_table_c21("\u0020"))
Martin v. Löwis's avatar
Martin v. Löwis committed
37

38 39
        self.assertTrue(in_table_c22("\u009f"))
        self.assertFalse(in_table_c22("\u00a0"))
Martin v. Löwis's avatar
Martin v. Löwis committed
40

41 42
        self.assertTrue(in_table_c21_c22("\u009f"))
        self.assertFalse(in_table_c21_c22("\u00a0"))
Martin v. Löwis's avatar
Martin v. Löwis committed
43

44 45
        self.assertTrue(in_table_c3("\ue000"))
        self.assertFalse(in_table_c3("\uf900"))
Martin v. Löwis's avatar
Martin v. Löwis committed
46

47 48
        self.assertTrue(in_table_c4("\uffff"))
        self.assertFalse(in_table_c4("\u0000"))
Martin v. Löwis's avatar
Martin v. Löwis committed
49

50 51
        self.assertTrue(in_table_c5("\ud800"))
        self.assertFalse(in_table_c5("\ud7ff"))
Martin v. Löwis's avatar
Martin v. Löwis committed
52

53 54
        self.assertTrue(in_table_c6("\ufff9"))
        self.assertFalse(in_table_c6("\ufffe"))
Martin v. Löwis's avatar
Martin v. Löwis committed
55

56 57
        self.assertTrue(in_table_c7("\u2ff0"))
        self.assertFalse(in_table_c7("\u2ffc"))
Martin v. Löwis's avatar
Martin v. Löwis committed
58

59 60
        self.assertTrue(in_table_c8("\u0340"))
        self.assertFalse(in_table_c8("\u0342"))
Martin v. Löwis's avatar
Martin v. Löwis committed
61

62
        # C.9 is not in the bmp
63 64
        # self.assertTrue(in_table_c9(u"\U000E0001"))
        # self.assertFalse(in_table_c8(u"\U000E0002"))
Martin v. Löwis's avatar
Martin v. Löwis committed
65

66 67
        self.assertTrue(in_table_d1("\u05be"))
        self.assertFalse(in_table_d1("\u05bf"))
Martin v. Löwis's avatar
Martin v. Löwis committed
68

69 70
        self.assertTrue(in_table_d2("\u0041"))
        self.assertFalse(in_table_d2("\u0040"))
Martin v. Löwis's avatar
Martin v. Löwis committed
71

72 73 74 75
        # This would generate a hash of all predicates. However, running
        # it is quite expensive, and only serves to detect changes in the
        # unicode database. Instead, stringprep.py asserts the version of
        # the database.
Martin v. Löwis's avatar
Martin v. Löwis committed
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        # import hashlib
        # predicates = [k for k in dir(stringprep) if k.startswith("in_table")]
        # predicates.sort()
        # for p in predicates:
        #     f = getattr(stringprep, p)
        #     # Collect all BMP code points
        #     data = ["0"] * 0x10000
        #     for i in range(0x10000):
        #         if f(unichr(i)):
        #             data[i] = "1"
        #     data = "".join(data)
        #     h = hashlib.sha1()
        #     h.update(data)
        #     print p, h.hexdigest()

def test_main():
93
    support.run_unittest(StringprepTests)
94 95 96

if __name__ == '__main__':
    test_main()