Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertion: _notin_text: handle non-printable characters #239

Merged
merged 1 commit into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,21 +498,31 @@ def _compare_eq_cls(


def _notin_text(term: str, text: str, verbose: int = 0) -> List[str]:
from wcwidth import wcwidth

index = text.find(term)
head = text[:index]
tail = text[index + len(term) :]
correct_text = head + tail
diff = _diff_text(correct_text, text, verbose)

newdiff = ["%s is contained here:" % saferepr(term, maxsize=42)]
for line in diff:
if line.startswith("Skipping"):
continue
if line.startswith("- "):
continue
if line.startswith("+ "):
newdiff.append(" " + line[2:])
else:
newdiff.append(line)
if any(
wcwidth(ch) <= 0
for ch in [ch for lines in [term, correct_text] for ch in lines]
):
newdiff = [
"NOTE: Strings contain non-printable characters. Escaping them using repr()."
] + newdiff
text = repr(text)
indent = " " * (index + 1)
marker = "+" * (len(repr(term)) - 2)
else:
indent = " " * index
marker = "+" * len(term)
newdiff += [
" " + text,
"? " + indent + marker,
]
return newdiff


Expand Down
10 changes: 10 additions & 0 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,16 @@ def test_reprcompare_notin() -> None:
]


def test_reprcompare_notin_newline() -> None:
assert callop("not in", "foo\n", "aaafoo\nbbb") == [
r"'foo\n' not in 'aaafoo\nbbb'",
r"NOTE: Strings contain non-printable characters. Escaping them using repr().",
r"'foo\n' is contained here:",
r" 'aaafoo\nbbb'",
r"? +++++",
]


def test_reprcompare_whitespaces():
config = mock_config()
detail = plugin.pytest_assertrepr_compare(config, "==", "\r\n", "\n")
Expand Down