summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_dictviews.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-02-12 02:23:40 (GMT)
committerGuido van Rossum <guido@python.org>2007-02-12 02:23:40 (GMT)
commitd9214d1f2cd9402e98c14809d17076e5f1db4167 (patch)
tree532da8aaf06cbaa4e4a3539e40e9506adef619ff /Lib/test/test_dictviews.py
parente19aad4c7b4804b4a13aca3999363f9d33a4fa75 (diff)
downloadcpython-d9214d1f2cd9402e98c14809d17076e5f1db4167.zip
cpython-d9214d1f2cd9402e98c14809d17076e5f1db4167.tar.gz
cpython-d9214d1f2cd9402e98c14809d17076e5f1db4167.tar.bz2
Make dict.keys() and dict.items() comparable to sets, using == and !=.
(PEP 3106 requires subset comparisons too, those will come later if someone really wants them. :-)
Diffstat (limited to 'Lib/test/test_dictviews.py')
-rw-r--r--Lib/test/test_dictviews.py30
1 files changed, 28 insertions, 2 deletions
diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py
index 607d182..3a5fce6 100644
--- a/Lib/test/test_dictviews.py
+++ b/Lib/test/test_dictviews.py
@@ -17,18 +17,33 @@ class DictSetTest(unittest.TestCase):
def test_dict_keys(self):
d = {1: 10, "a": "ABC"}
keys = d.keys()
- self.assertEqual(set(keys), {1, "a"})
self.assertEqual(len(keys), 2)
+ self.assertEqual(set(keys), {1, "a"})
+ self.assertEqual(keys, {1, "a"})
+ self.assertNotEqual(keys, {1, "a", "b"})
+ self.assertNotEqual(keys, {1, "b"})
+ self.assertNotEqual(keys, {1})
+ self.assertNotEqual(keys, 42)
self.assert_(1 in keys)
self.assert_("a" in keys)
self.assert_(10 not in keys)
self.assert_("Z" not in keys)
+ self.assertEqual(d.keys(), d.keys())
+ e = {1: 11, "a": "def"}
+ self.assertEqual(d.keys(), e.keys())
+ del e["a"]
+ self.assertNotEqual(d.keys(), e.keys())
def test_dict_items(self):
d = {1: 10, "a": "ABC"}
items = d.items()
- self.assertEqual(set(items), {(1, 10), ("a", "ABC")})
self.assertEqual(len(items), 2)
+ self.assertEqual(set(items), {(1, 10), ("a", "ABC")})
+ self.assertEqual(items, {(1, 10), ("a", "ABC")})
+ self.assertNotEqual(items, {(1, 10), ("a", "ABC"), "junk"})
+ self.assertNotEqual(items, {(1, 10), ("a", "def")})
+ self.assertNotEqual(items, {(1, 10)})
+ self.assertNotEqual(items, 42)
self.assert_((1, 10) in items)
self.assert_(("a", "ABC") in items)
self.assert_((1, 11) not in items)
@@ -36,6 +51,17 @@ class DictSetTest(unittest.TestCase):
self.assert_(() not in items)
self.assert_((1,) not in items)
self.assert_((1, 2, 3) not in items)
+ self.assertEqual(d.items(), d.items())
+ e = d.copy()
+ self.assertEqual(d.items(), e.items())
+ e["a"] = "def"
+ self.assertNotEqual(d.items(), e.items())
+
+ def test_dict_mixed_keys_items(self):
+ d = {(1, 1): 11, (2, 2): 22}
+ e = {1: 1, 2: 2}
+ self.assertEqual(d.keys(), e.items())
+ self.assertNotEqual(d.items(), e.keys())
def test_dict_values(self):
d = {1: 10, "a": "ABC"}