Rename pytest_assert_compare to pytest_assert_binrepr
Holger prefers to only have one hook and it also turns out that "in" is actually a ast.Compare node as well too. This also modifies the pytest_assert_binrepr hook slightly so that it's more accomodating to other operators then just compare (i.e. don't bail out as soon as the types of the operands differ). --HG-- branch : trunk
This commit is contained in:
@@ -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.
|
||||
"""
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user