Exemplo n.º 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,)
Exemplo n.º 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, "")]
Exemplo n.º 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, )
Exemplo n.º 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, "")]
Exemplo n.º 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, )
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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,)
Exemplo n.º 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}
Exemplo n.º 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}
Exemplo n.º 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}
Exemplo n.º 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}
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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, "")]
Exemplo n.º 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, "")]
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 19
0
def test_not_literal(char):
    assert char != unmatcher.reverse('[^%s]' % re.escape(char))
Exemplo n.º 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))
Exemplo n.º 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)
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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)
Exemplo n.º 25
0
def test_unicode_literal(phrase):
    assert phrase == unmatcher.reverse(re.escape(phrase))
Exemplo n.º 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()
Exemplo n.º 27
0
def test_unicode_literal(phrase):
    assert phrase == unmatcher.reverse(re.escape(phrase))
Exemplo n.º 28
0
def test_literal(expr):
    assert expr == unmatcher.reverse(re.escape(expr))
Exemplo n.º 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"
Exemplo n.º 30
0
def test_literal(expr):
    assert expr == unmatcher.reverse(re.escape(expr))
Exemplo n.º 31
0
def test_not_literal(char):
    assert char != unmatcher.reverse('[^%s]' % re.escape(char))
Exemplo n.º 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))
Exemplo n.º 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())
Exemplo n.º 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"
Exemplo 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)
Exemplo n.º 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))
Exemplo n.º 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()
Exemplo n.º 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)
Exemplo n.º 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())
Exemplo n.º 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
Exemplo n.º 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))
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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