2010-11-06 18:38:53 +08:00
|
|
|
"""
|
|
|
|
support for presented detailed information in failing assertions.
|
|
|
|
"""
|
2009-08-27 23:26:02 +08:00
|
|
|
import py
|
2009-08-29 01:16:15 +08:00
|
|
|
import sys
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.monkeypatch import monkeypatch
|
2009-08-27 23:26:02 +08:00
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup("debugconfig")
|
2010-07-27 03:15:15 +08:00
|
|
|
group._addoption('--no-assert', action="store_true", default=False,
|
|
|
|
dest="noassert",
|
2009-08-27 23:26:02 +08:00
|
|
|
help="disable python assert expression reinterpretation."),
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
2011-02-16 07:24:18 +08:00
|
|
|
# The _reprcompare attribute on the py.code module is used by
|
2010-09-22 07:26:12 +08:00
|
|
|
# py._code._assertionnew to detect this plugin was loaded and in
|
|
|
|
# turn call the hooks defined here as part of the
|
|
|
|
# DebugInterpreter.
|
2010-11-07 06:45:48 +08:00
|
|
|
config._monkeypatch = m = monkeypatch()
|
2010-12-09 18:00:31 +08:00
|
|
|
warn_about_missing_assertion()
|
2009-09-01 02:06:55 +08:00
|
|
|
if not config.getvalue("noassert") and not config.getvalue("nomagic"):
|
2010-10-03 00:47:39 +08:00
|
|
|
def callbinrepr(op, left, right):
|
2010-10-03 01:00:47 +08:00
|
|
|
hook_result = config.hook.pytest_assertrepr_compare(
|
2010-10-03 00:47:39 +08:00
|
|
|
config=config, op=op, left=left, right=right)
|
|
|
|
for new_expl in hook_result:
|
|
|
|
if new_expl:
|
|
|
|
return '\n~'.join(new_expl)
|
2010-11-07 06:45:48 +08:00
|
|
|
m.setattr(py.builtin.builtins,
|
|
|
|
'AssertionError', py.code._AssertionError)
|
|
|
|
m.setattr(py.code, '_reprcompare', callbinrepr)
|
2009-08-27 23:26:02 +08:00
|
|
|
|
|
|
|
def pytest_unconfigure(config):
|
2010-11-07 06:45:48 +08:00
|
|
|
config._monkeypatch.undo()
|
2009-08-27 23:26:02 +08:00
|
|
|
|
|
|
|
def warn_about_missing_assertion():
|
|
|
|
try:
|
|
|
|
assert False
|
|
|
|
except AssertionError:
|
|
|
|
pass
|
|
|
|
else:
|
2010-12-09 18:00:31 +08:00
|
|
|
sys.stderr.write("WARNING: failing tests may report as passing because "
|
|
|
|
"assertions are turned off! (are you using python -O?)\n")
|
2010-09-07 02:35:17 +08:00
|
|
|
|
2010-09-23 01:42:04 +08:00
|
|
|
# Provide basestring in python3
|
|
|
|
try:
|
|
|
|
basestring = basestring
|
|
|
|
except NameError:
|
|
|
|
basestring = str
|
|
|
|
|
|
|
|
|
2010-10-03 01:00:47 +08:00
|
|
|
def pytest_assertrepr_compare(op, left, right):
|
|
|
|
"""return specialised explanations for some operators/operands"""
|
2010-12-10 09:03:26 +08:00
|
|
|
width = 80 - 15 - len(op) - 2 # 15 chars indentation, 1 space around op
|
2011-02-16 07:24:18 +08:00
|
|
|
left_repr = py.io.saferepr(left, maxsize=int(width/2))
|
2010-12-10 09:03:26 +08:00
|
|
|
right_repr = py.io.saferepr(right, maxsize=width-len(left_repr))
|
2010-09-16 08:06:07 +08:00
|
|
|
summary = '%s %s %s' % (left_repr, op, right_repr)
|
|
|
|
|
2010-09-22 07:56:39 +08:00
|
|
|
issequence = lambda x: isinstance(x, (list, tuple))
|
2010-09-07 02:35:17 +08:00
|
|
|
istext = lambda x: isinstance(x, basestring)
|
|
|
|
isdict = lambda x: isinstance(x, dict)
|
2010-09-22 07:56:39 +08:00
|
|
|
isset = lambda x: isinstance(x, set)
|
2010-09-16 08:06:07 +08:00
|
|
|
|
|
|
|
explanation = None
|
2010-10-05 00:49:30 +08:00
|
|
|
try:
|
|
|
|
if op == '==':
|
|
|
|
if istext(left) and istext(right):
|
|
|
|
explanation = _diff_text(left, right)
|
|
|
|
elif issequence(left) and issequence(right):
|
|
|
|
explanation = _compare_eq_sequence(left, right)
|
|
|
|
elif isset(left) and isset(right):
|
|
|
|
explanation = _compare_eq_set(left, right)
|
|
|
|
elif isdict(left) and isdict(right):
|
|
|
|
explanation = _diff_text(py.std.pprint.pformat(left),
|
|
|
|
py.std.pprint.pformat(right))
|
2010-12-10 09:03:26 +08:00
|
|
|
elif op == 'not in':
|
|
|
|
if istext(left) and istext(right):
|
|
|
|
explanation = _notin_text(left, right)
|
2010-10-05 00:49:30 +08:00
|
|
|
except py.builtin._sysex:
|
|
|
|
raise
|
|
|
|
except:
|
2010-10-09 13:35:28 +08:00
|
|
|
excinfo = py.code.ExceptionInfo()
|
2010-10-05 00:49:30 +08:00
|
|
|
explanation = ['(pytest_assertion plugin: representation of '
|
2010-10-09 13:35:28 +08:00
|
|
|
'details failed. Probably an object has a faulty __repr__.)',
|
|
|
|
str(excinfo)
|
|
|
|
]
|
|
|
|
|
2010-09-16 08:06:07 +08:00
|
|
|
|
|
|
|
if not explanation:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Don't include pageloads of data, should be configurable
|
|
|
|
if len(''.join(explanation)) > 80*8:
|
|
|
|
explanation = ['Detailed information too verbose, truncated']
|
|
|
|
|
|
|
|
return [summary] + explanation
|
2010-09-07 02:35:17 +08:00
|
|
|
|
|
|
|
|
2010-10-01 06:15:41 +08:00
|
|
|
def _diff_text(left, right):
|
|
|
|
"""Return the explanation for the diff between text
|
|
|
|
|
|
|
|
This will skip leading and trailing characters which are
|
|
|
|
identical to keep the diff minimal.
|
|
|
|
"""
|
|
|
|
explanation = []
|
2010-10-09 13:35:28 +08:00
|
|
|
i = 0 # just in case left or right has zero length
|
2010-10-01 06:15:41 +08:00
|
|
|
for i in range(min(len(left), len(right))):
|
|
|
|
if left[i] != right[i]:
|
|
|
|
break
|
|
|
|
if i > 42:
|
|
|
|
i -= 10 # Provide some context
|
|
|
|
explanation = ['Skipping %s identical '
|
|
|
|
'leading characters in diff' % i]
|
|
|
|
left = left[i:]
|
|
|
|
right = right[i:]
|
|
|
|
if len(left) == len(right):
|
|
|
|
for i in range(len(left)):
|
|
|
|
if left[-i] != right[-i]:
|
|
|
|
break
|
|
|
|
if i > 42:
|
|
|
|
i -= 10 # Provide some context
|
|
|
|
explanation += ['Skipping %s identical '
|
|
|
|
'trailing characters in diff' % i]
|
|
|
|
left = left[:-i]
|
|
|
|
right = right[:-i]
|
|
|
|
explanation += [line.strip('\n')
|
|
|
|
for line in py.std.difflib.ndiff(left.splitlines(),
|
|
|
|
right.splitlines())]
|
|
|
|
return explanation
|
|
|
|
|
|
|
|
|
2010-09-07 02:35:17 +08:00
|
|
|
def _compare_eq_sequence(left, right):
|
|
|
|
explanation = []
|
2010-09-23 01:42:04 +08:00
|
|
|
for i in range(min(len(left), len(right))):
|
2010-09-07 02:35:17 +08:00
|
|
|
if left[i] != right[i]:
|
2010-10-05 00:49:30 +08:00
|
|
|
explanation += ['At index %s diff: %r != %r' %
|
2010-09-07 02:35:17 +08:00
|
|
|
(i, left[i], right[i])]
|
|
|
|
break
|
|
|
|
if len(left) > len(right):
|
|
|
|
explanation += ['Left contains more items, '
|
2010-10-05 00:49:30 +08:00
|
|
|
'first extra item: %s' % py.io.saferepr(left[len(right)],)]
|
2010-09-07 02:35:17 +08:00
|
|
|
elif len(left) < len(right):
|
|
|
|
explanation += ['Right contains more items, '
|
2010-10-05 00:49:30 +08:00
|
|
|
'first extra item: %s' % py.io.saferepr(right[len(left)],)]
|
|
|
|
return explanation # + _diff_text(py.std.pprint.pformat(left),
|
|
|
|
# py.std.pprint.pformat(right))
|
2010-09-16 08:06:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _compare_eq_set(left, right):
|
|
|
|
explanation = []
|
|
|
|
diff_left = left - right
|
|
|
|
diff_right = right - left
|
|
|
|
if diff_left:
|
|
|
|
explanation.append('Extra items in the left set:')
|
|
|
|
for item in diff_left:
|
|
|
|
explanation.append(py.io.saferepr(item))
|
|
|
|
if diff_right:
|
|
|
|
explanation.append('Extra items in the right set:')
|
|
|
|
for item in diff_right:
|
|
|
|
explanation.append(py.io.saferepr(item))
|
|
|
|
return explanation
|
2010-12-10 09:03:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _notin_text(term, text):
|
|
|
|
index = text.find(term)
|
|
|
|
head = text[:index]
|
|
|
|
tail = text[index+len(term):]
|
|
|
|
correct_text = head + tail
|
2011-01-26 04:47:34 +08:00
|
|
|
diff = _diff_text(correct_text, text)
|
|
|
|
newdiff = ['%s is contained here:' % py.io.saferepr(term, maxsize=42)]
|
|
|
|
for line in diff:
|
|
|
|
if line.startswith('Skipping'):
|
|
|
|
continue
|
|
|
|
if line.startswith('- '):
|
|
|
|
continue
|
|
|
|
if line.startswith('+ '):
|
|
|
|
newdiff.append(' ' + line[2:])
|
|
|
|
else:
|
|
|
|
newdiff.append(line)
|
|
|
|
return newdiff
|