mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Adding assertItemsEqual with tests. Issue 7832. assertSameElements still needs to be deprecated plus documentation needs to be updated.
This commit is contained in:
parent
a087963578
commit
8442a606b8
2 changed files with 81 additions and 2 deletions
|
@ -838,6 +838,44 @@ class TestCase(object):
|
|||
standardMsg = '\n'.join(errors)
|
||||
self.fail(self._formatMessage(msg, standardMsg))
|
||||
|
||||
|
||||
def assertItemsEqual(self, expected_seq, actual_seq, msg=None):
|
||||
"""An unordered sequence / set specific comparison. It asserts that
|
||||
expected_seq and actual_seq contain the same elements. It is
|
||||
the equivalent of::
|
||||
|
||||
self.assertEqual(sorted(expected_seq), sorted(actual_seq))
|
||||
|
||||
Raises with an error message listing which elements of expected_seq
|
||||
are missing from actual_seq and vice versa if any.
|
||||
|
||||
Asserts that each element has the same count in both sequences.
|
||||
Example:
|
||||
- [0, 1, 1] and [1, 0, 1] compare equal.
|
||||
- [0, 0, 1] and [0, 1] compare unequal.
|
||||
"""
|
||||
try:
|
||||
expected = sorted(expected_seq)
|
||||
actual = sorted(actual_seq)
|
||||
except TypeError:
|
||||
# Unsortable items (example: set(), complex(), ...)
|
||||
expected = list(expected_seq)
|
||||
actual = list(actual_seq)
|
||||
missing, unexpected = unorderable_list_difference(expected, actual)
|
||||
else:
|
||||
return self.assertSequenceEqual(expected, actual, msg=msg)
|
||||
|
||||
errors = []
|
||||
if missing:
|
||||
errors.append('Expected, but missing:\n %s' %
|
||||
safe_repr(missing))
|
||||
if unexpected:
|
||||
errors.append('Unexpected, but present:\n %s' %
|
||||
safe_repr(unexpected))
|
||||
if errors:
|
||||
standardMsg = '\n'.join(errors)
|
||||
self.fail(self._formatMessage(msg, standardMsg))
|
||||
|
||||
def assertMultiLineEqual(self, first, second, msg=None):
|
||||
"""Assert that two multi-line strings are equal."""
|
||||
self.assert_(isinstance(first, str), (
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue