Skip to content

Commit 44e9f68

Browse files
committed
Also highlight comparisons between strings
Fixes #13175
1 parent ef07401 commit 44e9f68

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

changelog/13175.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The diff is now also highlighted correctly when comparing two strings.

src/_pytest/assertion/util.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ def __call__(self, source: str, lexer: Literal["diff", "python"] = "python") ->
4343
"""Apply highlighting to the given source."""
4444

4545

46+
def dummy_highlighter(source: str, lexer: Literal["diff", "python"] = "python") -> str:
47+
"""Dummy highlighter that returns the text unprocessed.
48+
49+
Needed for _notin_text, as the diff gets post-processed to only show the "+" part.
50+
"""
51+
return source
52+
53+
4654
def format_explanation(explanation: str) -> str:
4755
r"""Format an explanation.
4856
@@ -242,7 +250,7 @@ def _compare_eq_any(
242250
) -> list[str]:
243251
explanation = []
244252
if istext(left) and istext(right):
245-
explanation = _diff_text(left, right, verbose)
253+
explanation = _diff_text(left, right, highlighter, verbose)
246254
else:
247255
from _pytest.python_api import ApproxBase
248256

@@ -274,7 +282,9 @@ def _compare_eq_any(
274282
return explanation
275283

276284

277-
def _diff_text(left: str, right: str, verbose: int = 0) -> list[str]:
285+
def _diff_text(
286+
left: str, right: str, highlighter: _HighlightFunc, verbose: int = 0
287+
) -> list[str]:
278288
"""Return the explanation for the diff between text.
279289
280290
Unless --verbose is used this will skip leading and trailing
@@ -315,10 +325,15 @@ def _diff_text(left: str, right: str, verbose: int = 0) -> list[str]:
315325
explanation += ["Strings contain only whitespace, escaping them using repr()"]
316326
# "right" is the expected base against which we compare "left",
317327
# see https://github.com/pytest-dev/pytest/issues/3333
318-
explanation += [
319-
line.strip("\n")
320-
for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
321-
]
328+
explanation.extend(
329+
highlighter(
330+
"\n".join(
331+
line.strip("\n")
332+
for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
333+
),
334+
lexer="diff",
335+
).splitlines()
336+
)
322337
return explanation
323338

324339

@@ -586,7 +601,7 @@ def _notin_text(term: str, text: str, verbose: int = 0) -> list[str]:
586601
head = text[:index]
587602
tail = text[index + len(term) :]
588603
correct_text = head + tail
589-
diff = _diff_text(text, correct_text, verbose)
604+
diff = _diff_text(text, correct_text, dummy_highlighter, verbose)
590605
newdiff = [f"{saferepr(term, maxsize=42)} is contained here:"]
591606
for line in diff:
592607
if line.startswith("Skipping"):

testing/test_assertion.py

+10
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,16 @@ def test():
20192019
"{bold}{red}E {light-green}+ 'number-is-5': 5,{hl-reset}{endline}{reset}",
20202020
],
20212021
),
2022+
(
2023+
"""
2024+
def test():
2025+
assert "abcd" == "abce"
2026+
""",
2027+
[
2028+
"{bold}{red}E {reset}{light-red}- abce{hl-reset}{endline}{reset}",
2029+
"{bold}{red}E {light-green}+ abcd{hl-reset}{endline}{reset}",
2030+
],
2031+
),
20222032
),
20232033
)
20242034
def test_comparisons_handle_colors(

0 commit comments

Comments
 (0)