[Pytest-commit] commit/pytest: bubenkoff: simpler and more verbose assertions
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Tue Sep 23 23:23:13 CEST 2014
1 new commit in pytest:
https://bitbucket.org/hpk42/pytest/commits/cf8da540f4ed/
Changeset: cf8da540f4ed
Branch: better-diff
User: bubenkoff
Date: 2014-09-23 21:22:57+00:00
Summary: simpler and more verbose assertions
Affected #: 2 files
diff -r fcfcf15459f0e4ab43e8784befcd83ded913c8b2 -r cf8da540f4ed1c305e128cd627933afa2034e710 _pytest/assertion/util.py
--- a/_pytest/assertion/util.py
+++ b/_pytest/assertion/util.py
@@ -1,4 +1,5 @@
"""Utilities for assertion debugging"""
+import difflib
import pprint
import py
@@ -129,11 +130,14 @@
right_repr = py.io.saferepr(right, maxsize=width-len(left_repr))
summary = u('%s %s %s') % (left_repr, op, right_repr)
- issequence = lambda x: (isinstance(x, (list, tuple, Sequence))
- and not isinstance(x, basestring))
istext = lambda x: isinstance(x, basestring)
- isdict = lambda x: isinstance(x, dict)
- isset = lambda x: isinstance(x, (set, frozenset))
+
+ def issequence(obj):
+ try:
+ iter(obj)
+ return not istext(obj)
+ except TypeError:
+ return False
verbose = config.getoption('verbose')
explanation = None
@@ -142,11 +146,7 @@
if istext(left) and istext(right):
explanation = _diff_text(left, right, verbose)
elif issequence(left) and issequence(right):
- explanation = _compare_eq_sequence(left, right, verbose)
- elif isset(left) and isset(right):
- explanation = _compare_eq_set(left, right, verbose)
- elif isdict(left) and isdict(right):
- explanation = _compare_eq_dict(left, right, verbose)
+ explanation = _compare_eq(left, right, verbose)
elif op == 'not in':
if istext(left) and istext(right):
explanation = _notin_text(left, right, verbose)
@@ -203,68 +203,6 @@
return explanation
-def _compare_eq_sequence(left, right, verbose=False):
- explanation = []
- for i in range(min(len(left), len(right))):
- if left[i] != right[i]:
- explanation += [u('At index %s diff: %r != %r')
- % (i, left[i], right[i])]
- break
- if len(left) > len(right):
- explanation += [u('Left contains more items, first extra item: %s')
- % py.io.saferepr(left[len(right)],)]
- elif len(left) < len(right):
- explanation += [
- u('Right contains more items, first extra item: %s') %
- py.io.saferepr(right[len(left)],)]
- return explanation # + _diff_text(pprint.pformat(left),
- # pprint.pformat(right))
-
-
-def _compare_eq_set(left, right, verbose=False):
- explanation = []
- diff_left = left - right
- diff_right = right - left
- if diff_left:
- explanation.append(u('Extra items in the left set:'))
- for item in diff_left:
- explanation.append(py.io.saferepr(item))
- if diff_right:
- explanation.append(u('Extra items in the right set:'))
- for item in diff_right:
- explanation.append(py.io.saferepr(item))
- return explanation
-
-
-def _compare_eq_dict(left, right, verbose=False):
- explanation = []
- common = set(left).intersection(set(right))
- same = dict((k, left[k]) for k in common if left[k] == right[k])
- if same and not verbose:
- explanation += [u('Omitting %s identical items, use -v to show') %
- len(same)]
- elif same:
- explanation += [u('Common items:')]
- explanation += pprint.pformat(same).splitlines()
- diff = set(k for k in common if left[k] != right[k])
- if diff:
- explanation += [u('Differing items:')]
- for k in diff:
- explanation += [py.io.saferepr({k: left[k]}) + ' != ' +
- py.io.saferepr({k: right[k]})]
- extra_left = set(left) - set(right)
- if extra_left:
- explanation.append(u('Left contains more items:'))
- explanation.extend(pprint.pformat(
- dict((k, left[k]) for k in extra_left)).splitlines())
- extra_right = set(right) - set(left)
- if extra_right:
- explanation.append(u('Right contains more items:'))
- explanation.extend(pprint.pformat(
- dict((k, right[k]) for k in extra_right)).splitlines())
- return explanation
-
-
def _notin_text(term, text, verbose=False):
index = text.find(term)
head = text[:index]
@@ -282,3 +220,10 @@
else:
newdiff.append(line)
return newdiff
+
+
+def _compare_eq(left, right, verbose=False):
+ left = pprint.pformat(left).splitlines()
+ right = pprint.pformat(right).splitlines()
+ return list(line.strip() for line in difflib.ndiff(left, right))
+
diff -r fcfcf15459f0e4ab43e8784befcd83ded913c8b2 -r cf8da540f4ed1c305e128cd627933afa2034e710 testing/test_assertion.py
--- a/testing/test_assertion.py
+++ b/testing/test_assertion.py
@@ -96,19 +96,6 @@
expl = callequal({'a': 0}, {'a': 1})
assert len(expl) > 1
- def test_dict_omitting(self):
- lines = callequal({'a': 0, 'b': 1}, {'a': 1, 'b': 1})
- assert lines[1].startswith('Omitting 1 identical item')
- assert 'Common items' not in lines
- for line in lines[1:]:
- assert 'b' not in line
-
- def test_dict_omitting_verbose(self):
- lines = callequal({'a': 0, 'b': 1}, {'a': 1, 'b': 1}, verbose=True)
- assert lines[1].startswith('Common items:')
- assert 'Omitting' not in lines[1]
- assert lines[2] == "{'b': 1}"
-
def test_set(self):
expl = callequal(set([0, 1]), set([0, 2]))
assert len(expl) > 1
@@ -370,7 +357,6 @@
"*truncated*use*-vv*",
])
-
result = testdir.runpytest('-vv')
result.stdout.fnmatch_lines([
"*- 197",
Repository URL: https://bitbucket.org/hpk42/pytest/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the pytest-commit
mailing list