mirror of
https://github.com/python/cpython.git
synced 2025-09-02 23:18:25 +00:00
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
This commit is contained in:
parent
270a9ceb5b
commit
a5809c84b3
2 changed files with 25 additions and 5 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue