From 77cacb99eeaac2fea0ac4a3c63633345f68c4051 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sat, 2 Oct 2010 19:00:47 +0200 Subject: [PATCH] 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 --- py/__init__.py | 2 +- py/_code/_assertionnew.py | 6 ++--- py/_code/assertion.py | 2 +- py/_plugin/hookspec.py | 13 +++++----- py/_plugin/pytest_assertion.py | 12 ++++----- testing/code/test_assertion.py | 4 +-- testing/plugin/test_pytest_assertion.py | 34 ++++++++++++------------- 7 files changed, 36 insertions(+), 37 deletions(-) diff --git a/py/__init__.py b/py/__init__.py index 814161cea..4a55d189d 100644 --- a/py/__init__.py +++ b/py/__init__.py @@ -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 diff --git a/py/_code/_assertionnew.py b/py/_code/_assertionnew.py index 5fdc1088b..c3d8df6aa 100644 --- a/py/_code/_assertionnew.py +++ b/py/_code/_assertionnew.py @@ -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 diff --git a/py/_code/assertion.py b/py/_code/assertion.py index 675643e57..e77f250df 100644 --- a/py/_code/assertion.py +++ b/py/_code/assertion.py @@ -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 diff --git a/py/_plugin/hookspec.py b/py/_plugin/hookspec.py index 2281d149c..02a310da5 100644 --- a/py/_plugin/hookspec.py +++ b/py/_plugin/hookspec.py @@ -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. """ # ------------------------------------------------------------------------- diff --git a/py/_plugin/pytest_assertion.py b/py/_plugin/pytest_assertion.py index bdfd0e819..e9a04975f 100644 --- a/py/_plugin/pytest_assertion.py +++ b/py/_plugin/pytest_assertion.py @@ -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) diff --git a/testing/code/test_assertion.py b/testing/code/test_assertion.py index 07e9fa754..4904f7fb1 100644 --- a/testing/code/test_assertion.py +++ b/testing/code/test_assertion.py @@ -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: diff --git a/testing/plugin/test_pytest_assertion.py b/testing/plugin/test_pytest_assertion.py index 8740b733a..1b98c8ae8 100644 --- a/testing/plugin/test_pytest_assertion.py +++ b/testing/plugin/test_pytest_assertion.py @@ -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))