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