From e2f550156e84fcaef5f9611910aadcec549f5745 Mon Sep 17 00:00:00 2001 From: RedBeardCode Date: Sat, 25 Jun 2016 17:21:10 +0200 Subject: [PATCH 1/3] Improve of the test output for logical expression with brackets. Fixes #925 --- CHANGELOG.rst | 9 +++++++++ _pytest/assertion/rewrite.py | 7 +++++++ testing/test_assertrewrite.py | 27 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 6f89fba99..075df922f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 @@ -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 @@ -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 diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py index 14b8e49db..910e2293d 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -1,6 +1,7 @@ """Rewrite assertion AST to produce nice error messages""" import ast +import _ast import errno import itertools import imp @@ -753,6 +754,8 @@ class AssertionRewriter(ast.NodeVisitor): 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) @@ -855,6 +858,8 @@ class AssertionRewriter(ast.NodeVisitor): 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] @@ -864,6 +869,8 @@ class AssertionRewriter(ast.NodeVisitor): 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)) diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index a47d8a75f..82dbe8dd9 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -720,3 +720,30 @@ def test_issue731(testdir): """) 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)') + From 22c2d876330661707a45942551a70c1539944a82 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 26 Jun 2016 21:18:01 +0200 Subject: [PATCH 2/3] Fix bad merge in CHANGELOG --- CHANGELOG.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 075df922f..2a18d2b86 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -54,11 +54,8 @@ .. _#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 From 58e558141d5e2218d0d1556bed7a4c8bb1a6e2ce Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 26 Jun 2016 21:19:07 +0200 Subject: [PATCH 3/3] Remove commented out code --- _pytest/assertion/rewrite.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py index 910e2293d..d8272a056 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -754,8 +754,6 @@ class AssertionRewriter(ast.NodeVisitor): 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)