def test_group_with_value(ingroup, outgroup): if not ingroup: return the_re = re.compile(r'(.*)%s' % outgroup) reversed_re = unmatcher.reverse(the_re, ingroup) match = the_re.match(reversed_re) assert bool(match) assert match.groups() == (ingroup,)
def test_group_condition_with_value(condgroup, yesgroup): if not condgroup: return the_re = re.compile('(%s)?((?(1)%s))' % (condgroup, yesgroup)) reversed_re = unmatcher.reverse(the_re, condgroup) match = the_re.match(reversed_re) assert bool(match) assert match.groups() in [(condgroup, yesgroup), (None, "")]
def test_group_with_value(ingroup, outgroup): if not ingroup: return the_re = re.compile(r'(.*)%s' % outgroup) reversed_re = unmatcher.reverse(the_re, ingroup) match = the_re.match(reversed_re) assert bool(match) assert match.groups() == (ingroup, )
def test_group_with_backrefs(ingroup, outgroup): if not ingroup: return the_re = re.compile(r'(%s)%s\1' % (ingroup, outgroup)) reversed_re = unmatcher.reverse(the_re) match = the_re.match(reversed_re) assert bool(match) assert match.groups() == (ingroup, )
def test_noncapture_group(ingroup, outgroup): if not ingroup: return the_re = re.compile('(?:%s)%s' % (ingroup, outgroup)) reversed_re = unmatcher.reverse(the_re) match = the_re.match(reversed_re) assert bool(match) assert len(match.groups()) == 0
def test_group_with_backrefs(ingroup, outgroup): if not ingroup: return the_re = re.compile(r'(%s)%s\1' % (ingroup, outgroup)) reversed_re = unmatcher.reverse(the_re) match = the_re.match(reversed_re) assert bool(match) assert match.groups() == (ingroup,)
def test_named_group_sans_backrefs(groupname, ingroup, outgroup): if not (groupname and ingroup): return groupname = 'a' + groupname # cannot start with digit the_re = re.compile('(?P<%s>%s)%s' % (groupname, ingroup, outgroup)) reversed_re = unmatcher.reverse(the_re) match = the_re.match(reversed_re) assert bool(match) assert match.groupdict() == {groupname: ingroup}
def test_named_group_with_value(groupname, ingroup, outgroup): if not (groupname and ingroup): return groupname = 'a' + groupname # cannot start with digit the_re = re.compile(r'(?P<%s>.*)%s' % (groupname, outgroup)) reversed_re = unmatcher.reverse(the_re, **{groupname: ingroup}) match = the_re.match(reversed_re) assert bool(match) assert match.groupdict() == {groupname: ingroup}
def test_repeat_symbols(symbol, char): repeat_re = re.compile('%s%s' % (re.escape(char), symbol)) reversed_repeat = unmatcher.reverse(repeat_re) assert bool(repeat_re.match(reversed_repeat)) if len(reversed_repeat) == 0: assert symbol == '*', "empty string for repeater other than `*'" else: for chunk in chunks(reversed_repeat, len(char)): assert char == chunk
def test_named_group_condition_with_value(groupname, condgroup, yesgroup): if not condgroup: return groupname = 'a' + groupname # cannot start with digit the_re = re.compile( '(?P<{groupname}>{condgroup})?((?({groupname}){yesgroup}))'.format( **locals())) reversed_re = unmatcher.reverse(the_re, **{groupname: condgroup}) match = the_re.match(reversed_re) assert bool(match) assert (match.group(groupname), match.group(2)) in [(condgroup, yesgroup), (None, "")]
def test_named_group_condition_with_value(groupname, condgroup, yesgroup): if not condgroup: return groupname = 'a' + groupname # cannot start with digit the_re = re.compile( '(?P<{groupname}>{condgroup})?((?({groupname}){yesgroup}))' .format(**locals())) reversed_re = unmatcher.reverse(the_re, **{groupname: condgroup}) match = the_re.match(reversed_re) assert bool(match) assert (match.group(groupname), match.group(2)) in [(condgroup, yesgroup), (None, "")]
def test_repeat_range(lower_bound, upper_bound, char): if lower_bound == upper_bound: # test exact "range": foo{3} repeat_re = re.compile('%s{%d}' % (re.escape(char), lower_bound)) elif lower_bound < upper_bound: # test closed range: foo{2,4} repeat_re = re.compile('%s{%d,%d}' % (re.escape(char), lower_bound, upper_bound)) elif lower_bound > upper_bound: # test "infinite" range: foo{4,} lower_bound = min((lower_bound, upper_bound)) repeat_re = re.compile('%s{%d,}' % (re.escape(char), lower_bound)) else: pytest.fail() # FSM help you if that actually happens reversed_repeat = unmatcher.reverse(repeat_re) assert bool(repeat_re.match(reversed_repeat)) for chunk in chunks(reversed_repeat, len(char)): assert char == chunk
def test_not_literal(char): assert char != unmatcher.reverse('[^%s]' % re.escape(char))
def test_charset_negate_class(class_): charset_re = re.compile(r'\%s' % class_.upper()) reversed_charset = unmatcher.reverse(charset_re) assert bool(charset_re.match(reversed_charset))
def test_explicitly_unsupported_cases(case): """Explicitly unsupported cases should not fail with just generic error.""" node_type, regex = case with pytest.raises(NotImplementedError) as e: unmatcher.reverse(regex) assert (": " + node_type) not in str(e)
def test_charset_negate_range(minchar, maxchar): minchar, maxchar = min(minchar, maxchar), max(minchar, maxchar) charset_re = re.compile('[^%s-%s]' % (minchar, maxchar)) reversed_charset = unmatcher.reverse(charset_re) assert reversed_charset < minchar or maxchar < reversed_charset
def test_branch(left, right): branch_re = re.compile('|'.join(map(re.escape, (left, right)))) reversed_branch = unmatcher.reverse(branch_re) assert reversed_branch in (left, right)
def test_unicode_literal(phrase): assert phrase == unmatcher.reverse(re.escape(phrase))
def test_literal__ignorecase(expr): literal_re = re.compile(expr, re.IGNORECASE) reversed_re = unmatcher.reverse(literal_re) assert expr.lower() == reversed_re.lower()
def test_literal(expr): assert expr == unmatcher.reverse(re.escape(expr))
def test_any(_): dot_re = re.compile('.') reversed_dot = unmatcher.reverse(dot_re) assert bool(dot_re.match(reversed_dot)) assert reversed_dot != "\n"
def test_not_literal__ignore_case(char): literal_re = re.compile('[^%s]' % re.escape(char), re.IGNORECASE) reversed_re = unmatcher.reverse(literal_re) assert reversed_re not in (char.lower(), char.upper())
def test_any__dotall(_): dot_re = re.compile('.', re.DOTALL) reversed_dot = unmatcher.reverse(dot_re) assert bool(dot_re.match(reversed_dot))
def test_charset_literal(chars): if not chars: return charset_re = re.compile('[%s]' % chars) reversed_charset = unmatcher.reverse(charset_re) assert reversed_charset in chars
def test_charset_range(minchar, maxchar): minchar, maxchar = min(minchar, maxchar), max(minchar, maxchar) charset_re = re.compile('[%s-%s]' % (minchar, maxchar)) reversed_charset = unmatcher.reverse(charset_re) assert minchar <= reversed_charset <= maxchar