Show final value first when explaining an attribute

Then show the expansion as a "where" part of the explanation.

--HG--
branch : trunk
This commit is contained in:
Floris Bruynooghe 2010-10-06 18:20:09 +01:00
parent c62ed0cd93
commit ec5ea5c05e
4 changed files with 55 additions and 1 deletions

View File

@ -159,5 +159,35 @@ class TestSpecialisedExplanations(object):
assert 1 in [0, 2, 3, 4, 5]
def test_attribute():
class Foo(object):
b = 1
i = Foo()
assert i.b == 2
def test_attribute_instance():
class Foo(object):
b = 1
assert Foo().b == 2
def test_attribute_failure():
class Foo(object):
def _get_b(self):
raise Exception('Failed to get attrib')
b = property(_get_b)
i = Foo()
assert i.b == 2
def test_attribute_multiple():
class Foo(object):
b = 1
class Bar(object):
b = 2
assert Foo().b == Bar().b
def globf(x):
return x+1

View File

@ -10,6 +10,6 @@ def test_failure_demo_fails_properly(testdir):
failure_demo.copy(testdir.tmpdir.join(failure_demo.basename))
result = testdir.runpytest(target)
result.stdout.fnmatch_lines([
"*31 failed*"
"*35 failed*"
])
assert result.ret != 0

View File

@ -298,6 +298,9 @@ class DebugInterpreter(ast.NodeVisitor):
result = self.frame.eval(co, __exprinfo_expr=source_result)
except Exception:
raise Failure(explanation)
explanation = "%s\n{%s = %s.%s\n}" % (self.frame.repr(result),
self.frame.repr(result),
source_explanation, attr.attr)
# Check if the attr is from an instance.
source = "%r in getattr(__exprinfo_expr, '__dict__', {})"
source = source % (attr.attr,)

View File

@ -81,6 +81,27 @@ def test_is():
s = str(e)
assert s.startswith("assert 1 is 2")
def test_attrib():
class Foo(object):
b = 1
i = Foo()
try:
assert i.b == 2
except AssertionError:
e = exvalue()
s = str(e)
assert s.startswith("assert 1 == 2")
def test_attrib_inst():
class Foo(object):
b = 1
try:
assert Foo().b == 2
except AssertionError:
e = exvalue()
s = str(e)
assert s.startswith("assert 1 == 2")
def test_assert_non_string_message():
class A:
def __str__(self):