blob: f67e55bea116745dbf7500f598452d5021d1ed64 [file] [log] [blame]
Guido van Rossum3eccc481999-03-26 15:32:05 +00001# Check every path through every method of UserDict
2
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +00003import unittest
4from test import test_support, mapping_tests
Walter Dörwalde28be592003-01-19 23:26:59 +00005import UserDict
Guido van Rossum3eccc481999-03-26 15:32:05 +00006
7d0 = {}
8d1 = {"one": 1}
9d2 = {"one": 1, "two": 2}
Raymond Hettingere4827eb2002-11-27 08:29:11 +000010d3 = {"one": 1, "two": 3, "three": 5}
11d4 = {"one": None, "two": None}
12d5 = {"one": 1, "two": 1}
Guido van Rossum3eccc481999-03-26 15:32:05 +000013
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +000014class UserDictTest(mapping_tests.TestHashMappingProtocol):
Raymond Hettinger2c2d3222003-03-09 07:05:43 +000015 _tested_class = UserDict.IterableUserDict
16
Walter Dörwalde28be592003-01-19 23:26:59 +000017 def test_all(self):
18 # Test constructors
19 u = UserDict.UserDict()
20 u0 = UserDict.UserDict(d0)
21 u1 = UserDict.UserDict(d1)
22 u2 = UserDict.IterableUserDict(d2)
Guido van Rossum3eccc481999-03-26 15:32:05 +000023
Walter Dörwalde28be592003-01-19 23:26:59 +000024 uu = UserDict.UserDict(u)
25 uu0 = UserDict.UserDict(u0)
26 uu1 = UserDict.UserDict(u1)
27 uu2 = UserDict.UserDict(u2)
Guido van Rossum3eccc481999-03-26 15:32:05 +000028
Walter Dörwalde28be592003-01-19 23:26:59 +000029 # keyword arg constructor
30 self.assertEqual(UserDict.UserDict(one=1, two=2), d2)
31 # item sequence constructor
32 self.assertEqual(UserDict.UserDict([('one',1), ('two',2)]), d2)
33 self.assertEqual(UserDict.UserDict(dict=[('one',1), ('two',2)]), d2)
34 # both together
35 self.assertEqual(UserDict.UserDict([('one',1), ('two',2)], two=3, three=5), d3)
Guido van Rossum3eccc481999-03-26 15:32:05 +000036
Walter Dörwalde28be592003-01-19 23:26:59 +000037 # alternate constructor
38 self.assertEqual(UserDict.UserDict.fromkeys('one two'.split()), d4)
39 self.assertEqual(UserDict.UserDict().fromkeys('one two'.split()), d4)
40 self.assertEqual(UserDict.UserDict.fromkeys('one two'.split(), 1), d5)
41 self.assertEqual(UserDict.UserDict().fromkeys('one two'.split(), 1), d5)
42 self.assert_(u1.fromkeys('one two'.split()) is not u1)
43 self.assert_(isinstance(u1.fromkeys('one two'.split()), UserDict.UserDict))
44 self.assert_(isinstance(u2.fromkeys('one two'.split()), UserDict.IterableUserDict))
Raymond Hettingere4827eb2002-11-27 08:29:11 +000045
Walter Dörwalde28be592003-01-19 23:26:59 +000046 # Test __repr__
47 self.assertEqual(str(u0), str(d0))
48 self.assertEqual(repr(u1), repr(d1))
49 self.assertEqual(`u2`, `d2`)
Raymond Hettinger54405452002-11-22 00:07:40 +000050
Walter Dörwalde28be592003-01-19 23:26:59 +000051 # Test __cmp__ and __len__
52 all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2]
53 for a in all:
54 for b in all:
55 self.assertEqual(cmp(a, b), cmp(len(a), len(b)))
Guido van Rossum3eccc481999-03-26 15:32:05 +000056
Walter Dörwalde28be592003-01-19 23:26:59 +000057 # Test __getitem__
58 self.assertEqual(u2["one"], 1)
59 self.assertRaises(KeyError, u1.__getitem__, "two")
Guido van Rossum3eccc481999-03-26 15:32:05 +000060
Walter Dörwalde28be592003-01-19 23:26:59 +000061 # Test __setitem__
62 u3 = UserDict.UserDict(u2)
63 u3["two"] = 2
64 u3["three"] = 3
Guido van Rossum3eccc481999-03-26 15:32:05 +000065
Walter Dörwalde28be592003-01-19 23:26:59 +000066 # Test __delitem__
67 del u3["three"]
68 self.assertRaises(KeyError, u3.__delitem__, "three")
Guido van Rossum3eccc481999-03-26 15:32:05 +000069
Walter Dörwalde28be592003-01-19 23:26:59 +000070 # Test clear
71 u3.clear()
72 self.assertEqual(u3, {})
Guido van Rossum3eccc481999-03-26 15:32:05 +000073
Walter Dörwalde28be592003-01-19 23:26:59 +000074 # Test copy()
75 u2a = u2.copy()
76 self.assertEqual(u2a, u2)
77 u2b = UserDict.UserDict(x=42, y=23)
78 u2c = u2b.copy() # making a copy of a UserDict is special cased
79 self.assertEqual(u2b, u2c)
Guido van Rossum3eccc481999-03-26 15:32:05 +000080
Walter Dörwalde28be592003-01-19 23:26:59 +000081 class MyUserDict(UserDict.UserDict):
82 def display(self): print self
Guido van Rossum3eccc481999-03-26 15:32:05 +000083
Walter Dörwalde28be592003-01-19 23:26:59 +000084 m2 = MyUserDict(u2)
85 m2a = m2.copy()
86 self.assertEqual(m2a, m2)
Guido van Rossum3eccc481999-03-26 15:32:05 +000087
Walter Dörwalde28be592003-01-19 23:26:59 +000088 # SF bug #476616 -- copy() of UserDict subclass shared data
89 m2['foo'] = 'bar'
90 self.assertNotEqual(m2a, m2)
Guido van Rossum3eccc481999-03-26 15:32:05 +000091
Walter Dörwalde28be592003-01-19 23:26:59 +000092 # Test keys, items, values
93 self.assertEqual(u2.keys(), d2.keys())
94 self.assertEqual(u2.items(), d2.items())
95 self.assertEqual(u2.values(), d2.values())
Guido van Rossum3eccc481999-03-26 15:32:05 +000096
Walter Dörwalde28be592003-01-19 23:26:59 +000097 # Test has_key and "in".
98 for i in u2.keys():
99 self.assert_(u2.has_key(i))
100 self.assert_(i in u2)
101 self.assertEqual(u1.has_key(i), d1.has_key(i))
102 self.assertEqual(i in u1, i in d1)
103 self.assertEqual(u0.has_key(i), d0.has_key(i))
104 self.assertEqual(i in u0, i in d0)
Guido van Rossum3eccc481999-03-26 15:32:05 +0000105
Walter Dörwalde28be592003-01-19 23:26:59 +0000106 # Test update
107 t = UserDict.UserDict()
108 t.update(u2)
109 self.assertEqual(t, u2)
110 class Items:
111 def items(self):
112 return (("x", 42), ("y", 23))
113 t = UserDict.UserDict()
114 t.update(Items())
115 self.assertEqual(t, {"x": 42, "y": 23})
Guido van Rossum3eccc481999-03-26 15:32:05 +0000116
Walter Dörwalde28be592003-01-19 23:26:59 +0000117 # Test get
118 for i in u2.keys():
119 self.assertEqual(u2.get(i), u2[i])
120 self.assertEqual(u1.get(i), d1.get(i))
121 self.assertEqual(u0.get(i), d0.get(i))
Guido van Rossum3eccc481999-03-26 15:32:05 +0000122
Walter Dörwalde28be592003-01-19 23:26:59 +0000123 # Test "in" iteration.
124 for i in xrange(20):
125 u2[i] = str(i)
126 ikeys = []
127 for k in u2:
128 ikeys.append(k)
Walter Dörwalde28be592003-01-19 23:26:59 +0000129 keys = u2.keys()
Raymond Hettingera690a992003-11-16 16:17:49 +0000130 self.assertEqual(set(ikeys), set(keys))
Guido van Rossum3eccc481999-03-26 15:32:05 +0000131
Walter Dörwalde28be592003-01-19 23:26:59 +0000132 # Test setdefault
133 t = UserDict.UserDict()
134 self.assertEqual(t.setdefault("x", 42), 42)
135 self.assert_(t.has_key("x"))
136 self.assertEqual(t.setdefault("x", 23), 42)
Guido van Rossum3eccc481999-03-26 15:32:05 +0000137
Walter Dörwalde28be592003-01-19 23:26:59 +0000138 # Test pop
139 t = UserDict.UserDict(x=42)
140 self.assertEqual(t.pop("x"), 42)
141 self.assertRaises(KeyError, t.pop, "x")
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +0000142 self.assertEqual(t.pop("x", 1), 1)
143 t["x"] = 42
144 self.assertEqual(t.pop("x", 1), 42)
Guido van Rossum3eccc481999-03-26 15:32:05 +0000145
Walter Dörwalde28be592003-01-19 23:26:59 +0000146 # Test popitem
147 t = UserDict.UserDict(x=42)
148 self.assertEqual(t.popitem(), ("x", 42))
149 self.assertRaises(KeyError, t.popitem)
Raymond Hettinger903bf902002-11-15 08:39:40 +0000150
151##########################
152# Test Dict Mixin
153
Walter Dörwalde28be592003-01-19 23:26:59 +0000154class SeqDict(UserDict.DictMixin):
Raymond Hettinger903bf902002-11-15 08:39:40 +0000155 """Dictionary lookalike implemented with lists.
156
157 Used to test and demonstrate DictMixin
158 """
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000159 def __init__(self, other=None, **kwargs):
Raymond Hettinger903bf902002-11-15 08:39:40 +0000160 self.keylist = []
161 self.valuelist = []
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000162 if other is not None:
163 for (key, value) in other:
164 self[key] = value
165 for (key, value) in kwargs.iteritems():
166 self[key] = value
Raymond Hettinger903bf902002-11-15 08:39:40 +0000167 def __getitem__(self, key):
168 try:
169 i = self.keylist.index(key)
170 except ValueError:
171 raise KeyError
172 return self.valuelist[i]
173 def __setitem__(self, key, value):
Raymond Hettinger0efa17c2002-12-11 07:16:06 +0000174 try:
175 i = self.keylist.index(key)
176 self.valuelist[i] = value
177 except ValueError:
178 self.keylist.append(key)
179 self.valuelist.append(value)
Raymond Hettinger903bf902002-11-15 08:39:40 +0000180 def __delitem__(self, key):
181 try:
182 i = self.keylist.index(key)
183 except ValueError:
184 raise KeyError
185 self.keylist.pop(i)
186 self.valuelist.pop(i)
187 def keys(self):
188 return list(self.keylist)
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000189 def copy(self):
190 d = self.__class__()
191 for key, value in self.iteritems():
192 d[key] = value
193 return d
194 def fromkeys(cls, keys, value=None):
195 d = cls()
196 for key in keys:
197 d[key] = value
198 return d
199 fromkeys = classmethod(fromkeys)
Raymond Hettinger903bf902002-11-15 08:39:40 +0000200
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000201class UserDictMixinTest(mapping_tests.TestMappingProtocol):
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000202 _tested_class = SeqDict
203
Walter Dörwalde28be592003-01-19 23:26:59 +0000204 def test_all(self):
205 ## Setup test and verify working of the test class
Raymond Hettinger903bf902002-11-15 08:39:40 +0000206
Walter Dörwalde28be592003-01-19 23:26:59 +0000207 # check init
208 s = SeqDict()
Raymond Hettinger903bf902002-11-15 08:39:40 +0000209
Walter Dörwalde28be592003-01-19 23:26:59 +0000210 # exercise setitem
211 s[10] = 'ten'
212 s[20] = 'twenty'
213 s[30] = 'thirty'
Raymond Hettinger903bf902002-11-15 08:39:40 +0000214
Walter Dörwalde28be592003-01-19 23:26:59 +0000215 # exercise delitem
216 del s[20]
217 # check getitem and setitem
218 self.assertEqual(s[10], 'ten')
219 # check keys() and delitem
220 self.assertEqual(s.keys(), [10, 30])
Raymond Hettinger903bf902002-11-15 08:39:40 +0000221
Walter Dörwalde28be592003-01-19 23:26:59 +0000222 ## Now, test the DictMixin methods one by one
223 # has_key
224 self.assert_(s.has_key(10))
225 self.assert_(not s.has_key(20))
Raymond Hettinger903bf902002-11-15 08:39:40 +0000226
Walter Dörwalde28be592003-01-19 23:26:59 +0000227 # __contains__
228 self.assert_(10 in s)
229 self.assert_(20 not in s)
Raymond Hettinger903bf902002-11-15 08:39:40 +0000230
Walter Dörwalde28be592003-01-19 23:26:59 +0000231 # __iter__
232 self.assertEqual([k for k in s], [10, 30])
Raymond Hettinger903bf902002-11-15 08:39:40 +0000233
Walter Dörwalde28be592003-01-19 23:26:59 +0000234 # __len__
235 self.assertEqual(len(s), 2)
Raymond Hettinger903bf902002-11-15 08:39:40 +0000236
Walter Dörwalde28be592003-01-19 23:26:59 +0000237 # iteritems
238 self.assertEqual(list(s.iteritems()), [(10,'ten'), (30, 'thirty')])
Raymond Hettinger903bf902002-11-15 08:39:40 +0000239
Walter Dörwalde28be592003-01-19 23:26:59 +0000240 # iterkeys
241 self.assertEqual(list(s.iterkeys()), [10, 30])
Raymond Hettinger903bf902002-11-15 08:39:40 +0000242
Walter Dörwalde28be592003-01-19 23:26:59 +0000243 # itervalues
244 self.assertEqual(list(s.itervalues()), ['ten', 'thirty'])
Raymond Hettinger903bf902002-11-15 08:39:40 +0000245
Walter Dörwalde28be592003-01-19 23:26:59 +0000246 # values
247 self.assertEqual(s.values(), ['ten', 'thirty'])
Raymond Hettinger903bf902002-11-15 08:39:40 +0000248
Walter Dörwalde28be592003-01-19 23:26:59 +0000249 # items
250 self.assertEqual(s.items(), [(10,'ten'), (30, 'thirty')])
Raymond Hettinger903bf902002-11-15 08:39:40 +0000251
Walter Dörwalde28be592003-01-19 23:26:59 +0000252 # get
253 self.assertEqual(s.get(10), 'ten')
254 self.assertEqual(s.get(15,'fifteen'), 'fifteen')
255 self.assertEqual(s.get(15), None)
Raymond Hettinger903bf902002-11-15 08:39:40 +0000256
Walter Dörwalde28be592003-01-19 23:26:59 +0000257 # setdefault
258 self.assertEqual(s.setdefault(40, 'forty'), 'forty')
259 self.assertEqual(s.setdefault(10, 'null'), 'ten')
260 del s[40]
Raymond Hettinger903bf902002-11-15 08:39:40 +0000261
Walter Dörwalde28be592003-01-19 23:26:59 +0000262 # pop
263 self.assertEqual(s.pop(10), 'ten')
264 self.assert_(10 not in s)
265 s[10] = 'ten'
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +0000266 self.assertEqual(s.pop("x", 1), 1)
267 s["x"] = 42
268 self.assertEqual(s.pop("x", 1), 42)
Raymond Hettinger903bf902002-11-15 08:39:40 +0000269
Walter Dörwalde28be592003-01-19 23:26:59 +0000270 # popitem
271 k, v = s.popitem()
272 self.assert_(k not in s)
273 s[k] = v
Raymond Hettinger903bf902002-11-15 08:39:40 +0000274
Walter Dörwalde28be592003-01-19 23:26:59 +0000275 # clear
276 s.clear()
277 self.assertEqual(len(s), 0)
278
279 # empty popitem
280 self.assertRaises(KeyError, s.popitem)
281
282 # update
283 s.update({10: 'ten', 20:'twenty'})
284 self.assertEqual(s[10], 'ten')
285 self.assertEqual(s[20], 'twenty')
286
287 # cmp
288 self.assertEqual(s, {10: 'ten', 20:'twenty'})
289 t = SeqDict()
290 t[20] = 'twenty'
291 t[10] = 'ten'
292 self.assertEqual(s, t)
293
294def test_main():
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000295 test_support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000296 UserDictTest,
297 UserDictMixinTest
298 )
Walter Dörwalde28be592003-01-19 23:26:59 +0000299
300if __name__ == "__main__":
301 test_main()