diff options
author | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-03-20 16:58:04 (GMT) |
---|---|---|
committer | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-03-20 16:58:04 (GMT) |
commit | 8442a606b82683121fc38b462f54754e22be3535 (patch) | |
tree | 3ea3861e6e50d86a0ce57bddfb3474c2156d7ad4 /Lib/test/test_unittest.py | |
parent | a087963578b29e7360564e87d7ba703eabbd6487 (diff) | |
download | cpython-8442a606b82683121fc38b462f54754e22be3535.zip cpython-8442a606b82683121fc38b462f54754e22be3535.tar.gz cpython-8442a606b82683121fc38b462f54754e22be3535.tar.bz2 |
Adding assertItemsEqual with tests. Issue 7832. assertSameElements still needs to be deprecated plus documentation needs to be updated.
Diffstat (limited to 'Lib/test/test_unittest.py')
-rw-r--r-- | Lib/test/test_unittest.py | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py index 453c48d..cf734f7 100644 --- a/Lib/test/test_unittest.py +++ b/Lib/test/test_unittest.py @@ -2775,6 +2775,47 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): self.assertRaises(self.failureException, self.assertSameElements, [{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 2}]) + + def testAssertItemsEqual(self): + a = object() + self.assertItemsEqual([1, 2, 3], [3, 2, 1]) + self.assertItemsEqual(['foo', 'bar', 'baz'], ['bar', 'baz', 'foo']) + self.assertItemsEqual([a, a, 2, 2, 3], (a, 2, 3, a, 2)) + self.assertItemsEqual([1, "2", "a", "a"], ["a", "2", True, "a"]) + self.assertRaises(self.failureException, self.assertItemsEqual, + [1, 2] + [3] * 100, [1] * 100 + [2, 3]) + self.assertRaises(self.failureException, self.assertItemsEqual, + [1, "2", "a", "a"], ["a", "2", True, 1]) + self.assertRaises(self.failureException, self.assertItemsEqual, + [10], [10, 11]) + self.assertRaises(self.failureException, self.assertItemsEqual, + [10, 11], [10]) + self.assertRaises(self.failureException, self.assertItemsEqual, + [10, 11, 10], [10, 11]) + + # Test that sequences of unhashable objects can be tested for sameness: + self.assertItemsEqual([[1, 2], [3, 4], 0], [False, [3, 4], [1, 2]]) + + # hashable types, but not orderable + self.assertRaises(self.failureException, self.assertItemsEqual, + [], [divmod, 'x', 1, 5j, 2j, frozenset()]) + # comparing dicts raises a py3k warning + self.assertItemsEqual([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}]) + # comparing heterogenous non-hashable sequences raises a py3k warning + self.assertItemsEqual([1, 'x', divmod, []], [divmod, [], 'x', 1]) + self.assertRaises(self.failureException, self.assertItemsEqual, + [], [divmod, [], 'x', 1, 5j, 2j, set()]) + self.assertRaises(self.failureException, self.assertItemsEqual, + [[1]], [[2]]) + + # Same elements, but not same sequence length + self.assertRaises(self.failureException, self.assertItemsEqual, + [1, 1, 2], [2, 1]) + self.assertRaises(self.failureException, self.assertItemsEqual, + [1, 1, "2", "a", "a"], ["2", "2", True, "a"]) + self.assertRaises(self.failureException, self.assertItemsEqual, + [1, {'b': 2}, None, True], [{'b': 2}, True, None]) + def testAssertSetEqual(self): set1 = set() set2 = set() @@ -3338,8 +3379,8 @@ class TestLongMessage(TestCase): "^Missing: 'key'$", "^Missing: 'key' : oops$"]) - def testAssertSameElements(self): - self.assertMessages('assertSameElements', ([], [None]), + def testAssertItemsEqual(self): + self.assertMessages('assertItemsEqual', ([], [None]), [r"\[None\]$", "^oops$", r"\[None\]$", r"\[None\] : oops$"]) |