Show bytes ascii representation instead of numeric value
This commit is contained in:
parent
8c7eb82363
commit
3f2344e8f7
|
@ -1 +1,17 @@
|
|||
Improve/fix comparison of byte strings with Python 3.
|
||||
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,35 @@ 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
|
||||
|
||||
if isinstance(left, bytes):
|
||||
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
|
||||
|
|
|
@ -331,17 +331,26 @@ class TestAssert_reprcompare:
|
|||
assert "- spam" in diff
|
||||
assert "+ eggs" in diff
|
||||
|
||||
def test_bytes_diff(self):
|
||||
def test_bytes_diff_normal(self):
|
||||
"""Check special handling for bytes diff (#5260)"""
|
||||
diff = callequal(b"spam", b"eggs")
|
||||
if PY3:
|
||||
assert diff == [
|
||||
"b'spam' == b'eggs'",
|
||||
"At index 0 diff: 115 != 101",
|
||||
"Use -v to get the full diff",
|
||||
]
|
||||
else:
|
||||
# Handled as text on Python 2.
|
||||
assert diff == ["'spam' == 'eggs'", "- spam", "+ 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])
|
||||
|
|
Loading…
Reference in New Issue