Skip to content

Commit

Permalink
assertion: _notin_text: handle non-printable characters
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Feb 20, 2020
1 parent 0de3525 commit f7c1873
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
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

0 comments on commit f7c1873

Please sign in to comment.