to better match the naming of the corresponding AST (and in case
we want to add more customizations later) rename pytest_assert_binrepr -> pytest_assertrepr_compare rename binrepr -> reprcompare --HG-- branch : trunk
This commit is contained in:
parent
1ff173baee
commit
77cacb99ee
|
@ -93,7 +93,7 @@ py.apipkg.initpkg(__name__, dict(
|
||||||
'_AssertionError' : '._code.assertion:AssertionError',
|
'_AssertionError' : '._code.assertion:AssertionError',
|
||||||
'_reinterpret_old' : '._code.assertion:reinterpret_old',
|
'_reinterpret_old' : '._code.assertion:reinterpret_old',
|
||||||
'_reinterpret' : '._code.assertion:reinterpret',
|
'_reinterpret' : '._code.assertion:reinterpret',
|
||||||
'_binrepr' : '._code.assertion:_binrepr',
|
'_reprcompare' : '._code.assertion:_reprcompare',
|
||||||
},
|
},
|
||||||
|
|
||||||
# backports and additions of builtins
|
# backports and additions of builtins
|
||||||
|
|
|
@ -178,9 +178,9 @@ class DebugInterpreter(ast.NodeVisitor):
|
||||||
break
|
break
|
||||||
left_explanation, left_result = next_explanation, next_result
|
left_explanation, left_result = next_explanation, next_result
|
||||||
|
|
||||||
binrepr = py.code._binrepr
|
rcomp = py.code._reprcompare
|
||||||
if binrepr:
|
if rcomp:
|
||||||
res = binrepr(op_symbol, left_result, next_result)
|
res = rcomp(op_symbol, left_result, next_result)
|
||||||
if res:
|
if res:
|
||||||
explanation = res
|
explanation = res
|
||||||
return explanation, result
|
return explanation, result
|
||||||
|
|
|
@ -3,7 +3,7 @@ import py
|
||||||
|
|
||||||
BuiltinAssertionError = py.builtin.builtins.AssertionError
|
BuiltinAssertionError = py.builtin.builtins.AssertionError
|
||||||
|
|
||||||
_binrepr = None # if set, will be called by assert reinterp for comparison ops
|
_reprcompare = None # if set, will be called by assert reinterp for comparison ops
|
||||||
|
|
||||||
def _format_explanation(explanation):
|
def _format_explanation(explanation):
|
||||||
"""This formats an explanation
|
"""This formats an explanation
|
||||||
|
|
|
@ -151,14 +151,13 @@ def pytest_sessionfinish(session, exitstatus):
|
||||||
# hooks for customising the assert methods
|
# hooks for customising the assert methods
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
def pytest_assert_binrepr(config, op, left, right):
|
def pytest_assertrepr_compare(config, op, left, right):
|
||||||
"""Customise explanation for binary operators
|
"""return explanation for comparisons in failing assert expressions.
|
||||||
|
|
||||||
Return None or an empty list for no custom explanation, otherwise
|
Return None for no custom explanation, otherwise return a list
|
||||||
return a list of strings. The strings will be joined by newlines
|
of strings. The strings will be joined by newlines but any newlines
|
||||||
but any newlines *in* a string will be escaped. Note that all but
|
*in* a string will be escaped. Note that all but the first line will
|
||||||
the first line will be indented sligthly, the intention is for the
|
be indented sligthly, the intention is for the first line to be a summary.
|
||||||
first line to be a summary.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
|
@ -15,20 +15,20 @@ def pytest_configure(config):
|
||||||
if not config.getvalue("noassert") and not config.getvalue("nomagic"):
|
if not config.getvalue("noassert") and not config.getvalue("nomagic"):
|
||||||
warn_about_missing_assertion()
|
warn_about_missing_assertion()
|
||||||
config._oldassertion = py.builtin.builtins.AssertionError
|
config._oldassertion = py.builtin.builtins.AssertionError
|
||||||
config._oldbinrepr = py.code._binrepr
|
config._oldbinrepr = py.code._reprcompare
|
||||||
py.builtin.builtins.AssertionError = py.code._AssertionError
|
py.builtin.builtins.AssertionError = py.code._AssertionError
|
||||||
def callbinrepr(op, left, right):
|
def callbinrepr(op, left, right):
|
||||||
hook_result = config.hook.pytest_assert_binrepr(
|
hook_result = config.hook.pytest_assertrepr_compare(
|
||||||
config=config, op=op, left=left, right=right)
|
config=config, op=op, left=left, right=right)
|
||||||
for new_expl in hook_result:
|
for new_expl in hook_result:
|
||||||
if new_expl:
|
if new_expl:
|
||||||
return '\n~'.join(new_expl)
|
return '\n~'.join(new_expl)
|
||||||
py.code._binrepr = callbinrepr
|
py.code._reprcompare = callbinrepr
|
||||||
|
|
||||||
def pytest_unconfigure(config):
|
def pytest_unconfigure(config):
|
||||||
if hasattr(config, '_oldassertion'):
|
if hasattr(config, '_oldassertion'):
|
||||||
py.builtin.builtins.AssertionError = config._oldassertion
|
py.builtin.builtins.AssertionError = config._oldassertion
|
||||||
py.code._binrepr = config._oldbinrepr
|
py.code._reprcompare = config._oldbinrepr
|
||||||
del config._oldassertion
|
del config._oldassertion
|
||||||
del config._oldbinrepr
|
del config._oldbinrepr
|
||||||
|
|
||||||
|
@ -49,8 +49,8 @@ except NameError:
|
||||||
basestring = str
|
basestring = str
|
||||||
|
|
||||||
|
|
||||||
def pytest_assert_binrepr(op, left, right):
|
def pytest_assertrepr_compare(op, left, right):
|
||||||
"""Make specialised explanations for some operators/operands"""
|
"""return specialised explanations for some operators/operands"""
|
||||||
left_repr = py.io.saferepr(left, maxsize=30)
|
left_repr = py.io.saferepr(left, maxsize=30)
|
||||||
right_repr = py.io.saferepr(right, maxsize=30)
|
right_repr = py.io.saferepr(right, maxsize=30)
|
||||||
summary = '%s %s %s' % (left_repr, op, right_repr)
|
summary = '%s %s %s' % (left_repr, op, right_repr)
|
||||||
|
|
|
@ -219,8 +219,8 @@ def test_underscore_api():
|
||||||
py.code._reinterpret
|
py.code._reinterpret
|
||||||
|
|
||||||
@py.test.mark.skipif("sys.version_info < (2,6)")
|
@py.test.mark.skipif("sys.version_info < (2,6)")
|
||||||
def test_assert_customizable_binrepr(monkeypatch):
|
def test_assert_customizable_reprcompare(monkeypatch):
|
||||||
monkeypatch.setattr(py.code, '_binrepr', lambda *args: 'hello')
|
monkeypatch.setattr(py.code, '_reprcompare', lambda *args: 'hello')
|
||||||
try:
|
try:
|
||||||
assert 3 == 4
|
assert 3 == 4
|
||||||
except AssertionError:
|
except AssertionError:
|
||||||
|
|
|
@ -25,68 +25,68 @@ class TestBinReprIntegration:
|
||||||
self.right = right
|
self.right = right
|
||||||
mockhook = MockHook()
|
mockhook = MockHook()
|
||||||
monkeypatch = request.getfuncargvalue("monkeypatch")
|
monkeypatch = request.getfuncargvalue("monkeypatch")
|
||||||
monkeypatch.setattr(py.code, '_binrepr', mockhook)
|
monkeypatch.setattr(py.code, '_reprcompare', mockhook)
|
||||||
return mockhook
|
return mockhook
|
||||||
|
|
||||||
def test_pytest_assert_binrepr_called(self, hook):
|
def test_pytest_assertrepr_compare_called(self, hook):
|
||||||
interpret('assert 0 == 1')
|
interpret('assert 0 == 1')
|
||||||
assert hook.called
|
assert hook.called
|
||||||
|
|
||||||
|
|
||||||
def test_pytest_assert_binrepr_args(self, hook):
|
def test_pytest_assertrepr_compare_args(self, hook):
|
||||||
interpret('assert [0, 1] == [0, 2]')
|
interpret('assert [0, 1] == [0, 2]')
|
||||||
assert hook.op == '=='
|
assert hook.op == '=='
|
||||||
assert hook.left == [0, 1]
|
assert hook.left == [0, 1]
|
||||||
assert hook.right == [0, 2]
|
assert hook.right == [0, 2]
|
||||||
|
|
||||||
def test_configure_unconfigure(self, testdir, hook):
|
def test_configure_unconfigure(self, testdir, hook):
|
||||||
assert hook == py.code._binrepr
|
assert hook == py.code._reprcompare
|
||||||
config = testdir.parseconfig()
|
config = testdir.parseconfig()
|
||||||
plugin.pytest_configure(config)
|
plugin.pytest_configure(config)
|
||||||
assert hook != py.code._binrepr
|
assert hook != py.code._reprcompare
|
||||||
plugin.pytest_unconfigure(config)
|
plugin.pytest_unconfigure(config)
|
||||||
assert hook == py.code._binrepr
|
assert hook == py.code._reprcompare
|
||||||
|
|
||||||
class TestAssert_binrepr:
|
class TestAssert_reprcompare:
|
||||||
def test_different_types(self):
|
def test_different_types(self):
|
||||||
assert plugin.pytest_assert_binrepr('==', [0, 1], 'foo') is None
|
assert plugin.pytest_assertrepr_compare('==', [0, 1], 'foo') is None
|
||||||
|
|
||||||
def test_summary(self):
|
def test_summary(self):
|
||||||
summary = plugin.pytest_assert_binrepr('==', [0, 1], [0, 2])[0]
|
summary = plugin.pytest_assertrepr_compare('==', [0, 1], [0, 2])[0]
|
||||||
assert len(summary) < 65
|
assert len(summary) < 65
|
||||||
|
|
||||||
def test_text_diff(self):
|
def test_text_diff(self):
|
||||||
diff = plugin.pytest_assert_binrepr('==', 'spam', 'eggs')[1:]
|
diff = plugin.pytest_assertrepr_compare('==', 'spam', 'eggs')[1:]
|
||||||
assert '- spam' in diff
|
assert '- spam' in diff
|
||||||
assert '+ eggs' in diff
|
assert '+ eggs' in diff
|
||||||
|
|
||||||
def test_multiline_text_diff(self):
|
def test_multiline_text_diff(self):
|
||||||
left = 'foo\nspam\nbar'
|
left = 'foo\nspam\nbar'
|
||||||
right = 'foo\neggs\nbar'
|
right = 'foo\neggs\nbar'
|
||||||
diff = plugin.pytest_assert_binrepr('==', left, right)
|
diff = plugin.pytest_assertrepr_compare('==', left, right)
|
||||||
assert '- spam' in diff
|
assert '- spam' in diff
|
||||||
assert '+ eggs' in diff
|
assert '+ eggs' in diff
|
||||||
|
|
||||||
def test_list(self):
|
def test_list(self):
|
||||||
expl = plugin.pytest_assert_binrepr('==', [0, 1], [0, 2])
|
expl = plugin.pytest_assertrepr_compare('==', [0, 1], [0, 2])
|
||||||
assert len(expl) > 1
|
assert len(expl) > 1
|
||||||
|
|
||||||
def test_list_different_lenghts(self):
|
def test_list_different_lenghts(self):
|
||||||
expl = plugin.pytest_assert_binrepr('==', [0, 1], [0, 1, 2])
|
expl = plugin.pytest_assertrepr_compare('==', [0, 1], [0, 1, 2])
|
||||||
assert len(expl) > 1
|
assert len(expl) > 1
|
||||||
expl = plugin.pytest_assert_binrepr('==', [0, 1, 2], [0, 1])
|
expl = plugin.pytest_assertrepr_compare('==', [0, 1, 2], [0, 1])
|
||||||
assert len(expl) > 1
|
assert len(expl) > 1
|
||||||
|
|
||||||
def test_dict(self):
|
def test_dict(self):
|
||||||
expl = plugin.pytest_assert_binrepr('==', {'a': 0}, {'a': 1})
|
expl = plugin.pytest_assertrepr_compare('==', {'a': 0}, {'a': 1})
|
||||||
assert len(expl) > 1
|
assert len(expl) > 1
|
||||||
|
|
||||||
def test_set(self):
|
def test_set(self):
|
||||||
expl = plugin.pytest_assert_binrepr('==', set([0, 1]), set([0, 2]))
|
expl = plugin.pytest_assertrepr_compare('==', set([0, 1]), set([0, 2]))
|
||||||
assert len(expl) > 1
|
assert len(expl) > 1
|
||||||
|
|
||||||
@needsnewassert
|
@needsnewassert
|
||||||
def test_pytest_assert_binrepr_integration(testdir):
|
def test_pytest_assertrepr_compare_integration(testdir):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
def test_hello():
|
def test_hello():
|
||||||
x = set(range(100))
|
x = set(range(100))
|
||||||
|
|
Loading…
Reference in New Issue