[Python-checkins] r87474 - in python/branches/py3k/Lib/unittest: case.py util.py

raymond.hettinger python-checkins at python.org
Fri Dec 24 12:20:30 CET 2010


Author: raymond.hettinger
Date: Fri Dec 24 12:20:30 2010
New Revision: 87474

Log:
Put diff output in useful order (when the elements were first seen).

Modified:
   python/branches/py3k/Lib/unittest/case.py
   python/branches/py3k/Lib/unittest/util.py

Modified: python/branches/py3k/Lib/unittest/case.py
==============================================================================
--- python/branches/py3k/Lib/unittest/case.py	(original)
+++ python/branches/py3k/Lib/unittest/case.py	Fri Dec 24 12:20:30 2010
@@ -1023,18 +1023,15 @@
             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)

Modified: python/branches/py3k/Lib/unittest/util.py
==============================================================================
--- python/branches/py3k/Lib/unittest/util.py	(original)
+++ python/branches/py3k/Lib/unittest/util.py	Fri Dec 24 12:20:30 2010
@@ -1,6 +1,6 @@
 """Various utility functions."""
 
-from collections import namedtuple, Counter
+from collections import namedtuple, OrderedDict
 
 __unittest = True
 
@@ -116,15 +116,20 @@
         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)


More information about the Python-checkins mailing list