diff --git a/py/_code/_assertionnew.py b/py/_code/_assertionnew.py index 192eecfce..2c40a93dc 100644 --- a/py/_code/_assertionnew.py +++ b/py/_code/_assertionnew.py @@ -184,7 +184,7 @@ class DebugInterpreter(ast.NodeVisitor): break left_explanation, left_result = next_explanation, next_result if self._pytesthook: - hook_result = self._pytesthook.pytest_assert_compare( + hook_result = self._pytesthook.pytest_assert_binrepr( op=op_symbol, left=left_result, right=next_result) if hook_result: for new_expl in hook_result: diff --git a/py/_plugin/hookspec.py b/py/_plugin/hookspec.py index f295211cd..9f34c4598 100644 --- a/py/_plugin/hookspec.py +++ b/py/_plugin/hookspec.py @@ -127,13 +127,14 @@ def pytest_sessionfinish(session, exitstatus): # hooks for customising the assert methods # ------------------------------------------------------------------------- -def pytest_assert_compare(op, left, right): - """Customise compare assertion +def pytest_assert_binrepr(op, left, right): + """Customise explanation for binary operators - Return None or an empty list for no custom compare, otherwise + 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* as string will be escaped. Note that all - but the first line will be indented sligthly. + 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 2816671ab..347f7edb7 100644 --- a/py/_plugin/pytest_assertion.py +++ b/py/_plugin/pytest_assertion.py @@ -33,32 +33,32 @@ def warn_about_missing_assertion(): " (are you using python -O?)") -def pytest_assert_compare(op, left, right): - """Make a specialised explanation for comapare equal""" - if type(left) != type(right): - return None - +def pytest_assert_binrepr(op, left, right): + """Make 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) - issquence = lambda x: isinstance(x, (list, tuple)) + issequence = lambda x: isinstance(x, (list, tuple)) istext = lambda x: isinstance(x, basestring) isdict = lambda x: isinstance(x, dict) - isset = lambda: isinstance(left, set) + isset = lambda x: isinstance(x, set) explanation = None if op == '==': - if istext(left): + if istext(left) and istext(right): explanation = [line.strip('\n') for line in py.std.difflib.ndiff(left.splitlines(), right.splitlines())] - elif issquence(left): + elif issequence(left) and issequence(right): explanation = _compare_eq_sequence(left, right) - elif isset(): + elif isset(left) and isset(right): explanation = _compare_eq_set(left, right) - elif isdict(left): + elif isdict(left) and isdict(right): explanation = _pprint_diff(left, right) + elif op == 'in': + # XXX + pass if not explanation: return None diff --git a/testing/plugin/test_pytest_assertion.py b/testing/plugin/test_pytest_assertion.py index 596de0050..81796dd12 100644 --- a/testing/plugin/test_pytest_assertion.py +++ b/testing/plugin/test_pytest_assertion.py @@ -77,17 +77,17 @@ def test_traceback_failure(testdir): ]) -def test_pytest_assert_compare_called(monkeypatch, hook): +def test_pytest_assert_binrepr_called(monkeypatch, hook): monkeypatch.setattr(py._plugin.pytest_assertion, - 'pytest_assert_compare', hook) + 'pytest_assert_binrepr', hook) interpret('assert 0 == 1', getframe()) assert hook.called -def test_pytest_assert_compare_args(monkeypatch, hook): +def test_pytest_assert_binrepr_args(monkeypatch, hook): print hook.called monkeypatch.setattr(py._plugin.pytest_assertion, - 'pytest_assert_compare', hook) + 'pytest_assert_binrepr', hook) interpret('assert [0, 1] == [0, 2]', getframe()) print hook.called print hook.left @@ -99,32 +99,32 @@ def test_pytest_assert_compare_args(monkeypatch, hook): class TestAssertCompare: def test_different_types(self): - assert plugin.pytest_assert_compare('==', [0, 1], 'foo') is None + assert plugin.pytest_assert_binrepr('==', [0, 1], 'foo') is None def test_summary(self): - summary = plugin.pytest_assert_compare('==', [0, 1], [0, 2])[0] + summary = plugin.pytest_assert_binrepr('==', [0, 1], [0, 2])[0] assert len(summary) < 65 def test_text_diff(self): - diff = plugin.pytest_assert_compare('==', 'spam', 'eggs')[1:] + diff = plugin.pytest_assert_binrepr('==', '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_compare('==', left, right) + diff = plugin.pytest_assert_binrepr('==', left, right) assert '- spam' in diff assert '+ eggs' in diff def test_list(self): - expl = plugin.pytest_assert_compare('==', [0, 1], [0, 2]) + expl = plugin.pytest_assert_binrepr('==', [0, 1], [0, 2]) assert len(expl) > 1 def test_dict(self): - expl = plugin.pytest_assert_compare('==', {'a': 0}, {'a': 1}) + expl = plugin.pytest_assert_binrepr('==', {'a': 0}, {'a': 1}) assert len(expl) > 1 def test_set(self): - expl = plugin.pytest_assert_compare('==', set([0, 1]), set([0, 2])) + expl = plugin.pytest_assert_binrepr('==', set([0, 1]), set([0, 2])) assert len(expl) > 1