blob: d3e8002f668045eb13028e49785f665009f4010e [file] [log] [blame]
Guido van Rossum3eccc481999-03-26 15:32:05 +00001# Check every path through every method of UserDict
2
Walter Dörwalde28be592003-01-19 23:26:59 +00003import test.test_support, unittest
4
5import 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örwalde28be592003-01-19 23:26:59 +000014class UserDictTest(unittest.TestCase):
15 def test_all(self):
16 # Test constructors
17 u = UserDict.UserDict()
18 u0 = UserDict.UserDict(d0)
19 u1 = UserDict.UserDict(d1)
20 u2 = UserDict.IterableUserDict(d2)
Guido van Rossum3eccc481999-03-26 15:32:05 +000021
Walter Dörwalde28be592003-01-19 23:26:59 +000022 uu = UserDict.UserDict(u)
23 uu0 = UserDict.UserDict(u0)
24 uu1 = UserDict.UserDict(u1)
25 uu2 = UserDict.UserDict(u2)
Guido van Rossum3eccc481999-03-26 15:32:05 +000026
Walter Dörwalde28be592003-01-19 23:26:59 +000027 # keyword arg constructor
28 self.assertEqual(UserDict.UserDict(one=1, two=2), d2)
29 # item sequence constructor
30 self.assertEqual(UserDict.UserDict([('one',1), ('two',2)]), d2)
31 self.assertEqual(UserDict.UserDict(dict=[('one',1), ('two',2)]), d2)
32 # both together
33 self.assertEqual(UserDict.UserDict([('one',1), ('two',2)], two=3, three=5), d3)
Guido van Rossum3eccc481999-03-26 15:32:05 +000034
Walter Dörwalde28be592003-01-19 23:26:59 +000035 # alternate constructor
36 self.assertEqual(UserDict.UserDict.fromkeys('one two'.split()), d4)
37 self.assertEqual(UserDict.UserDict().fromkeys('one two'.split()), d4)
38 self.assertEqual(UserDict.UserDict.fromkeys('one two'.split(), 1), d5)
39 self.assertEqual(UserDict.UserDict().fromkeys('one two'.split(), 1), d5)
40 self.assert_(u1.fromkeys('one two'.split()) is not u1)
41 self.assert_(isinstance(u1.fromkeys('one two'.split()), UserDict.UserDict))
42 self.assert_(isinstance(u2.fromkeys('one two'.split()), UserDict.IterableUserDict))
Raymond Hettingere4827eb2002-11-27 08:29:11 +000043
Walter Dörwalde28be592003-01-19 23:26:59 +000044 # Test __repr__
45 self.assertEqual(str(u0), str(d0))
46 self.assertEqual(repr(u1), repr(d1))
47 self.assertEqual(`u2`, `d2`)
Raymond Hettinger54405452002-11-22 00:07:40 +000048
Walter Dörwalde28be592003-01-19 23:26:59 +000049 # Test __cmp__ and __len__
50 all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2]
51 for a in all:
52 for b in all:
53 self.assertEqual(cmp(a, b), cmp(len(a), len(b)))
Guido van Rossum3eccc481999-03-26 15:32:05 +000054
Walter Dörwalde28be592003-01-19 23:26:59 +000055 # Test __getitem__
56 self.assertEqual(u2["one"], 1)
57 self.assertRaises(KeyError, u1.__getitem__, "two")
Guido van Rossum3eccc481999-03-26 15:32:05 +000058
Walter Dörwalde28be592003-01-19 23:26:59 +000059 # Test __setitem__
60 u3 = UserDict.UserDict(u2)
61 u3["two"] = 2
62 u3["three"] = 3
Guido van Rossum3eccc481999-03-26 15:32:05 +000063
Walter Dörwalde28be592003-01-19 23:26:59 +000064 # Test __delitem__
65 del u3["three"]
66 self.assertRaises(KeyError, u3.__delitem__, "three")
Guido van Rossum3eccc481999-03-26 15:32:05 +000067
Walter Dörwalde28be592003-01-19 23:26:59 +000068 # Test clear
69 u3.clear()
70 self.assertEqual(u3, {})
Guido van Rossum3eccc481999-03-26 15:32:05 +000071
Walter Dörwalde28be592003-01-19 23:26:59 +000072 # Test copy()
73 u2a = u2.copy()
74 self.assertEqual(u2a, u2)
75 u2b = UserDict.UserDict(x=42, y=23)
76 u2c = u2b.copy() # making a copy of a UserDict is special cased
77 self.assertEqual(u2b, u2c)
Guido van Rossum3eccc481999-03-26 15:32:05 +000078
Walter Dörwalde28be592003-01-19 23:26:59 +000079 class MyUserDict(UserDict.UserDict):
80 def display(self): print self
Guido van Rossum3eccc481999-03-26 15:32:05 +000081
Walter Dörwalde28be592003-01-19 23:26:59 +000082 m2 = MyUserDict(u2)
83 m2a = m2.copy()
84 self.assertEqual(m2a, m2)
Guido van Rossum3eccc481999-03-26 15:32:05 +000085
Walter Dörwalde28be592003-01-19 23:26:59 +000086 # SF bug #476616 -- copy() of UserDict subclass shared data
87 m2['foo'] = 'bar'
88 self.assertNotEqual(m2a, m2)
Guido van Rossum3eccc481999-03-26 15:32:05 +000089
Walter Dörwalde28be592003-01-19 23:26:59 +000090 # Test keys, items, values
91 self.assertEqual(u2.keys(), d2.keys())
92 self.assertEqual(u2.items(), d2.items())
93 self.assertEqual(u2.values(), d2.values())
Guido van Rossum3eccc481999-03-26 15:32:05 +000094
Walter Dörwalde28be592003-01-19 23:26:59 +000095 # Test has_key and "in".
96 for i in u2.keys():
97 self.assert_(u2.has_key(i))
98 self.assert_(i in u2)
99 self.assertEqual(u1.has_key(i), d1.has_key(i))
100 self.assertEqual(i in u1, i in d1)
101 self.assertEqual(u0.has_key(i), d0.has_key(i))
102 self.assertEqual(i in u0, i in d0)
Guido van Rossum3eccc481999-03-26 15:32:05 +0000103
Walter Dörwalde28be592003-01-19 23:26:59 +0000104 # Test update
105 t = UserDict.UserDict()
106 t.update(u2)
107 self.assertEqual(t, u2)
108 class Items:
109 def items(self):
110 return (("x", 42), ("y", 23))
111 t = UserDict.UserDict()
112 t.update(Items())
113 self.assertEqual(t, {"x": 42, "y": 23})
Guido van Rossum3eccc481999-03-26 15:32:05 +0000114
Walter Dörwalde28be592003-01-19 23:26:59 +0000115 # Test get
116 for i in u2.keys():
117 self.assertEqual(u2.get(i), u2[i])
118 self.assertEqual(u1.get(i), d1.get(i))
119 self.assertEqual(u0.get(i), d0.get(i))
Guido van Rossum3eccc481999-03-26 15:32:05 +0000120
Walter Dörwalde28be592003-01-19 23:26:59 +0000121 # Test "in" iteration.
122 for i in xrange(20):
123 u2[i] = str(i)
124 ikeys = []
125 for k in u2:
126 ikeys.append(k)
127 ikeys.sort()
128 keys = u2.keys()
129 keys.sort()
130 self.assertEqual(ikeys, 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")
Guido van Rossum3eccc481999-03-26 15:32:05 +0000142
Walter Dörwalde28be592003-01-19 23:26:59 +0000143 # Test popitem
144 t = UserDict.UserDict(x=42)
145 self.assertEqual(t.popitem(), ("x", 42))
146 self.assertRaises(KeyError, t.popitem)
Raymond Hettinger903bf902002-11-15 08:39:40 +0000147
148##########################
149# Test Dict Mixin
150
Walter Dörwalde28be592003-01-19 23:26:59 +0000151class SeqDict(UserDict.DictMixin):
Raymond Hettinger903bf902002-11-15 08:39:40 +0000152 """Dictionary lookalike implemented with lists.
153
154 Used to test and demonstrate DictMixin
155 """
156 def __init__(self):
157 self.keylist = []
158 self.valuelist = []
159 def __getitem__(self, key):
160 try:
161 i = self.keylist.index(key)
162 except ValueError:
163 raise KeyError
164 return self.valuelist[i]
165 def __setitem__(self, key, value):
Raymond Hettinger0efa17c2002-12-11 07:16:06 +0000166 try:
167 i = self.keylist.index(key)
168 self.valuelist[i] = value
169 except ValueError:
170 self.keylist.append(key)
171 self.valuelist.append(value)
Raymond Hettinger903bf902002-11-15 08:39:40 +0000172 def __delitem__(self, key):
173 try:
174 i = self.keylist.index(key)
175 except ValueError:
176 raise KeyError
177 self.keylist.pop(i)
178 self.valuelist.pop(i)
179 def keys(self):
180 return list(self.keylist)
181
Walter Dörwalde28be592003-01-19 23:26:59 +0000182class UserDictMixinTest(unittest.TestCase):
183 def test_all(self):
184 ## Setup test and verify working of the test class
Raymond Hettinger903bf902002-11-15 08:39:40 +0000185
Walter Dörwalde28be592003-01-19 23:26:59 +0000186 # check init
187 s = SeqDict()
Raymond Hettinger903bf902002-11-15 08:39:40 +0000188
Walter Dörwalde28be592003-01-19 23:26:59 +0000189 # exercise setitem
190 s[10] = 'ten'
191 s[20] = 'twenty'
192 s[30] = 'thirty'
Raymond Hettinger903bf902002-11-15 08:39:40 +0000193
Walter Dörwalde28be592003-01-19 23:26:59 +0000194 # exercise delitem
195 del s[20]
196 # check getitem and setitem
197 self.assertEqual(s[10], 'ten')
198 # check keys() and delitem
199 self.assertEqual(s.keys(), [10, 30])
Raymond Hettinger903bf902002-11-15 08:39:40 +0000200
Walter Dörwalde28be592003-01-19 23:26:59 +0000201 ## Now, test the DictMixin methods one by one
202 # has_key
203 self.assert_(s.has_key(10))
204 self.assert_(not s.has_key(20))
Raymond Hettinger903bf902002-11-15 08:39:40 +0000205
Walter Dörwalde28be592003-01-19 23:26:59 +0000206 # __contains__
207 self.assert_(10 in s)
208 self.assert_(20 not in s)
Raymond Hettinger903bf902002-11-15 08:39:40 +0000209
Walter Dörwalde28be592003-01-19 23:26:59 +0000210 # __iter__
211 self.assertEqual([k for k in s], [10, 30])
Raymond Hettinger903bf902002-11-15 08:39:40 +0000212
Walter Dörwalde28be592003-01-19 23:26:59 +0000213 # __len__
214 self.assertEqual(len(s), 2)
Raymond Hettinger903bf902002-11-15 08:39:40 +0000215
Walter Dörwalde28be592003-01-19 23:26:59 +0000216 # iteritems
217 self.assertEqual(list(s.iteritems()), [(10,'ten'), (30, 'thirty')])
Raymond Hettinger903bf902002-11-15 08:39:40 +0000218
Walter Dörwalde28be592003-01-19 23:26:59 +0000219 # iterkeys
220 self.assertEqual(list(s.iterkeys()), [10, 30])
Raymond Hettinger903bf902002-11-15 08:39:40 +0000221
Walter Dörwalde28be592003-01-19 23:26:59 +0000222 # itervalues
223 self.assertEqual(list(s.itervalues()), ['ten', 'thirty'])
Raymond Hettinger903bf902002-11-15 08:39:40 +0000224
Walter Dörwalde28be592003-01-19 23:26:59 +0000225 # values
226 self.assertEqual(s.values(), ['ten', 'thirty'])
Raymond Hettinger903bf902002-11-15 08:39:40 +0000227
Walter Dörwalde28be592003-01-19 23:26:59 +0000228 # items
229 self.assertEqual(s.items(), [(10,'ten'), (30, 'thirty')])
Raymond Hettinger903bf902002-11-15 08:39:40 +0000230
Walter Dörwalde28be592003-01-19 23:26:59 +0000231 # get
232 self.assertEqual(s.get(10), 'ten')
233 self.assertEqual(s.get(15,'fifteen'), 'fifteen')
234 self.assertEqual(s.get(15), None)
Raymond Hettinger903bf902002-11-15 08:39:40 +0000235
Walter Dörwalde28be592003-01-19 23:26:59 +0000236 # setdefault
237 self.assertEqual(s.setdefault(40, 'forty'), 'forty')
238 self.assertEqual(s.setdefault(10, 'null'), 'ten')
239 del s[40]
Raymond Hettinger903bf902002-11-15 08:39:40 +0000240
Walter Dörwalde28be592003-01-19 23:26:59 +0000241 # pop
242 self.assertEqual(s.pop(10), 'ten')
243 self.assert_(10 not in s)
244 s[10] = 'ten'
Raymond Hettinger903bf902002-11-15 08:39:40 +0000245
Walter Dörwalde28be592003-01-19 23:26:59 +0000246 # popitem
247 k, v = s.popitem()
248 self.assert_(k not in s)
249 s[k] = v
Raymond Hettinger903bf902002-11-15 08:39:40 +0000250
Walter Dörwalde28be592003-01-19 23:26:59 +0000251 # clear
252 s.clear()
253 self.assertEqual(len(s), 0)
254
255 # empty popitem
256 self.assertRaises(KeyError, s.popitem)
257
258 # update
259 s.update({10: 'ten', 20:'twenty'})
260 self.assertEqual(s[10], 'ten')
261 self.assertEqual(s[20], 'twenty')
262
263 # cmp
264 self.assertEqual(s, {10: 'ten', 20:'twenty'})
265 t = SeqDict()
266 t[20] = 'twenty'
267 t[10] = 'ten'
268 self.assertEqual(s, t)
269
270def test_main():
271 suite = unittest.TestSuite()
272 suite.addTest(unittest.makeSuite(UserDictTest))
273 suite.addTest(unittest.makeSuite(UserDictMixinTest))
274 test.test_support.run_suite(suite)
275
276if __name__ == "__main__":
277 test_main()