示例#1
0
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,)
示例#2
0
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, "")]
示例#3
0
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, )
示例#4
0
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, "")]
示例#5
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, )
示例#6
0
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
示例#7
0
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
示例#8
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,)
示例#9
0
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}
示例#10
0
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}
示例#11
0
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}
示例#12
0
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}
示例#13
0
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
示例#14
0
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
示例#15
0
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, "")]
示例#16
0
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, "")]
示例#17
0
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
示例#18
0
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
示例#19
0
def test_not_literal(char):
    assert char != unmatcher.reverse('[^%s]' % re.escape(char))
示例#20
0
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))
示例#21
0
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)
示例#22
0
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
示例#23
0
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
示例#24
0
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)
示例#25
0
def test_unicode_literal(phrase):
    assert phrase == unmatcher.reverse(re.escape(phrase))
示例#26
0
def test_literal__ignorecase(expr):
    literal_re = re.compile(expr, re.IGNORECASE)
    reversed_re = unmatcher.reverse(literal_re)
    assert expr.lower() == reversed_re.lower()
示例#27
0
def test_unicode_literal(phrase):
    assert phrase == unmatcher.reverse(re.escape(phrase))
示例#28
0
def test_literal(expr):
    assert expr == unmatcher.reverse(re.escape(expr))
示例#29
0
def test_any(_):
    dot_re = re.compile('.')
    reversed_dot = unmatcher.reverse(dot_re)
    assert bool(dot_re.match(reversed_dot))
    assert reversed_dot != "\n"
示例#30
0
def test_literal(expr):
    assert expr == unmatcher.reverse(re.escape(expr))
示例#31
0
def test_not_literal(char):
    assert char != unmatcher.reverse('[^%s]' % re.escape(char))
示例#32
0
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))
示例#33
0
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())
示例#34
0
def test_any(_):
    dot_re = re.compile('.')
    reversed_dot = unmatcher.reverse(dot_re)
    assert bool(dot_re.match(reversed_dot))
    assert reversed_dot != "\n"
示例#35
0
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)
示例#36
0
def test_any__dotall(_):
    dot_re = re.compile('.', re.DOTALL)
    reversed_dot = unmatcher.reverse(dot_re)
    assert bool(dot_re.match(reversed_dot))
示例#37
0
def test_literal__ignorecase(expr):
    literal_re = re.compile(expr, re.IGNORECASE)
    reversed_re = unmatcher.reverse(literal_re)
    assert expr.lower() == reversed_re.lower()
示例#38
0
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)
示例#39
0
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())
示例#40
0
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
示例#41
0
def test_any__dotall(_):
    dot_re = re.compile('.', re.DOTALL)
    reversed_dot = unmatcher.reverse(dot_re)
    assert bool(dot_re.match(reversed_dot))
示例#42
0
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
示例#43
0
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
示例#44
0
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