Show full repr with assert a==b and -vv

This commit is contained in:
Oscar Benjamin 2019-01-08 01:25:05 +00:00
parent a4c426b1a8
commit 85055a9efe
4 changed files with 40 additions and 0 deletions

View File

@ -176,6 +176,7 @@ Oliver Bestwalter
Omar Kohl Omar Kohl
Omer Hadari Omer Hadari
Ondřej Súkup Ondřej Súkup
Oscar Benjamin
Patrick Hayes Patrick Hayes
Paweł Adamczak Paweł Adamczak
Pedro Algarvio Pedro Algarvio

View File

@ -0,0 +1 @@
Show full repr with ``assert a==b`` and ``-vv``.

View File

@ -151,6 +151,8 @@ def assertrepr_compare(config, op, left, right):
elif type(left) == type(right) and (isdatacls(left) or isattrs(left)): elif type(left) == type(right) and (isdatacls(left) or isattrs(left)):
type_fn = (isdatacls, isattrs) type_fn = (isdatacls, isattrs)
explanation = _compare_eq_cls(left, right, verbose, type_fn) explanation = _compare_eq_cls(left, right, verbose, type_fn)
elif verbose:
explanation = _compare_eq_verbose(left, right)
if isiterable(left) and isiterable(right): if isiterable(left) and isiterable(right):
expl = _compare_eq_iterable(left, right, verbose) expl = _compare_eq_iterable(left, right, verbose)
if explanation is not None: if explanation is not None:
@ -236,6 +238,18 @@ def _diff_text(left, right, verbose=False):
return explanation return explanation
def _compare_eq_verbose(left, right):
keepends = True
left_lines = repr(left).splitlines(keepends)
right_lines = repr(right).splitlines(keepends)
explanation = []
explanation += [u"-" + line for line in left_lines]
explanation += [u"+" + line for line in right_lines]
return explanation
def _compare_eq_iterable(left, right, verbose=False): def _compare_eq_iterable(left, right, verbose=False):
if not verbose: if not verbose:
return [u"Use -v to get the full diff"] return [u"Use -v to get the full diff"]

View File

@ -488,6 +488,30 @@ class TestAssert_reprcompare(object):
expl = callequal([(1, 2)], []) expl = callequal([(1, 2)], [])
assert len(expl) > 1 assert len(expl) > 1
def test_repr_verbose(self):
class Nums:
def __init__(self, nums):
self.nums = nums
def __repr__(self):
return str(self.nums)
list_x = list(range(5000))
list_y = list(range(5000))
list_y[len(list_y) // 2] = 3
nums_x = Nums(list_x)
nums_y = Nums(list_y)
assert callequal(nums_x, nums_y) is None
expl = callequal(nums_x, nums_y, verbose=1)
assert "-" + repr(nums_x) in expl
assert "+" + repr(nums_y) in expl
expl = callequal(nums_x, nums_y, verbose=2)
assert "-" + repr(nums_x) in expl
assert "+" + repr(nums_y) in expl
def test_list_bad_repr(self): def test_list_bad_repr(self):
class A(object): class A(object):
def __repr__(self): def __repr__(self):