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:
holger krekel 2010-10-02 19:00:47 +02:00
parent 1ff173baee
commit 77cacb99ee
7 changed files with 36 additions and 37 deletions

View File

@ -93,7 +93,7 @@ py.apipkg.initpkg(__name__, dict(
'_AssertionError' : '._code.assertion:AssertionError',
'_reinterpret_old' : '._code.assertion:reinterpret_old',
'_reinterpret' : '._code.assertion:reinterpret',
'_binrepr' : '._code.assertion:_binrepr',
'_reprcompare' : '._code.assertion:_reprcompare',
},
# backports and additions of builtins

View File

@ -178,9 +178,9 @@ class DebugInterpreter(ast.NodeVisitor):
break
left_explanation, left_result = next_explanation, next_result
binrepr = py.code._binrepr
if binrepr:
res = binrepr(op_symbol, left_result, next_result)
rcomp = py.code._reprcompare
if rcomp:
res = rcomp(op_symbol, left_result, next_result)
if res:
explanation = res
return explanation, result

View File

@ -3,7 +3,7 @@ import py
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):
"""This formats an explanation

View File

@ -151,14 +151,13 @@ def pytest_sessionfinish(session, exitstatus):
# hooks for customising the assert methods
# -------------------------------------------------------------------------
def pytest_assert_binrepr(config, op, left, right):
"""Customise explanation for binary operators
def pytest_assertrepr_compare(config, op, left, right):
"""return explanation for comparisons in failing assert expressions.
Return None or an empty list for no custom explanation, otherwise
return a list of strings. The strings will be joined by newlines
but any newlines *in* a string will be escaped. Note that all but
the first line will be indented sligthly, the intention is for the
first line to be a summary.
Return None for no custom explanation, otherwise return a list
of strings. The strings will be joined by newlines but any newlines
*in* a string will be escaped. Note that all but the first line will
be indented sligthly, the intention is for the first line to be a summary.
"""
# -------------------------------------------------------------------------

View File

@ -15,20 +15,20 @@ def pytest_configure(config):
if not config.getvalue("noassert") and not config.getvalue("nomagic"):
warn_about_missing_assertion()
config._oldassertion = py.builtin.builtins.AssertionError
config._oldbinrepr = py.code._binrepr
config._oldbinrepr = py.code._reprcompare
py.builtin.builtins.AssertionError = py.code._AssertionError
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)
for new_expl in hook_result:
if new_expl:
return '\n~'.join(new_expl)
py.code._binrepr = callbinrepr
py.code._reprcompare = callbinrepr
def pytest_unconfigure(config):
if hasattr(config, '_oldassertion'):
py.builtin.builtins.AssertionError = config._oldassertion
py.code._binrepr = config._oldbinrepr
py.code._reprcompare = config._oldbinrepr
del config._oldassertion
del config._oldbinrepr
@ -49,8 +49,8 @@ except NameError:
basestring = str
def pytest_assert_binrepr(op, left, right):
"""Make specialised explanations for some operators/operands"""
def pytest_assertrepr_compare(op, left, right):
"""return specialised explanations for some operators/operands"""
left_repr = py.io.saferepr(left, maxsize=30)
right_repr = py.io.saferepr(right, maxsize=30)
summary = '%s %s %s' % (left_repr, op, right_repr)

View File

@ -219,8 +219,8 @@ def test_underscore_api():
py.code._reinterpret
@py.test.mark.skipif("sys.version_info < (2,6)")
def test_assert_customizable_binrepr(monkeypatch):
monkeypatch.setattr(py.code, '_binrepr', lambda *args: 'hello')
def test_assert_customizable_reprcompare(monkeypatch):
monkeypatch.setattr(py.code, '_reprcompare', lambda *args: 'hello')
try:
assert 3 == 4
except AssertionError:

View File

@ -25,68 +25,68 @@ class TestBinReprIntegration:
self.right = right
mockhook = MockHook()
monkeypatch = request.getfuncargvalue("monkeypatch")
monkeypatch.setattr(py.code, '_binrepr', mockhook)
monkeypatch.setattr(py.code, '_reprcompare', mockhook)
return mockhook
def test_pytest_assert_binrepr_called(self, hook):
def test_pytest_assertrepr_compare_called(self, hook):
interpret('assert 0 == 1')
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]')
assert hook.op == '=='
assert hook.left == [0, 1]
assert hook.right == [0, 2]
def test_configure_unconfigure(self, testdir, hook):
assert hook == py.code._binrepr
assert hook == py.code._reprcompare
config = testdir.parseconfig()
plugin.pytest_configure(config)
assert hook != py.code._binrepr
assert hook != py.code._reprcompare
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):
assert plugin.pytest_assert_binrepr('==', [0, 1], 'foo') is None
assert plugin.pytest_assertrepr_compare('==', [0, 1], 'foo') is None
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
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 '+ eggs' in diff
def test_multiline_text_diff(self):
left = 'foo\nspam\nbar'
right = 'foo\neggs\nbar'
diff = plugin.pytest_assert_binrepr('==', left, right)
diff = plugin.pytest_assertrepr_compare('==', left, right)
assert '- spam' in diff
assert '+ eggs' in diff
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
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
expl = plugin.pytest_assert_binrepr('==', [0, 1, 2], [0, 1])
expl = plugin.pytest_assertrepr_compare('==', [0, 1, 2], [0, 1])
assert len(expl) > 1
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
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
@needsnewassert
def test_pytest_assert_binrepr_integration(testdir):
def test_pytest_assertrepr_compare_integration(testdir):
testdir.makepyfile("""
def test_hello():
x = set(range(100))