def test_flag_ignorecase(self): rgx_ci = cffi_re2.compile(r'a(b+)$', flags=cffi_re2.IGNORECASE) rgx_cs = cffi_re2.compile(r'a(b+)$') # Check case sensitive assert_is_none(rgx_cs.match("AB")) assert_is_none(rgx_cs.match("Ab")) assert_is_none(rgx_cs.match("aB")) assert_is_none(rgx_cs.match("aBb")) assert_is_none(rgx_cs.match("abB")) assert_is_not_none(rgx_cs.match("ab")) assert_is_not_none(rgx_cs.match("abb")) # Check case insensitive assert_is_not_none(rgx_ci.match("AB")) assert_is_not_none(rgx_ci.match("Ab")) assert_is_not_none(rgx_ci.match("aB")) assert_is_not_none(rgx_ci.match("aBb")) assert_is_not_none(rgx_ci.match("abB")) assert_is_not_none(rgx_ci.match("ab")) assert_is_not_none(rgx_ci.match("abb")) # Official example assert_equal( cffi_re2.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=cffi_re2.IGNORECASE), 'Baked Beans & Spam')
def test_module_level_functions(self): """ Quick test of module-level functions. These are generally expected to call the compiled counterparts, so these tests do not check all aspects """ assert_equal(cffi_re2.findall(r'a(b+)', "abbcdefabbbbca"), ["bb", "bbbb"]) assert_equal(cffi_re2.sub(r'b+', '', 'abbcbbd'), 'acd') assert_is_not_none(cffi_re2.search(r'b+', 'abbcbbd')) assert_is_none(cffi_re2.match(r'b+', 'abbcbbd')) assert_is_not_none(cffi_re2.match(r'b+', 'bbbbb'))
def test_medium_complexity(self): """Check medium complexity regexes""" # Examples from github.com/ulikoehler/KATranslationCheck # 1 rgx = cffi_re2.compile(r"\b[Ii]nto\b") assert_is_not_none(rgx.search("Into the darkness")) assert_is_not_none(rgx.search("I went into the darkness")) assert_is_none(rgx.search("abcde beintoaqe aqet")) # 2 rgx = cffi_re2.compile(r"\d+\$\s*dollars?") assert_is_not_none(rgx.search("12$ dollars")) assert_is_not_none(rgx.match("12$ dollars")) assert_is_not_none(rgx.match("1$ dollar")) assert_is_not_none(rgx.match("1$ dollar")) assert_is_not_none(rgx.match("1$ dollars"))
def test_flag_ignorecase(self): rgx_ci = cffi_re2.compile(r'a(b+)$', flags=cffi_re2.IGNORECASE) rgx_cs = cffi_re2.compile(r'a(b+)$') # Check case sensitive assert_is_none(rgx_cs.match("AB")) assert_is_none(rgx_cs.match("Ab")) assert_is_none(rgx_cs.match("aB")) assert_is_none(rgx_cs.match("aBb")) assert_is_none(rgx_cs.match("abB")) assert_is_not_none(rgx_cs.match("ab")) assert_is_not_none(rgx_cs.match("abb")) # Check case insensitive assert_is_not_none(rgx_ci.match("AB")) assert_is_not_none(rgx_ci.match("Ab")) assert_is_not_none(rgx_ci.match("aB")) assert_is_not_none(rgx_ci.match("aBb")) assert_is_not_none(rgx_ci.match("abB")) assert_is_not_none(rgx_ci.match("ab")) assert_is_not_none(rgx_ci.match("abb")) # Official example assert_equal(cffi_re2.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=cffi_re2.IGNORECASE), 'Baked Beans & Spam')
def test_basic_match(self): # Search-type regex should NOT match full string robj = cffi_re2.compile(r'b+') assert_is_none(robj.match('abbcd')) # This regex only matches the left end robj = cffi_re2.compile(r'[abc]+$') assert_is_none(robj.match('abbcd')) # Full match regex should match robj = cffi_re2.compile(r'[abcd]+') assert_is_not_none(robj.match('abbcd')) # Regex match should be left-anchored, not both-anchored robj = cffi_re2.compile(r'a+') assert_is_not_none(robj.match('aaab')) assert_is_none(robj.match('baaab'))
def test_optional_groups(self): result = cffi_re2.search(r"(foo)?bar", "bar") print(result.ranges) assert_equal(result.group(0), "bar") assert_is_none(result.group(1)) assert_equal(result.groups(), (None, ))
def test_more_x(self): assert_is_none(cffi_re2.search(r"<(a|span|div|table)", "Kapazitäten")) assert_equal(cffi_re2.findall(r"<(a|span|div|table)", "Kapazitäten"), [])
def test_optional_groups(self): result = cffi_re2.search(r"(foo)?bar", "bar") print(result.ranges) assert_equal(result.group(0), "bar") assert_is_none(result.group(1)) assert_equal(result.groups(), (None,))