Commits

Anonymous committed b14e9ec

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

Comments (0)

Files changed (2)

Lib/test/test_unittest.py

             self.assertRaises(self.failureException, self.assertEqual, a, b,
                               msg='foo')
 
-    # The fact that dictionaries are unorderable breaks this test for them.
-    @unittest.expectedFailure
     def testEquality(self):
         self.assertListEqual([], [])
         self.assertTupleEqual((), ())
         self.assertSameElements([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}])
         self.assertRaises(self.failureException, self.assertSameElements,
                           [[1]], [[2]])
+        self.assertRaises(self.failureException, self.assertSameElements,
+                          [{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 2}])
 
     def testAssertSetEqual(self):
         set1 = set()
             # 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)
             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.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.