blob: 39dd196c344e2ab8bf86c2c5fba19fdf540bab61 [file] [log] [blame]
Guido van Rossum3eccc481999-03-26 15:32:05 +00001# Check every path through every method of UserDict
2
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support, mapping_tests
Raymond Hettinger554c8b82008-02-05 22:54:43 +00004import collections
Guido van Rossum3eccc481999-03-26 15:32:05 +00005
6d0 = {}
7d1 = {"one": 1}
8d2 = {"one": 1, "two": 2}
Raymond Hettingere4827eb2002-11-27 08:29:11 +00009d3 = {"one": 1, "two": 3, "three": 5}
10d4 = {"one": None, "two": None}
11d5 = {"one": 1, "two": 1}
Guido van Rossum3eccc481999-03-26 15:32:05 +000012
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +000013class UserDictTest(mapping_tests.TestHashMappingProtocol):
Raymond Hettinger554c8b82008-02-05 22:54:43 +000014 type2test = collections.UserDict
Raymond Hettinger2c2d3222003-03-09 07:05:43 +000015
Walter Dörwalde28be592003-01-19 23:26:59 +000016 def test_all(self):
17 # Test constructors
Raymond Hettinger554c8b82008-02-05 22:54:43 +000018 u = collections.UserDict()
19 u0 = collections.UserDict(d0)
20 u1 = collections.UserDict(d1)
21 u2 = collections.UserDict(d2)
Guido van Rossum3eccc481999-03-26 15:32:05 +000022
Raymond Hettinger554c8b82008-02-05 22:54:43 +000023 uu = collections.UserDict(u)
24 uu0 = collections.UserDict(u0)
25 uu1 = collections.UserDict(u1)
26 uu2 = collections.UserDict(u2)
Guido van Rossum3eccc481999-03-26 15:32:05 +000027
Walter Dörwalde28be592003-01-19 23:26:59 +000028 # keyword arg constructor
Raymond Hettinger554c8b82008-02-05 22:54:43 +000029 self.assertEqual(collections.UserDict(one=1, two=2), d2)
Walter Dörwalde28be592003-01-19 23:26:59 +000030 # item sequence constructor
Raymond Hettinger554c8b82008-02-05 22:54:43 +000031 self.assertEqual(collections.UserDict([('one',1), ('two',2)]), d2)
32 self.assertEqual(collections.UserDict(dict=[('one',1), ('two',2)]), d2)
Walter Dörwalde28be592003-01-19 23:26:59 +000033 # both together
Raymond Hettinger554c8b82008-02-05 22:54:43 +000034 self.assertEqual(collections.UserDict([('one',1), ('two',2)], two=3, three=5), d3)
Guido van Rossum3eccc481999-03-26 15:32:05 +000035
Walter Dörwalde28be592003-01-19 23:26:59 +000036 # alternate constructor
Raymond Hettinger554c8b82008-02-05 22:54:43 +000037 self.assertEqual(collections.UserDict.fromkeys('one two'.split()), d4)
38 self.assertEqual(collections.UserDict().fromkeys('one two'.split()), d4)
39 self.assertEqual(collections.UserDict.fromkeys('one two'.split(), 1), d5)
40 self.assertEqual(collections.UserDict().fromkeys('one two'.split(), 1), d5)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000041 self.assertTrue(u1.fromkeys('one two'.split()) is not u1)
42 self.assertTrue(isinstance(u1.fromkeys('one two'.split()), collections.UserDict))
43 self.assertTrue(isinstance(u2.fromkeys('one two'.split()), collections.UserDict))
Raymond Hettingere4827eb2002-11-27 08:29:11 +000044
Walter Dörwalde28be592003-01-19 23:26:59 +000045 # Test __repr__
46 self.assertEqual(str(u0), str(d0))
47 self.assertEqual(repr(u1), repr(d1))
Brett Cannon0b70cca2006-08-25 02:59:59 +000048 self.assertEqual(repr(u2), repr(d2))
Raymond Hettinger54405452002-11-22 00:07:40 +000049
Mark Dickinsona56c4672009-01-27 18:17:45 +000050 # Test rich comparison and __len__
Walter Dörwalde28be592003-01-19 23:26:59 +000051 all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2]
52 for a in all:
53 for b in all:
Guido van Rossum47b9ff62006-08-24 00:41:19 +000054 self.assertEqual(a == b, len(a) == len(b))
Guido van Rossum3eccc481999-03-26 15:32:05 +000055
Walter Dörwalde28be592003-01-19 23:26:59 +000056 # Test __getitem__
57 self.assertEqual(u2["one"], 1)
58 self.assertRaises(KeyError, u1.__getitem__, "two")
Guido van Rossum3eccc481999-03-26 15:32:05 +000059
Walter Dörwalde28be592003-01-19 23:26:59 +000060 # Test __setitem__
Raymond Hettinger554c8b82008-02-05 22:54:43 +000061 u3 = collections.UserDict(u2)
Walter Dörwalde28be592003-01-19 23:26:59 +000062 u3["two"] = 2
63 u3["three"] = 3
Guido van Rossum3eccc481999-03-26 15:32:05 +000064
Walter Dörwalde28be592003-01-19 23:26:59 +000065 # Test __delitem__
66 del u3["three"]
67 self.assertRaises(KeyError, u3.__delitem__, "three")
Guido van Rossum3eccc481999-03-26 15:32:05 +000068
Walter Dörwalde28be592003-01-19 23:26:59 +000069 # Test clear
70 u3.clear()
71 self.assertEqual(u3, {})
Guido van Rossum3eccc481999-03-26 15:32:05 +000072
Walter Dörwalde28be592003-01-19 23:26:59 +000073 # Test copy()
74 u2a = u2.copy()
75 self.assertEqual(u2a, u2)
Raymond Hettinger554c8b82008-02-05 22:54:43 +000076 u2b = collections.UserDict(x=42, y=23)
Walter Dörwalde28be592003-01-19 23:26:59 +000077 u2c = u2b.copy() # making a copy of a UserDict is special cased
78 self.assertEqual(u2b, u2c)
Guido van Rossum3eccc481999-03-26 15:32:05 +000079
Raymond Hettinger554c8b82008-02-05 22:54:43 +000080 class MyUserDict(collections.UserDict):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000081 def display(self): print(self)
Guido van Rossum3eccc481999-03-26 15:32:05 +000082
Walter Dörwalde28be592003-01-19 23:26:59 +000083 m2 = MyUserDict(u2)
84 m2a = m2.copy()
85 self.assertEqual(m2a, m2)
Guido van Rossum3eccc481999-03-26 15:32:05 +000086
Walter Dörwalde28be592003-01-19 23:26:59 +000087 # SF bug #476616 -- copy() of UserDict subclass shared data
88 m2['foo'] = 'bar'
89 self.assertNotEqual(m2a, m2)
Guido van Rossum3eccc481999-03-26 15:32:05 +000090
Walter Dörwalde28be592003-01-19 23:26:59 +000091 # Test keys, items, values
92 self.assertEqual(u2.keys(), d2.keys())
93 self.assertEqual(u2.items(), d2.items())
Guido van Rossumd81206d2007-02-15 03:49:08 +000094 self.assertEqual(list(u2.values()), list(d2.values()))
Guido van Rossum3eccc481999-03-26 15:32:05 +000095
Guido van Rossume2b70bc2006-08-18 22:13:04 +000096 # Test "in".
Walter Dörwalde28be592003-01-19 23:26:59 +000097 for i in u2.keys():
Benjamin Peterson577473f2010-01-19 00:09:57 +000098 self.assertIn(i, u2)
Walter Dörwalde28be592003-01-19 23:26:59 +000099 self.assertEqual(i in u1, i in d1)
Walter Dörwalde28be592003-01-19 23:26:59 +0000100 self.assertEqual(i in u0, i in d0)
Guido van Rossum3eccc481999-03-26 15:32:05 +0000101
Walter Dörwalde28be592003-01-19 23:26:59 +0000102 # Test update
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000103 t = collections.UserDict()
Walter Dörwalde28be592003-01-19 23:26:59 +0000104 t.update(u2)
105 self.assertEqual(t, u2)
Guido van Rossum3eccc481999-03-26 15:32:05 +0000106
Walter Dörwalde28be592003-01-19 23:26:59 +0000107 # Test get
108 for i in u2.keys():
109 self.assertEqual(u2.get(i), u2[i])
110 self.assertEqual(u1.get(i), d1.get(i))
111 self.assertEqual(u0.get(i), d0.get(i))
Guido van Rossum3eccc481999-03-26 15:32:05 +0000112
Walter Dörwalde28be592003-01-19 23:26:59 +0000113 # Test "in" iteration.
Guido van Rossum805365e2007-05-07 22:24:25 +0000114 for i in range(20):
Walter Dörwalde28be592003-01-19 23:26:59 +0000115 u2[i] = str(i)
116 ikeys = []
117 for k in u2:
118 ikeys.append(k)
Walter Dörwalde28be592003-01-19 23:26:59 +0000119 keys = u2.keys()
Raymond Hettingera690a992003-11-16 16:17:49 +0000120 self.assertEqual(set(ikeys), set(keys))
Guido van Rossum3eccc481999-03-26 15:32:05 +0000121
Walter Dörwalde28be592003-01-19 23:26:59 +0000122 # Test setdefault
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000123 t = collections.UserDict()
Walter Dörwalde28be592003-01-19 23:26:59 +0000124 self.assertEqual(t.setdefault("x", 42), 42)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000125 self.assertIn("x", t)
Walter Dörwalde28be592003-01-19 23:26:59 +0000126 self.assertEqual(t.setdefault("x", 23), 42)
Guido van Rossum3eccc481999-03-26 15:32:05 +0000127
Walter Dörwalde28be592003-01-19 23:26:59 +0000128 # Test pop
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000129 t = collections.UserDict(x=42)
Walter Dörwalde28be592003-01-19 23:26:59 +0000130 self.assertEqual(t.pop("x"), 42)
131 self.assertRaises(KeyError, t.pop, "x")
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +0000132 self.assertEqual(t.pop("x", 1), 1)
133 t["x"] = 42
134 self.assertEqual(t.pop("x", 1), 42)
Guido van Rossum3eccc481999-03-26 15:32:05 +0000135
Walter Dörwalde28be592003-01-19 23:26:59 +0000136 # Test popitem
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000137 t = collections.UserDict(x=42)
Walter Dörwalde28be592003-01-19 23:26:59 +0000138 self.assertEqual(t.popitem(), ("x", 42))
139 self.assertRaises(KeyError, t.popitem)
Raymond Hettinger903bf902002-11-15 08:39:40 +0000140
Guido van Rossum1968ad32006-02-25 22:38:04 +0000141 def test_missing(self):
142 # Make sure UserDict doesn't have a __missing__ method
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000143 self.assertEqual(hasattr(collections.UserDict, "__missing__"), False)
Guido van Rossum1968ad32006-02-25 22:38:04 +0000144 # Test several cases:
145 # (D) subclass defines __missing__ method returning a value
146 # (E) subclass defines __missing__ method raising RuntimeError
147 # (F) subclass sets __missing__ instance variable (no effect)
148 # (G) subclass doesn't define __missing__ at a all
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000149 class D(collections.UserDict):
Guido van Rossum1968ad32006-02-25 22:38:04 +0000150 def __missing__(self, key):
151 return 42
152 d = D({1: 2, 3: 4})
153 self.assertEqual(d[1], 2)
154 self.assertEqual(d[3], 4)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000155 self.assertNotIn(2, d)
156 self.assertNotIn(2, d.keys())
Guido van Rossum1968ad32006-02-25 22:38:04 +0000157 self.assertEqual(d[2], 42)
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000158 class E(collections.UserDict):
Guido van Rossum1968ad32006-02-25 22:38:04 +0000159 def __missing__(self, key):
160 raise RuntimeError(key)
161 e = E()
162 try:
163 e[42]
Guido van Rossumb940e112007-01-10 16:19:56 +0000164 except RuntimeError as err:
Guido van Rossum1968ad32006-02-25 22:38:04 +0000165 self.assertEqual(err.args, (42,))
166 else:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000167 self.fail("e[42] didn't raise RuntimeError")
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000168 class F(collections.UserDict):
Guido van Rossum1968ad32006-02-25 22:38:04 +0000169 def __init__(self):
170 # An instance variable __missing__ should have no effect
171 self.__missing__ = lambda key: None
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000172 collections.UserDict.__init__(self)
Guido van Rossum1968ad32006-02-25 22:38:04 +0000173 f = F()
174 try:
175 f[42]
Guido van Rossumb940e112007-01-10 16:19:56 +0000176 except KeyError as err:
Guido van Rossum1968ad32006-02-25 22:38:04 +0000177 self.assertEqual(err.args, (42,))
178 else:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000179 self.fail("f[42] didn't raise KeyError")
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000180 class G(collections.UserDict):
Guido van Rossum1968ad32006-02-25 22:38:04 +0000181 pass
182 g = G()
183 try:
184 g[42]
Guido van Rossumb940e112007-01-10 16:19:56 +0000185 except KeyError as err:
Guido van Rossum1968ad32006-02-25 22:38:04 +0000186 self.assertEqual(err.args, (42,))
187 else:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000188 self.fail("g[42] didn't raise KeyError")
Guido van Rossum1968ad32006-02-25 22:38:04 +0000189
Raymond Hettinger903bf902002-11-15 08:39:40 +0000190
Walter Dörwalde28be592003-01-19 23:26:59 +0000191
192def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000193 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000194 UserDictTest,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000195 )
Walter Dörwalde28be592003-01-19 23:26:59 +0000196
197if __name__ == "__main__":
198 test_main()