2007-02-09 21:11:45 -04:00
|
|
|
import unittest
|
2008-05-20 18:35:26 -03:00
|
|
|
from test import support
|
2007-02-09 21:11:45 -04:00
|
|
|
|
|
|
|
class DictSetTest(unittest.TestCase):
|
|
|
|
|
2007-02-10 14:55:06 -04:00
|
|
|
def test_constructors_not_callable(self):
|
2007-02-11 02:12:03 -04:00
|
|
|
kt = type({}.keys())
|
2007-02-10 14:55:06 -04:00
|
|
|
self.assertRaises(TypeError, kt, {})
|
|
|
|
self.assertRaises(TypeError, kt)
|
2007-02-11 02:12:03 -04:00
|
|
|
it = type({}.items())
|
2007-02-10 14:55:06 -04:00
|
|
|
self.assertRaises(TypeError, it, {})
|
|
|
|
self.assertRaises(TypeError, it)
|
2007-02-11 02:12:03 -04:00
|
|
|
vt = type({}.values())
|
2007-02-10 14:55:06 -04:00
|
|
|
self.assertRaises(TypeError, vt, {})
|
|
|
|
self.assertRaises(TypeError, vt)
|
|
|
|
|
2007-02-09 21:11:45 -04:00
|
|
|
def test_dict_keys(self):
|
|
|
|
d = {1: 10, "a": "ABC"}
|
2007-02-11 02:12:03 -04:00
|
|
|
keys = d.keys()
|
2007-02-10 00:54:19 -04:00
|
|
|
self.assertEqual(len(keys), 2)
|
2007-02-11 22:23:40 -04:00
|
|
|
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)
|
2007-02-10 14:55:06 -04:00
|
|
|
self.assert_(1 in keys)
|
|
|
|
self.assert_("a" in keys)
|
|
|
|
self.assert_(10 not in keys)
|
|
|
|
self.assert_("Z" not in keys)
|
2007-02-11 22:23:40 -04:00
|
|
|
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())
|
2007-02-09 21:11:45 -04:00
|
|
|
|
|
|
|
def test_dict_items(self):
|
|
|
|
d = {1: 10, "a": "ABC"}
|
2007-02-11 02:12:03 -04:00
|
|
|
items = d.items()
|
2007-02-10 00:54:19 -04:00
|
|
|
self.assertEqual(len(items), 2)
|
2007-02-11 22:23:40 -04:00
|
|
|
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)
|
2007-02-10 14:55:06 -04:00
|
|
|
self.assert_((1, 10) in items)
|
|
|
|
self.assert_(("a", "ABC") in items)
|
|
|
|
self.assert_((1, 11) not in items)
|
|
|
|
self.assert_(1 not in items)
|
|
|
|
self.assert_(() not in items)
|
|
|
|
self.assert_((1,) not in items)
|
|
|
|
self.assert_((1, 2, 3) not in items)
|
2007-02-11 22:23:40 -04:00
|
|
|
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())
|
2007-02-09 21:11:45 -04:00
|
|
|
|
|
|
|
def test_dict_values(self):
|
|
|
|
d = {1: 10, "a": "ABC"}
|
2007-02-11 02:12:03 -04:00
|
|
|
values = d.values()
|
2007-02-09 21:11:45 -04:00
|
|
|
self.assertEqual(set(values), {10, "ABC"})
|
2007-02-10 00:54:19 -04:00
|
|
|
self.assertEqual(len(values), 2)
|
2007-02-09 21:11:45 -04:00
|
|
|
|
|
|
|
def test_main():
|
2008-05-20 18:35:26 -03:00
|
|
|
support.run_unittest(DictSetTest)
|
2007-02-09 21:11:45 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|