Skip to content

Commit

Permalink
Improve of the test output for logical expression with brackets.
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeardCode committed Jun 25, 2016
1 parent c519b95 commit e2f5501
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@

* Renamed the pytest ``pdb`` module (plugin) into ``debugging``.

* Improve of the test output for logical expression with brackets.
Fixes(`#925`_). Thanks `@DRMacIver`_ for reporting. Thanks to `@RedBeardCode`_
for PR.

*

* ImportErrors in plugins now are a fatal error instead of issuing a
Expand All @@ -50,7 +54,11 @@
.. _#1553: https://github.com/pytest-dev/pytest/issues/1553
.. _#1626: https://github.com/pytest-dev/pytest/pull/1626
.. _#1503: https://github.com/pytest-dev/pytest/issues/1503
<<<<<<< HEAD
.. _#1479: https://github.com/pytest-dev/pytest/issues/1479
=======
.. _#925: https://github.com/pytest-dev/pytest/issues/925
>>>>>>> Improve of the test output for logical expression with brackets.

.. _@graingert: https://github.com/graingert
.. _@taschini: https://github.com/taschini
Expand All @@ -59,6 +67,7 @@
.. _@Vogtinator: https://github.com/Vogtinator
.. _@bagerard: https://github.com/bagerard
.. _@davehunt: https://github.com/davehunt
.. _@DRMacIver: https://github.com/DRMacIver


2.9.2
Expand Down
7 changes: 7 additions & 0 deletions _pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Rewrite assertion AST to produce nice error messages"""

import ast
import _ast
import errno
import itertools
import imp
Expand Down Expand Up @@ -753,6 +754,8 @@ def visit_BoolOp(self, boolop):
self.statements = save
self.on_failure = fail_save
expl_template = self.helper("format_boolop", expl_list, ast.Num(is_or))
#if isinstance(boolop, (_ast.Compare, _ast.BoolOp)):
# expl_template = "({0})".format(expl_template)
expl = self.pop_format_context(expl_template)
return ast.Name(res_var, ast.Load()), self.explanation_param(expl)

Expand Down Expand Up @@ -855,6 +858,8 @@ def visit_Attribute(self, attr):
def visit_Compare(self, comp):
self.push_format_context()
left_res, left_expl = self.visit(comp.left)
if isinstance(comp.left, (_ast.Compare, _ast.BoolOp)):
left_expl = "({0})".format(left_expl)
res_variables = [self.variable() for i in range(len(comp.ops))]
load_names = [ast.Name(v, ast.Load()) for v in res_variables]
store_names = [ast.Name(v, ast.Store()) for v in res_variables]
Expand All @@ -864,6 +869,8 @@ def visit_Compare(self, comp):
results = [left_res]
for i, op, next_operand in it:
next_res, next_expl = self.visit(next_operand)
if isinstance(next_operand, (_ast.Compare, _ast.BoolOp)):
next_expl = "({0})".format(next_expl)
results.append(next_res)
sym = binop_map[op.__class__]
syms.append(ast.Str(sym))
Expand Down
27 changes: 27 additions & 0 deletions testing/test_assertrewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,3 +720,30 @@ def test_long_repr():
""")
result = testdir.runpytest()
assert 'unbalanced braces' not in result.stdout.str()


class TestIssue925():
def test_simple_case(self, testdir):
testdir.makepyfile("""
def test_ternary_display():
assert (False == False) == False
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines('*E*assert (False == False) == False')

def test_long_case(self, testdir):
testdir.makepyfile("""
def test_ternary_display():
assert False == (False == True) == True
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines('*E*assert (False == True) == True')

def test_many_brackets(self, testdir):
testdir.makepyfile("""
def test_ternary_display():
assert True == ((False == True) == True)
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines('*E*assert True == ((False == True) == True)')

0 comments on commit e2f5501

Please sign in to comment.