Merge pull request #5495 from nicoddemus/blueyed/cmp-bytes
Improve comparison of byte strings (supersedes #5267)
This commit is contained in:
commit
3f3f3e7a29
|
@ -0,0 +1,17 @@
|
|||
Improved comparison of byte strings.
|
||||
|
||||
When comparing bytes, the assertion message used to show the byte numeric value when showing the differences::
|
||||
|
||||
def test():
|
||||
> assert b'spam' == b'eggs'
|
||||
E AssertionError: assert b'spam' == b'eggs'
|
||||
E At index 0 diff: 115 != 101
|
||||
E Use -v to get the full diff
|
||||
|
||||
It now shows the actual ascii representation instead, which is often more useful::
|
||||
|
||||
def test():
|
||||
> assert b'spam' == b'eggs'
|
||||
E AssertionError: assert b'spam' == b'eggs'
|
||||
E At index 0 diff: b's' != b'e'
|
||||
E Use -v to get the full diff
|
|
@ -254,17 +254,38 @@ def _compare_eq_iterable(left, right, verbose=0):
|
|||
|
||||
|
||||
def _compare_eq_sequence(left, right, verbose=0):
|
||||
comparing_bytes = isinstance(left, bytes) and isinstance(right, bytes)
|
||||
explanation = []
|
||||
len_left = len(left)
|
||||
len_right = len(right)
|
||||
for i in range(min(len_left, len_right)):
|
||||
if left[i] != right[i]:
|
||||
if comparing_bytes:
|
||||
# when comparing bytes, we want to see their ascii representation
|
||||
# instead of their numeric values (#5260)
|
||||
# using a slice gives us the ascii representation:
|
||||
# >>> s = b'foo'
|
||||
# >>> s[0]
|
||||
# 102
|
||||
# >>> s[0:1]
|
||||
# b'f'
|
||||
left_value = left[i : i + 1]
|
||||
right_value = right[i : i + 1]
|
||||
else:
|
||||
left_value = left[i]
|
||||
right_value = right[i]
|
||||
|
||||
explanation += [
|
||||
"At index {} diff: {!r} != {!r}".format(i, left[i], right[i])
|
||||
"At index {} diff: {!r} != {!r}".format(i, left_value, right_value)
|
||||
]
|
||||
break
|
||||
len_diff = len_left - len_right
|
||||
|
||||
if comparing_bytes:
|
||||
# when comparing bytes, it doesn't help to show the "sides contain one or more items"
|
||||
# longer explanation, so skip it
|
||||
return explanation
|
||||
|
||||
len_diff = len_left - len_right
|
||||
if len_diff:
|
||||
if len_diff > 0:
|
||||
dir_with_more = "Left"
|
||||
|
|
|
@ -331,6 +331,27 @@ class TestAssert_reprcompare:
|
|||
assert "- spam" in diff
|
||||
assert "+ eggs" in diff
|
||||
|
||||
def test_bytes_diff_normal(self):
|
||||
"""Check special handling for bytes diff (#5260)"""
|
||||
diff = callequal(b"spam", b"eggs")
|
||||
|
||||
assert diff == [
|
||||
"b'spam' == b'eggs'",
|
||||
"At index 0 diff: b's' != b'e'",
|
||||
"Use -v to get the full diff",
|
||||
]
|
||||
|
||||
def test_bytes_diff_verbose(self):
|
||||
"""Check special handling for bytes diff (#5260)"""
|
||||
diff = callequal(b"spam", b"eggs", verbose=True)
|
||||
assert diff == [
|
||||
"b'spam' == b'eggs'",
|
||||
"At index 0 diff: b's' != b'e'",
|
||||
"Full diff:",
|
||||
"- b'spam'",
|
||||
"+ b'eggs'",
|
||||
]
|
||||
|
||||
def test_list(self):
|
||||
expl = callequal([0, 1], [0, 2])
|
||||
assert len(expl) > 1
|
||||
|
|
Loading…
Reference in New Issue