Minor style cleanup

This commit is contained in:
Floris Bruynooghe 2013-04-28 20:56:56 +01:00
parent b2cb93e06d
commit 3c317dc35e
1 changed files with 16 additions and 13 deletions

View File

@ -10,6 +10,7 @@ BuiltinAssertionError = py.builtin.builtins.AssertionError
# DebugInterpreter. # DebugInterpreter.
_reprcompare = None _reprcompare = None
def format_explanation(explanation): def format_explanation(explanation):
"""This formats an explanation """This formats an explanation
@ -85,7 +86,7 @@ except NameError:
def assertrepr_compare(config, op, left, right): def assertrepr_compare(config, op, left, right):
"""Return specialised explanations for some operators/operands""" """Return specialised explanations for some operators/operands"""
width = 80 - 15 - len(op) - 2 # 15 chars indentation, 1 space around op width = 80 - 15 - len(op) - 2 # 15 chars indentation, 1 space around op
left_repr = py.io.saferepr(left, maxsize=int(width/2)) left_repr = py.io.saferepr(left, maxsize=int(width/2))
right_repr = py.io.saferepr(right, maxsize=width-len(left_repr)) right_repr = py.io.saferepr(right, maxsize=width-len(left_repr))
summary = '%s %s %s' % (left_repr, op, right_repr) summary = '%s %s %s' % (left_repr, op, right_repr)
@ -114,9 +115,9 @@ def assertrepr_compare(config, op, left, right):
raise raise
except: except:
excinfo = py.code.ExceptionInfo() excinfo = py.code.ExceptionInfo()
explanation = ['(pytest_assertion plugin: representation of ' explanation = [
'details failed. Probably an object has a faulty __repr__.)', '(pytest_assertion plugin: representation of details failed. '
str(excinfo)] 'Probably an object has a faulty __repr__.)', str(excinfo)]
if not explanation: if not explanation:
return None return None
@ -132,7 +133,7 @@ def _diff_text(left, right, verbose=False):
""" """
explanation = [] explanation = []
if not verbose: if not verbose:
i = 0 # just in case left or right has zero length i = 0 # just in case left or right has zero length
for i in range(min(len(left), len(right))): for i in range(min(len(left), len(right))):
if left[i] != right[i]: if left[i] != right[i]:
break break
@ -166,13 +167,15 @@ def _compare_eq_sequence(left, right, verbose=False):
(i, left[i], right[i])] (i, left[i], right[i])]
break break
if len(left) > len(right): if len(left) > len(right):
explanation += ['Left contains more items, ' explanation += [
'first extra item: %s' % py.io.saferepr(left[len(right)],)] 'Left contains more items, first extra item: %s' %
py.io.saferepr(left[len(right)],)]
elif len(left) < len(right): elif len(left) < len(right):
explanation += ['Right contains more items, ' explanation += [
'first extra item: %s' % py.io.saferepr(right[len(left)],)] 'Right contains more items, first extra item: %s' %
return explanation # + _diff_text(py.std.pprint.pformat(left), py.io.saferepr(right[len(left)],)]
# py.std.pprint.pformat(right)) return explanation # + _diff_text(py.std.pprint.pformat(left),
# py.std.pprint.pformat(right))
def _compare_eq_set(left, right, verbose=False): def _compare_eq_set(left, right, verbose=False):
@ -210,12 +213,12 @@ def _compare_eq_dict(left, right, verbose=False):
if extra_left: if extra_left:
explanation.append('Left contains more items:') explanation.append('Left contains more items:')
explanation.extend(py.std.pprint.pformat( explanation.extend(py.std.pprint.pformat(
dict((k, left[k]) for k in extra_left)).splitlines()) dict((k, left[k]) for k in extra_left)).splitlines())
extra_right = set(right) - set(left) extra_right = set(right) - set(left)
if extra_right: if extra_right:
explanation.append('Right contains more items:') explanation.append('Right contains more items:')
explanation.extend(py.std.pprint.pformat( explanation.extend(py.std.pprint.pformat(
dict((k, right[k]) for k in extra_right)).splitlines()) dict((k, right[k]) for k in extra_right)).splitlines())
return explanation return explanation