Fixes #1503 no longer collapse false explanations

This commit is contained in:
Tom Viner 2016-06-24 12:44:06 +02:00
parent 3d263c64c3
commit 77689eb486
3 changed files with 30 additions and 47 deletions

View File

@ -31,6 +31,13 @@
deprecated but still present. Thanks to `@RedBeardCode`_ and `@tomviner`_ deprecated but still present. Thanks to `@RedBeardCode`_ and `@tomviner`_
for PR (`#1626`_). for PR (`#1626`_).
* Always include full assertion explanation. The previous behaviour was hiding
sub-expressions that happened to be False, assuming this was redundant information.
Thanks `@bagerard`_ for reporting (`#1503`_). Thanks to `@davehunt`_ and
`@tomviner`_ for PR.
*
* *
.. _#1580: https://github.com/pytest-dev/pytest/pull/1580 .. _#1580: https://github.com/pytest-dev/pytest/pull/1580
@ -39,12 +46,15 @@
.. _#460: https://github.com/pytest-dev/pytest/pull/460 .. _#460: https://github.com/pytest-dev/pytest/pull/460
.. _#1553: https://github.com/pytest-dev/pytest/issues/1553 .. _#1553: https://github.com/pytest-dev/pytest/issues/1553
.. _#1626: https://github.com/pytest-dev/pytest/pull/1626 .. _#1626: https://github.com/pytest-dev/pytest/pull/1626
.. _#1503: https://github.com/pytest-dev/pytest/issues/1503
.. _@graingert: https://github.com/graingert .. _@graingert: https://github.com/graingert
.. _@taschini: https://github.com/taschini .. _@taschini: https://github.com/taschini
.. _@nikratio: https://github.com/nikratio .. _@nikratio: https://github.com/nikratio
.. _@RedBeardCode: https://github.com/RedBeardCode .. _@RedBeardCode: https://github.com/RedBeardCode
.. _@Vogtinator: https://github.com/Vogtinator .. _@Vogtinator: https://github.com/Vogtinator
.. _@bagerard: https://github.com/bagerard
.. _@davehunt: https://github.com/davehunt
2.9.2 2.9.2

View File

@ -38,44 +38,11 @@ def format_explanation(explanation):
displaying diffs. displaying diffs.
""" """
explanation = ecu(explanation) explanation = ecu(explanation)
explanation = _collapse_false(explanation)
lines = _split_explanation(explanation) lines = _split_explanation(explanation)
result = _format_lines(lines) result = _format_lines(lines)
return u('\n').join(result) return u('\n').join(result)
def _collapse_false(explanation):
"""Collapse expansions of False
So this strips out any "assert False\n{where False = ...\n}"
blocks.
"""
where = 0
while True:
start = where = explanation.find("False\n{False = ", where)
if where == -1:
break
level = 0
prev_c = explanation[start]
for i, c in enumerate(explanation[start:]):
if prev_c + c == "\n{":
level += 1
elif prev_c + c == "\n}":
level -= 1
if not level:
break
prev_c = c
else:
raise AssertionError("unbalanced braces: %r" % (explanation,))
end = start + i
where = end
if explanation[end - 1] == '\n':
explanation = (explanation[:start] + explanation[start+15:end-1] +
explanation[end+1:])
where -= 17
return explanation
def _split_explanation(explanation): def _split_explanation(explanation):
"""Return a list of individual lines in the explanation """Return a list of individual lines in the explanation

View File

@ -213,10 +213,12 @@ class TestAssertionRewrite:
return False return False
def f(): def f():
assert x() and x() assert x() and x()
assert getmsg(f, {"x" : x}) == "assert (x())" assert getmsg(f, {"x" : x}) == """assert (False)
+ where False = x()"""
def f(): def f():
assert False or x() assert False or x()
assert getmsg(f, {"x" : x}) == "assert (False or x())" assert getmsg(f, {"x" : x}) == """assert (False or False)
+ where False = x()"""
def f(): def f():
assert 1 in {} and 2 in {} assert 1 in {} and 2 in {}
assert getmsg(f) == "assert (1 in {})" assert getmsg(f) == "assert (1 in {})"
@ -299,27 +301,34 @@ class TestAssertionRewrite:
ns = {"g" : g} ns = {"g" : g}
def f(): def f():
assert g() assert g()
assert getmsg(f, ns) == """assert g()""" assert getmsg(f, ns) == """assert False
+ where False = g()"""
def f(): def f():
assert g(1) assert g(1)
assert getmsg(f, ns) == """assert g(1)""" assert getmsg(f, ns) == """assert False
+ where False = g(1)"""
def f(): def f():
assert g(1, 2) assert g(1, 2)
assert getmsg(f, ns) == """assert g(1, 2)""" assert getmsg(f, ns) == """assert False
+ where False = g(1, 2)"""
def f(): def f():
assert g(1, g=42) assert g(1, g=42)
assert getmsg(f, ns) == """assert g(1, g=42)""" assert getmsg(f, ns) == """assert False
+ where False = g(1, g=42)"""
def f(): def f():
assert g(1, 3, g=23) assert g(1, 3, g=23)
assert getmsg(f, ns) == """assert g(1, 3, g=23)""" assert getmsg(f, ns) == """assert False
+ where False = g(1, 3, g=23)"""
def f(): def f():
seq = [1, 2, 3] seq = [1, 2, 3]
assert g(*seq) assert g(*seq)
assert getmsg(f, ns) == """assert g(*[1, 2, 3])""" assert getmsg(f, ns) == """assert False
+ where False = g(*[1, 2, 3])"""
def f(): def f():
x = "a" x = "a"
assert g(**{x : 2}) assert g(**{x : 2})
assert getmsg(f, ns) == """assert g(**{'a': 2})""" assert getmsg(f, ns) == """assert False
+ where False = g(**{'a': 2})"""
def test_attribute(self): def test_attribute(self):
class X(object): class X(object):
@ -332,7 +341,8 @@ class TestAssertionRewrite:
def f(): def f():
x.a = False # noqa x.a = False # noqa
assert x.a # noqa assert x.a # noqa
assert getmsg(f, ns) == """assert x.a""" assert getmsg(f, ns) == """assert False
+ where False = x.a"""
def test_comparisons(self): def test_comparisons(self):
def f(): def f():
@ -710,7 +720,3 @@ def test_issue731(testdir):
""") """)
result = testdir.runpytest() result = testdir.runpytest()
assert 'unbalanced braces' not in result.stdout.str() assert 'unbalanced braces' not in result.stdout.str()
def test_collapse_false_unbalanced_braces():
util._collapse_false('some text{ False\n{False = some more text\n}')