summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/util.py
diff options
context:
space:
mode:
authorMichael Foord <fuzzyman@voidspace.org.uk>2010-03-20 03:00:34 (GMT)
committerMichael Foord <fuzzyman@voidspace.org.uk>2010-03-20 03:00:34 (GMT)
commit98e7b7644bfa3ecc7919b1a139da299da081f34e (patch)
tree97e7397de6a4d75f8daf3db2ee3d4dfead5c8902 /Lib/unittest/util.py
parent2e6d2622bd793c0eda9e838bb12d98b02ab0db18 (diff)
downloadcpython-98e7b7644bfa3ecc7919b1a139da299da081f34e.zip
cpython-98e7b7644bfa3ecc7919b1a139da299da081f34e.tar.gz
cpython-98e7b7644bfa3ecc7919b1a139da299da081f34e.tar.bz2
Issue 7832: renaming unittest.TestCase.assertSameElements to assertItemsEqual and changing behaviour
Diffstat (limited to 'Lib/unittest/util.py')
-rw-r--r--Lib/unittest/util.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/Lib/unittest/util.py b/Lib/unittest/util.py
index 9fce625..9ddf879 100644
--- a/Lib/unittest/util.py
+++ b/Lib/unittest/util.py
@@ -48,3 +48,40 @@ def sorted_list_difference(expected, actual):
unexpected.extend(actual[j:])
break
return missing, unexpected
+
+
+def unorderable_list_difference(expected, actual, ignore_duplicate=False):
+ """Same behavior as sorted_list_difference but
+ for lists of unorderable items (like dicts).
+
+ As it does a linear search per item (remove) it
+ has O(n*n) performance.
+ """
+ missing = []
+ unexpected = []
+ while expected:
+ item = expected.pop()
+ try:
+ actual.remove(item)
+ except ValueError:
+ missing.append(item)
+ if ignore_duplicate:
+ for lst in expected, actual:
+ try:
+ while True:
+ lst.remove(item)
+ except ValueError:
+ pass
+ if ignore_duplicate:
+ while actual:
+ item = actual.pop()
+ unexpected.append(item)
+ try:
+ while True:
+ actual.remove(item)
+ except ValueError:
+ pass
+ return missing, unexpected
+
+ # anything left in actual is unexpected
+ return missing, actual