1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
import re
import pytest
from re_assert import Matches
def test_typeerror_equality_different_type():
with pytest.raises(TypeError):
Matches('foo') == b'foo'
def test_matches_repr_plain():
assert repr(Matches('^foo')) == "Matches('^foo')"
def test_matches_repr_with_flags():
ret = repr(Matches('^foo', re.I))
assert ret == "Matches('^foo', flags=regex.I | regex.V0)"
def test_repr_with_failure():
obj = Matches('^foo')
assert obj != 'fa'
assert repr(obj) == (
"Matches('^foo')\n"
' # regex failed to match at:\n'
' #\n'
' #> fa\n'
' # ^'
)
def test_assert_success():
obj = Matches('foo')
assert obj == 'food'
obj.assert_matches('food')
def test_fail_at_beginning():
with pytest.raises(AssertionError) as excinfo:
Matches('foo').assert_matches('bar')
msg, = excinfo.value.args
assert msg == (
' regex failed to match at:\n'
'\n'
'> bar\n'
' ^'
)
def test_fail_at_end_of_line():
with pytest.raises(AssertionError) as excinfo:
Matches('foo').assert_matches('fo')
msg, = excinfo.value.args
assert msg == (
' regex failed to match at:\n'
'\n'
'> fo\n'
' ^'
)
def test_fail_multiple_lines():
with pytest.raises(AssertionError) as excinfo:
Matches('foo.bar', re.DOTALL).assert_matches('foo\nbr')
msg, = excinfo.value.args
assert msg == (
' regex failed to match at:\n'
'\n'
'> foo\n'
'> br\n'
' ^'
)
def test_fail_end_of_line_with_newline():
with pytest.raises(AssertionError) as excinfo:
Matches('foo.bar', re.DOTALL).assert_matches('foo\n')
msg, = excinfo.value.args
assert msg == (
' regex failed to match at:\n'
'\n'
'> foo\n'
'>\n'
' ^'
)
def test_fail_at_end_of_line_mismatching_newline():
with pytest.raises(AssertionError) as excinfo:
Matches('foo.', re.DOTALL).assert_matches('foo')
msg, = excinfo.value.args
assert msg == (
' regex failed to match at:\n'
'\n'
'> foo\n'
' ^'
)
def test_match_with_tabs():
with pytest.raises(AssertionError) as excinfo:
Matches('f.o.o').assert_matches('f\to\tx\n')
msg, = excinfo.value.args
assert msg == (
' regex failed to match at:\n'
'\n'
'> f\to\tx\n'
' \t \t^'
)
def test_from_pattern():
pattern = re.compile('^foo', flags=re.I)
assert Matches.from_pattern(pattern) == 'FOO'
|