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

_compare_eq_sequence: align index diff for longer values #218

Merged
merged 2 commits into from
Feb 13, 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
16 changes: 13 additions & 3 deletions src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,19 @@ def _compare_eq_sequence(
left_value = left[i]
right_value = right[i]

explanation += [
"At index {} diff: {!r} != {!r}".format(i, left_value, right_value)
]
left_repr = repr(left_value)
right_repr = repr(right_value)
gets_full_diff = verbose > 0 # via _compare_eq_iterable later.
if not gets_full_diff and len(left_repr) > 10 and len(right_repr) > 10:
explanation += [
"At index {} diff:".format(i),
"{} !=".format(left_repr),
"{}".format(right_repr),
]
else:
explanation += [
"At index {} diff: {} != {}".format(i, left_repr, right_repr)
]
break

if comparing_bytes:
Expand Down
20 changes: 20 additions & 0 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,26 @@ def test_mojibake(self):
msg = "\n".join(expl)
assert msg

def test_compare_eq_sequence_aligns_long_index_diff(self):
diff = callequal(["a" * 40], ["a" * 20 + "x" + "a" * 19], verbose=0)
assert diff == [
"['aaaaaaaaaaa...aaaaaaaaaaaa'] == ['aaaaaaaaaaa...aaaaaaaaaaaa']",
"At index 0 diff:",
"'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' !=",
"'aaaaaaaaaaaaaaaaaaaaxaaaaaaaaaaaaaaaaaaa'",
"Use -v to get the full diff",
]
diff = callequal(["a" * 40], ["a" * 20 + "x" + "a" * 19], verbose=1)
assert diff == [
"['aaaaaaaaaaa...aaaaaaaaaaaa'] == ['aaaaaaaaaaa...aaaaaaaaaaaa']",
"At index 0 diff: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' != 'aaaaaaaaaaaaaaaaaaaaxaaaaaaaaaaaaaaaaaaa'",
"Full diff:",
"- ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']",
"? ^",
"+ ['aaaaaaaaaaaaaaaaaaaaxaaaaaaaaaaaaaaaaaaa']",
"? ^",
]


class TestAssert_reprcompare_dataclass:
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+")
Expand Down