display the repr of some global names (fixes #171)
This commit is contained in:
parent
4d4b551079
commit
65edf87ea6
|
@ -1,6 +1,9 @@
|
||||||
Changes between 2.3.4 and 2.3.5dev
|
Changes between 2.3.4 and 2.3.5dev
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
|
|
||||||
|
- fix issue171 - in assertion rewriting, show the repr of some
|
||||||
|
global variables
|
||||||
|
|
||||||
- fix option help for "-k"
|
- fix option help for "-k"
|
||||||
|
|
||||||
- move long description of distribution into README.rst
|
- move long description of distribution into README.rst
|
||||||
|
@ -119,7 +122,7 @@ Changes between 2.2.4 and 2.3.0
|
||||||
|
|
||||||
- fix issue202 - better automatic names for parametrized test functions
|
- fix issue202 - better automatic names for parametrized test functions
|
||||||
- fix issue139 - introduce @pytest.fixture which allows direct scoping
|
- fix issue139 - introduce @pytest.fixture which allows direct scoping
|
||||||
and parametrization of funcarg factories.
|
and parametrization of funcarg factories.
|
||||||
- fix issue198 - conftest fixtures were not found on windows32 in some
|
- fix issue198 - conftest fixtures were not found on windows32 in some
|
||||||
circumstances with nested directory structures due to path manipulation issues
|
circumstances with nested directory structures due to path manipulation issues
|
||||||
- fix issue193 skip test functions with were parametrized with empty
|
- fix issue193 skip test functions with were parametrized with empty
|
||||||
|
|
|
@ -262,6 +262,9 @@ def rewrite_asserts(mod):
|
||||||
_saferepr = py.io.saferepr
|
_saferepr = py.io.saferepr
|
||||||
from _pytest.assertion.util import format_explanation as _format_explanation
|
from _pytest.assertion.util import format_explanation as _format_explanation
|
||||||
|
|
||||||
|
def _should_repr_global_name(obj):
|
||||||
|
return not hasattr(obj, "__name__") and not py.builtin.callable(obj)
|
||||||
|
|
||||||
def _format_boolop(explanations, is_or):
|
def _format_boolop(explanations, is_or):
|
||||||
return "(" + (is_or and " or " or " and ").join(explanations) + ")"
|
return "(" + (is_or and " or " or " and ").join(explanations) + ")"
|
||||||
|
|
||||||
|
@ -473,11 +476,12 @@ class AssertionRewriter(ast.NodeVisitor):
|
||||||
return self.statements
|
return self.statements
|
||||||
|
|
||||||
def visit_Name(self, name):
|
def visit_Name(self, name):
|
||||||
# Check if the name is local or not.
|
# Display the repr of the name if it's a local variable or
|
||||||
|
# _should_repr_global_name() thinks it's acceptable.
|
||||||
locs = ast.Call(self.builtin("locals"), [], [], None, None)
|
locs = ast.Call(self.builtin("locals"), [], [], None, None)
|
||||||
globs = ast.Call(self.builtin("globals"), [], [], None, None)
|
inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs])
|
||||||
ops = [ast.In(), ast.IsNot()]
|
dorepr = self.helper("should_repr_global_name", name)
|
||||||
test = ast.Compare(ast.Str(name.id), ops, [locs, globs])
|
test = ast.BoolOp(ast.Or(), [inlocs, dorepr])
|
||||||
expr = ast.IfExp(test, self.display(name), ast.Str(name.id))
|
expr = ast.IfExp(test, self.display(name), ast.Str(name.id))
|
||||||
return name, self.explanation_param(expr)
|
return name, self.explanation_param(expr)
|
||||||
|
|
||||||
|
|
|
@ -107,7 +107,15 @@ class TestAssertionRewrite:
|
||||||
assert getmsg(f) == "assert False"
|
assert getmsg(f) == "assert False"
|
||||||
def f():
|
def f():
|
||||||
assert a_global
|
assert a_global
|
||||||
assert getmsg(f, {"a_global" : False}) == "assert a_global"
|
assert getmsg(f, {"a_global" : False}) == "assert False"
|
||||||
|
def f():
|
||||||
|
assert sys == 42
|
||||||
|
assert getmsg(f, {"sys" : sys}) == "assert sys == 42"
|
||||||
|
def f():
|
||||||
|
assert cls == 42
|
||||||
|
class X(object):
|
||||||
|
pass
|
||||||
|
assert getmsg(f, {"cls" : X}) == "assert cls == 42"
|
||||||
|
|
||||||
def test_assert_already_has_message(self):
|
def test_assert_already_has_message(self):
|
||||||
def f():
|
def f():
|
||||||
|
@ -232,7 +240,7 @@ class TestAssertionRewrite:
|
||||||
def test_attribute(self):
|
def test_attribute(self):
|
||||||
class X(object):
|
class X(object):
|
||||||
g = 3
|
g = 3
|
||||||
ns = {"X" : X, "x" : X()}
|
ns = {"x" : X}
|
||||||
def f():
|
def f():
|
||||||
assert not x.g
|
assert not x.g
|
||||||
assert getmsg(f, ns) == """assert not 3
|
assert getmsg(f, ns) == """assert not 3
|
||||||
|
|
Loading…
Reference in New Issue