Commits

Floris Bruynooghe committed 4cb5b6b

Show final value first when explaining an attribute

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

  • Participants
  • Parent commits 3ed830e

Comments (0)

Files changed (4)

File doc/example/assertion/failure_demo.py

         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

File doc/example/assertion/test_failures.py

     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

File py/_code/_assertionnew.py

             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,)

File testing/code/test_assertion.py

         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):