Skip to content

Commit

Permalink
visit_Assert: minor cleanup
Browse files Browse the repository at this point in the history
- import format_explanation as-is
- move setting of `negation` out of branches
  • Loading branch information
blueyed committed Nov 18, 2019
1 parent 64d8910 commit ed1d1b1
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/_pytest/assertion/rewrite.py
Expand Up @@ -22,9 +22,7 @@
from _pytest._io.saferepr import saferepr
from _pytest._version import version
from _pytest.assertion import util
from _pytest.assertion.util import ( # noqa: F401
format_explanation as _format_explanation,
)
from _pytest.assertion.util import format_explanation # noqa: F401
from _pytest.compat import fspath
from _pytest.pathlib import fnmatch_ex
from _pytest.pathlib import Path
Expand Down Expand Up @@ -807,8 +805,9 @@ def visit_Assert(self, assert_):
)
)

negation = ast.UnaryOp(ast.Not(), top_condition)

if self.enable_assertion_pass_hook: # Experimental pytest_assertion_pass hook
negation = ast.UnaryOp(ast.Not(), top_condition)
msg = self.pop_format_context(ast.Str(explanation))

# Failed
Expand All @@ -821,15 +820,15 @@ def visit_Assert(self, assert_):
err_explanation = ast.BinOp(ast.Str(gluestr), ast.Add(), msg)
err_msg = ast.BinOp(assertmsg, ast.Add(), err_explanation)
err_name = ast.Name("AssertionError", ast.Load())
fmt = self.helper("_format_explanation", err_msg)
fmt = self.helper("format_explanation", err_msg)
exc = ast.Call(err_name, [fmt], [])
raise_ = ast.Raise(exc, None)
statements_fail = []
statements_fail.extend(self.expl_stmts)
statements_fail.append(raise_)

# Passed
fmt_pass = self.helper("_format_explanation", msg)
fmt_pass = self.helper("format_explanation", msg)
orig = self._assert_expr_to_lineno()[assert_.lineno]
hook_call_pass = ast.Expr(
self.helper(
Expand Down Expand Up @@ -860,7 +859,6 @@ def visit_Assert(self, assert_):
else: # Original assertion rewriting
# Create failure message.
body = self.expl_stmts
negation = ast.UnaryOp(ast.Not(), top_condition)
self.statements.append(ast.If(negation, body, []))
if assert_.msg:
assertmsg = self.helper("_format_assertmsg", assert_.msg)
Expand All @@ -870,7 +868,7 @@ def visit_Assert(self, assert_):
explanation = "assert " + explanation
template = ast.BinOp(assertmsg, ast.Add(), ast.Str(explanation))
msg = self.pop_format_context(template)
fmt = self.helper("_format_explanation", msg)
fmt = self.helper("format_explanation", msg)
err_name = ast.Name("AssertionError", ast.Load())
exc = ast.Call(err_name, [fmt], [])
raise_ = ast.Raise(exc, None)
Expand Down

0 comments on commit ed1d1b1

Please sign in to comment.