fix and test a unbound local in _diff_text of the assertion plugin

--HG--
branch : trunk
This commit is contained in:
Ronny Pfannschmidt 2010-10-09 07:35:28 +02:00
parent 6b0db18eca
commit 09a9ce1da1
2 changed files with 17 additions and 1 deletions

View File

@ -75,8 +75,12 @@ def pytest_assertrepr_compare(op, left, right):
except py.builtin._sysex:
raise
except:
excinfo = py.code.ExceptionInfo()
explanation = ['(pytest_assertion plugin: representation of '
'details failed. Probably an object has a faulty __repr__.)']
'details failed. Probably an object has a faulty __repr__.)',
str(excinfo)
]
if not explanation:
return None
@ -95,6 +99,7 @@ def _diff_text(left, right):
identical to keep the diff minimal.
"""
explanation = []
i = 0 # just in case left or right has zero length
for i in range(min(len(left), len(right))):
if left[i] != right[i]:
break

View File

@ -103,6 +103,17 @@ class TestAssert_reprcompare:
expl = callequal({}, {'1': A()})
assert 'faulty' in "".join(expl)
def test_one_repr_empty(self):
"""
the faulty empty string repr did trigger
a unbound local error in _diff_text
"""
class A(str):
def __repr__(self):
return ''
expl = callequal(A(), '')
assert not expl
@needsnewassert
def test_pytest_assertrepr_compare_integration(testdir):
testdir.makepyfile("""