diff options
author | Raymond Hettinger <python@rcn.com> | 2010-12-24 11:20:30 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2010-12-24 11:20:30 (GMT) |
commit | 9d668dac6824f73f0edca15aa7b5f5ae7be5b5e2 (patch) | |
tree | 658fcff6ea44385250e86b2f5c188c950f45e517 /Lib | |
parent | f954217458add5e23dff6996de9f022a8d9e3b54 (diff) | |
download | cpython-9d668dac6824f73f0edca15aa7b5f5ae7be5b5e2.zip cpython-9d668dac6824f73f0edca15aa7b5f5ae7be5b5e2.tar.gz cpython-9d668dac6824f73f0edca15aa7b5f5ae7be5b5e2.tar.bz2 |
Put diff output in useful order (when the elements were first seen).
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/unittest/case.py | 9 | ||||
-rw-r--r-- | Lib/unittest/util.py | 15 |
2 files changed, 13 insertions, 11 deletions
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py index 235af82..82b139f 100644 --- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -1023,18 +1023,15 @@ class TestCase(object): expected = collections.Counter(expected_seq) except TypeError: # Handle case with unhashable elements - differences = _count_diff_all_purpose(expected_seq, actual_seq) + differences = _count_diff_all_purpose(actual_seq, expected_seq) else: if actual == expected: return - differences = _count_diff_hashable(expected_seq, actual_seq) + differences = _count_diff_hashable(actual_seq, expected_seq) if differences: standardMsg = 'Element counts were not equal:\n' - lines = [] - for act, exp, elem in differences: - line = 'Expected %d, got %d: %r' % (exp, act, elem) - lines.append(line) + lines = ['Got %d, expected %d: %r' % diff for diff in differences] diffMsg = '\n'.join(lines) standardMsg = self._truncateMessage(standardMsg, diffMsg) msg = self._formatMessage(msg, standardMsg) diff --git a/Lib/unittest/util.py b/Lib/unittest/util.py index 0407ae9..9dd147a 100644 --- a/Lib/unittest/util.py +++ b/Lib/unittest/util.py @@ -1,6 +1,6 @@ """Various utility functions.""" -from collections import namedtuple, Counter +from collections import namedtuple, OrderedDict __unittest = True @@ -116,15 +116,20 @@ def _count_diff_all_purpose(actual, expected): result.append(diff) return result +def ordered_count(iterable): + 'Return dict of element counts, in the order they were first seen' + c = OrderedDict() + for elem in iterable: + c[elem] = c.get(elem, 0) + 1 + return c + def _count_diff_hashable(actual, expected): 'Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ' # elements must be hashable - s, t = Counter(actual), Counter(expected) - if s == t: - return [] + s, t = ordered_count(actual), ordered_count(expected) result = [] for elem, cnt_s in s.items(): - cnt_t = t[elem] + cnt_t = t.get(elem, 0) if cnt_s != cnt_t: diff = _Mismatch(cnt_s, cnt_t, elem) result.append(diff) |