| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from collections.abc import Iterator |
| 4 |
|
| 5 |
from _pytest._io.saferepr import saferepr |
| 6 |
from _pytest.assertion._typing import _AssertionTextDiffStyle |
| 7 |
from _pytest.assertion._typing import _HighlightFunc |
| 8 |
from _pytest.assertion.highlight import dummy_highlighter |
| 9 |
from _pytest.compat import assert_never |
| 10 |
|
| 11 |
|
| 12 |
def _compare_eq_text( |
| 13 |
left: str, |
| 14 |
right: str, |
| 15 |
highlighter: _HighlightFunc, |
| 16 |
verbose: int, |
| 17 |
assertion_text_diff_style: _AssertionTextDiffStyle, |
| 18 |
) -> Iterator[str]: |
| 19 |
match assertion_text_diff_style: |
| 20 |
case "block": |
| 21 |
yield from _diff_text_block(left, right) |
| 22 |
case "ndiff": |
| 23 |
yield from _diff_text(left, right, highlighter, verbose) |
| 24 |
case unreachable: |
| 25 |
assert_never(unreachable) |
| 26 |
|
| 27 |
|
| 28 |
def _diff_text_block(left: str, right: str) -> Iterator[str]: |
| 29 |
yield "Left:" |
| 30 |
yield from _format_text_block_lines(left) |
| 31 |
yield "" |
| 32 |
yield "Right:" |
| 33 |
yield from _format_text_block_lines(right) |
| 34 |
|
| 35 |
|
| 36 |
def _format_text_block_lines(text: str) -> Iterator[str]: |
| 37 |
for line in text.split("\n"): |
| 38 |
yield f" {line}" |
| 39 |
|
| 40 |
|
| 41 |
def _diff_text( |
| 42 |
left: str, right: str, highlighter: _HighlightFunc, verbose: int = 0 |
| 43 |
) -> Iterator[str]: |
| 44 |
"""Yield the explanation for the diff between text. |
| 45 |
|
| 46 |
Unless --verbose is used this will skip leading and trailing |
| 47 |
characters which are identical to keep the diff minimal. |
| 48 |
""" |
| 49 |
from difflib import ndiff |
| 50 |
|
| 51 |
if verbose < 1: |
| 52 |
i = 0 # just in case left or right has zero length |
| 53 |
for i in range(min(len(left), len(right))): |
| 54 |
if left[i] != right[i]: |
| 55 |
break |
| 56 |
if i > 42: |
| 57 |
i -= 10 # Provide some context |
| 58 |
yield f"Skipping {i} identical leading characters in diff, use -v to show" |
| 59 |
left = left[i:] |
| 60 |
right = right[i:] |
| 61 |
if len(left) == len(right): |
| 62 |
for i in range(len(left)): |
| 63 |
if left[-i] != right[-i]: |
| 64 |
break |
| 65 |
if i > 42: |
| 66 |
i -= 10 # Provide some context |
| 67 |
yield ( |
| 68 |
f"Skipping {i} identical trailing " |
| 69 |
"characters in diff, use -v to show" |
| 70 |
) |
| 71 |
left = left[:-i] |
| 72 |
right = right[:-i] |
| 73 |
keepends = True |
| 74 |
if left.isspace() or right.isspace(): |
| 75 |
left = repr(str(left)) |
| 76 |
right = repr(str(right)) |
| 77 |
yield "Strings contain only whitespace, escaping them using repr()" |
| 78 |
# "right" is the expected base against which we compare "left", |
| 79 |
# see https://github.com/pytest-dev/pytest/issues/3333 |
| 80 |
yield from highlighter( |
| 81 |
"\n".join( |
| 82 |
line.strip("\n") |
| 83 |
for line in ndiff(right.splitlines(keepends), left.splitlines(keepends)) |
| 84 |
), |
| 85 |
lexer="diff", |
| 86 |
).splitlines() |
| 87 |
|
| 88 |
|
| 89 |
def _notin_text(term: str, text: str, verbose: int = 0) -> Iterator[str]: |
| 90 |
index = text.find(term) |
| 91 |
head = text[:index] |
| 92 |
tail = text[index + len(term) :] |
| 93 |
correct_text = head + tail |
| 94 |
diff = _diff_text(text, correct_text, dummy_highlighter, verbose) |
| 95 |
yield f"{saferepr(term, maxsize=42)} is contained here:" |
| 96 |
for line in diff: |
| 97 |
if line.startswith("Skipping"): |
| 98 |
continue |
| 99 |
if line.startswith("- "): |
| 100 |
continue |
| 101 |
if line.startswith("+ "): |
| 102 |
yield " " + line[2:] |
| 103 |
else: |
| 104 |
yield line |