summaryrefslogtreecommitdiffstats
path: root/Lib/unittest.py
diff options
context:
space:
mode:
authorMichael Foord <fuzzyman@voidspace.org.uk>2009-04-04 18:55:09 (GMT)
committerMichael Foord <fuzzyman@voidspace.org.uk>2009-04-04 18:55:09 (GMT)
commita5809c84b392ef855ac18b17967c9f7e34118053 (patch)
treea50c177030a0710e73b9c96c46a4ed57e3feff29 /Lib/unittest.py
parent270a9ceb5b5dd72792ee88d5420417105cedcb2b (diff)
downloadcpython-a5809c84b392ef855ac18b17967c9f7e34118053.zip
cpython-a5809c84b392ef855ac18b17967c9f7e34118053.tar.gz
cpython-a5809c84b392ef855ac18b17967c9f7e34118053.tar.bz2
Patch for Py3k with fallback for comparing unsortable sequences in
assertSameElements. Removed the expected failure and added another test case to confirm that this patch works for unsortable sequences that are the same (no fail) and different (fail). Issue #2578
Diffstat (limited to 'Lib/unittest.py')
-rw-r--r--Lib/unittest.py26
1 files changed, 23 insertions, 3 deletions
diff --git a/Lib/unittest.py b/Lib/unittest.py
index 16a8663..244a45b 100644
--- a/Lib/unittest.py
+++ b/Lib/unittest.py
@@ -858,9 +858,13 @@ class TestCase(object):
# not hashable.
expected = list(expected_seq)
actual = list(actual_seq)
- expected.sort()
- actual.sort()
- missing, unexpected = _SortedListDifference(expected, actual)
+ try:
+ expected.sort()
+ actual.sort()
+ except TypeError:
+ missing, unexpected = _UnorderableListDifference(expected, actual)
+ else:
+ missing, unexpected = _SortedListDifference(expected, actual)
errors = []
if missing:
errors.append('Expected, but missing:\n %r' % missing)
@@ -985,6 +989,22 @@ def _SortedListDifference(expected, actual):
break
return missing, unexpected
+def _UnorderableListDifference(expected, actual):
+ """Same behavior as _SortedListDifference but
+ for lists of unorderable items (like dicts).
+
+ As it does a linear search per item (remove) it
+ has O(n*n) performance."""
+ missing = []
+ while expected:
+ item = expected.pop()
+ try:
+ actual.remove(item)
+ except ValueError:
+ missing.append(item)
+
+ # anything left in actual is unexpected
+ return missing, actual
class TestSuite(object):
"""A test suite is a composite test consisting of a number of TestCases.