This commit is contained in:
OlegP-andrew 2024-06-18 18:11:07 +02:00 committed by GitHub
commit cac0241917
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 0 deletions

View File

@ -306,6 +306,7 @@ Nico Vidal
Nikolay Kondratyev
Nipunn Koorapati
Oleg Pidsadnyi
Oleg Plisov
Oleg Sushchenko
Olga Matoula
Oliver Bestwalter

View File

@ -0,0 +1 @@
Terminal outputting string difference will now escape whitespace characters if those are the difference

View File

@ -309,10 +309,22 @@ def _diff_text(left: str, right: str, verbose: int = 0) -> List[str]:
left = left[:-i]
right = right[:-i]
keepends = True
diffspace = False
for i in range(min(len(left), len(right))):
if left[i] != right[i]:
if left[i].isspace() and right[i].isspace():
diffspace = True # Check if difference in strings is whitespace symbol
break
if left.isspace() or right.isspace():
left = repr(str(left))
right = repr(str(right))
explanation += ["Strings contain only whitespace, escaping them using repr()"]
elif diffspace:
left = repr(str(left))
right = repr(str(right))
explanation += [
"Strings are different by whitespaces, escaping them using repr()"
]
# "right" is the expected base against which we compare "left",
# see https://github.com/pytest-dev/pytest/issues/3333
explanation += [

View File

@ -282,6 +282,20 @@ TESTCASES = [
""",
id="Compare attrs classes",
),
pytest.param(
"""
def test_this():
assert "word\t" == "word "
""",
"""
> assert "word\t" == "word "
E AssertionError: assert 'word\\t' == 'word '
E Strings are different by whitespaces, escaping them using repr()
E - 'word '
E + 'word\\t'
""",
id="Compare whitespaces",
),
]