Esempio n. 1
0
 def test_subtraction(self):
     c = unicode5.CharClass("abc")
     c.subtract_char("b")
     result = self.class_test(c)
     self.assertTrue(result == "ac", "subtract_char: %s" % result)
     c.subtract_range("b", "d")
     self.assertTrue(self.class_test(c) == "a", "subtract_range")
     tests = [[[], [], ""], [[['a', 'b']], [['c', 'd']], "ab"],
              [[['a', 'b']], [['b', 'c']], "a"],
              [[['a', 'c']], [['b', 'd']], "a"],
              [[['a', 'd']], [['b', 'd']], "a"],
              [[['a', 'd']], [['b', 'c']], "ad"],
              [[['a', 'c']], [['a', 'd']], ""],
              [[['a', 'c']], [['a', 'c']], ""],
              [[['a', 'd']], [['a', 'b']], "cd"],
              [[['b', 'c']], [['a', 'd']], ""],
              [[['b', 'c']], [['a', 'c']], ""],
              [[['b', 'd']], [['a', 'c']], "d"],
              [[['a', 'z']], [['f', 'h'], ['s', 'u']],
               "abcdeijklmnopqrvwxyz"],
              [[['a', 'e'], ['i', 'r'], ['v', 'z']], [['m', 'x']],
               "abcdeijklyz"]]
     for test in tests:
         c1 = unicode5.CharClass(*test[0])
         c2 = unicode5.CharClass(*test[1])
         c3 = unicode5.CharClass(c1)
         c3.subtract_class(c2)
         result = self.class_test(c3)
         self.assertTrue(
             result == test[2],
             "Subtract: %s - %s, found %s" % (repr(c1), repr(c2), repr(c3)))
Esempio n. 2
0
 def test_add(self):
     c = unicode5.CharClass(u"ac")
     c.add_char(u"b")
     self.assertTrue(self.class_test(c) == "abc", "add_char")
     c.add_range(u"b", u"e")
     self.assertTrue(self.class_test(c) == "abcde", "add_range")
     c.add_class(unicode5.CharClass(["m", "s"]))
     self.assertTrue(self.class_test(c) == "abcdemnopqrs", "add_class")
Esempio n. 3
0
 def test_representation(self):
     repr_tests = [
         [[], "CharClass()", ""],
         [[['a', 'z']],
          "CharClass((u'a',u'z'))" if py2 else "CharClass(('a','z'))",
          "a-z"],
         [[['a', 'd'], ['f', 'k']], "CharClass((u'a',u'd'), (u'f',u'k'))"
          if py2 else "CharClass(('a','d'), ('f','k'))", "a-df-k"],
         [[['-', '-']], "CharClass(u'-')" if py2 else "CharClass('-')",
          "\\-"],
         [[['[', ']']],
          "CharClass((u'[',u']'))" if py2 else "CharClass(('[',']'))",
          "[-\\]"],
         [[['\\', '\\']],
          "CharClass(u'\\\\')" if py2 else "CharClass('\\\\')", "\\\\"],
     ]
     for test in repr_tests:
         c = unicode5.CharClass(*test[0])
         self.assertTrue(
             repr(c) == test[1],
             "CharClass repr test: expected %s, found %s" %
             (test[1], repr(c)))
         result = c.format_re()
         self.assertTrue(
             result == test[2],
             "CharClass Re test: expected %s, found %s" % (test[2], result))
Esempio n. 4
0
 def test_negate_char_class(self):
     """Check the Negation function"""
     min_char = character(0)
     max_char = character(maxunicode)
     char_class_tests = [[[], [[min_char, max_char]]],
                         [[['b', 'c']], [[min_char, 'a'], ['d', max_char]]],
                         [[['b', 'c'], ['e', 'f']],
                          [[min_char, 'a'], ['d', 'd'], ['g', max_char]]]]
     for test in char_class_tests:
         c1 = unicode5.CharClass(*test[0])
         c2 = unicode5.CharClass(c1)
         c2.negate()
         c3 = unicode5.CharClass(*test[1])
         self.assertTrue(
             c2 == c3, "%s negated to %s, expected %s" %
             (repr(c1), repr(c2), repr(c3)))
         c2.negate()
         self.assertTrue(c2 == c1,
                         "%s double negation got %s" % (repr(c1), repr(c2)))
Esempio n. 5
0
 def test_complex_constructors(self):
     init_tests = [
         [[], ""],
         [[['a', 'z']], "abcdefghijklmnopqrstuvwxyz"],
         [[['a', 'd'], ['f', 'k']], "abcdfghijk"],
         [[['b', 'b']], "b"],
         [[['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'j'],
           ['k', 'k']], "abcdefghijk"],
         [[['a', 'b'], ['d', 'f'], ['h', 'h']], "abdefh"],
         [[['h', 'h'], ['d', 'f'], ['a', 'b']], "abdefh"],
     ]
     for test in init_tests:
         c = unicode5.CharClass(*test[0])
         result = self.class_test(c)
         self.assertTrue(
             result == test[1],
             "CharClass test: expected %s, found %s" % (test[1], result))
Esempio n. 6
0
 def test_constructor(self):
     c = unicode5.CharClass()
     if MAX_CHAR < 0x10FFFF:
         logging.warn("unicode5 tests truncated to character(0x%X) by "
                      "narrow python build" % MAX_CHAR)
     for code in range3(MAX_CHAR + 1):
         self.assertFalse(c.test(character(code)))
     c = unicode5.CharClass('a')
     self.assertTrue(self.class_test(c) == 'a')
     c = unicode5.CharClass(('a', 'z'))
     self.assertTrue(self.class_test(c) == 'abcdefghijklmnopqrstuvwxyz')
     c = unicode5.CharClass('abcxyz')
     self.assertTrue(
         len(c.ranges) == 2, "No range optimization: %s" % repr(c.ranges))
     self.assertTrue(self.class_test(c) == 'abcxyz')
     cc = unicode5.CharClass(c)
     self.assertTrue(self.class_test(cc) == 'abcxyz')
     c = unicode5.CharClass(('a', 'c'), ('e', 'g'), 'd')
     self.assertTrue(
         len(c.ranges) == 1,
         "Missing range optimization: %s" % repr(c.ranges))