Improve quoting in raises match failure message (#5553)

Improve quoting in raises match failure message
This commit is contained in:
Bruno Oliveira 2019-07-04 10:25:27 -03:00 committed by GitHub
commit 95824c588a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 2 deletions

View File

@ -0,0 +1 @@
Improve quoting in ``raises`` match failure message.

View File

@ -544,7 +544,7 @@ class ExceptionInfo:
""" """
__tracebackhide__ = True __tracebackhide__ = True
if not re.search(regexp, str(self.value)): if not re.search(regexp, str(self.value)):
assert 0, "Pattern '{!s}' not found in '{!s}'".format(regexp, self.value) assert 0, "Pattern {!r} not found in {!r}".format(regexp, str(self.value))
return True return True

View File

@ -220,13 +220,20 @@ class TestRaises:
int("asdf") int("asdf")
msg = "with base 16" msg = "with base 16"
expr = r"Pattern '{}' not found in 'invalid literal for int\(\) with base 10: 'asdf''".format( expr = r"Pattern '{}' not found in \"invalid literal for int\(\) with base 10: 'asdf'\"".format(
msg msg
) )
with pytest.raises(AssertionError, match=expr): with pytest.raises(AssertionError, match=expr):
with pytest.raises(ValueError, match=msg): with pytest.raises(ValueError, match=msg):
int("asdf", base=10) int("asdf", base=10)
def test_match_failure_string_quoting(self):
with pytest.raises(AssertionError) as excinfo:
with pytest.raises(AssertionError, match="'foo"):
raise AssertionError("'bar")
msg, = excinfo.value.args
assert msg == 'Pattern "\'foo" not found in "\'bar"'
def test_raises_match_wrong_type(self): def test_raises_match_wrong_type(self):
"""Raising an exception with the wrong type and match= given. """Raising an exception with the wrong type and match= given.