Kaydet (Commit) a5809c84 authored tarafından Michael Foord's avatar Michael Foord

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
üst 270a9ceb
...@@ -2392,8 +2392,6 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): ...@@ -2392,8 +2392,6 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
self.assertRaises(self.failureException, self.assertEqual, a, b, self.assertRaises(self.failureException, self.assertEqual, a, b,
msg='foo') msg='foo')
# The fact that dictionaries are unorderable breaks this test for them.
@unittest.expectedFailure
def testEquality(self): def testEquality(self):
self.assertListEqual([], []) self.assertListEqual([], [])
self.assertTupleEqual((), ()) self.assertTupleEqual((), ())
...@@ -2459,6 +2457,8 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): ...@@ -2459,6 +2457,8 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
self.assertSameElements([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}]) self.assertSameElements([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}])
self.assertRaises(self.failureException, self.assertSameElements, self.assertRaises(self.failureException, self.assertSameElements,
[[1]], [[2]]) [[1]], [[2]])
self.assertRaises(self.failureException, self.assertSameElements,
[{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 2}])
def testAssertSetEqual(self): def testAssertSetEqual(self):
set1 = set() set1 = set()
......
...@@ -858,9 +858,13 @@ class TestCase(object): ...@@ -858,9 +858,13 @@ class TestCase(object):
# not hashable. # not hashable.
expected = list(expected_seq) expected = list(expected_seq)
actual = list(actual_seq) actual = list(actual_seq)
expected.sort() try:
actual.sort() expected.sort()
missing, unexpected = _SortedListDifference(expected, actual) actual.sort()
except TypeError:
missing, unexpected = _UnorderableListDifference(expected, actual)
else:
missing, unexpected = _SortedListDifference(expected, actual)
errors = [] errors = []
if missing: if missing:
errors.append('Expected, but missing:\n %r' % missing) errors.append('Expected, but missing:\n %r' % missing)
...@@ -985,6 +989,22 @@ def _SortedListDifference(expected, actual): ...@@ -985,6 +989,22 @@ def _SortedListDifference(expected, actual):
break break
return missing, unexpected 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): class TestSuite(object):
"""A test suite is a composite test consisting of a number of TestCases. """A test suite is a composite test consisting of a number of TestCases.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment