Issue 7832: renaming unittest.TestCase.assertSameElements to assertItemsEqual and changing behaviour

This commit is contained in:
Michael Foord 2010-03-20 03:00:34 +00:00
parent 2e6d2622bd
commit 98e7b7644b
6 changed files with 144 additions and 68 deletions

View file

@ -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