Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 1 | # Check every path through every method of UserDict |
| 2 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame^] | 3 | import unittest |
| 4 | from test import test_support, mapping_tests |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 5 | import UserDict |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 6 | |
| 7 | d0 = {} |
| 8 | d1 = {"one": 1} |
| 9 | d2 = {"one": 1, "two": 2} |
Raymond Hettinger | e4827eb | 2002-11-27 08:29:11 +0000 | [diff] [blame] | 10 | d3 = {"one": 1, "two": 3, "three": 5} |
| 11 | d4 = {"one": None, "two": None} |
| 12 | d5 = {"one": 1, "two": 1} |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 13 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame^] | 14 | class UserDictTest(mapping_tests.TestHashMappingProtocol): |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 15 | _tested_class = UserDict.IterableUserDict |
| 16 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 17 | 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 23 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 24 | uu = UserDict.UserDict(u) |
| 25 | uu0 = UserDict.UserDict(u0) |
| 26 | uu1 = UserDict.UserDict(u1) |
| 27 | uu2 = UserDict.UserDict(u2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 28 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 29 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 36 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 37 | # 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 Hettinger | e4827eb | 2002-11-27 08:29:11 +0000 | [diff] [blame] | 45 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 46 | # Test __repr__ |
| 47 | self.assertEqual(str(u0), str(d0)) |
| 48 | self.assertEqual(repr(u1), repr(d1)) |
| 49 | self.assertEqual(`u2`, `d2`) |
Raymond Hettinger | 5440545 | 2002-11-22 00:07:40 +0000 | [diff] [blame] | 50 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 51 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 56 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 57 | # Test __getitem__ |
| 58 | self.assertEqual(u2["one"], 1) |
| 59 | self.assertRaises(KeyError, u1.__getitem__, "two") |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 60 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 61 | # Test __setitem__ |
| 62 | u3 = UserDict.UserDict(u2) |
| 63 | u3["two"] = 2 |
| 64 | u3["three"] = 3 |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 65 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 66 | # Test __delitem__ |
| 67 | del u3["three"] |
| 68 | self.assertRaises(KeyError, u3.__delitem__, "three") |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 69 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 70 | # Test clear |
| 71 | u3.clear() |
| 72 | self.assertEqual(u3, {}) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 73 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 74 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 80 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 81 | class MyUserDict(UserDict.UserDict): |
| 82 | def display(self): print self |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 83 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 84 | m2 = MyUserDict(u2) |
| 85 | m2a = m2.copy() |
| 86 | self.assertEqual(m2a, m2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 87 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 88 | # SF bug #476616 -- copy() of UserDict subclass shared data |
| 89 | m2['foo'] = 'bar' |
| 90 | self.assertNotEqual(m2a, m2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 91 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 92 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 96 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 97 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 105 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 106 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 116 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 117 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 122 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 123 | # 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örwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 129 | keys = u2.keys() |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 130 | self.assertEqual(set(ikeys), set(keys)) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 131 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 132 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 137 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 138 | # Test pop |
| 139 | t = UserDict.UserDict(x=42) |
| 140 | self.assertEqual(t.pop("x"), 42) |
| 141 | self.assertRaises(KeyError, t.pop, "x") |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 142 | self.assertEqual(t.pop("x", 1), 1) |
| 143 | t["x"] = 42 |
| 144 | self.assertEqual(t.pop("x", 1), 42) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 145 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 146 | # Test popitem |
| 147 | t = UserDict.UserDict(x=42) |
| 148 | self.assertEqual(t.popitem(), ("x", 42)) |
| 149 | self.assertRaises(KeyError, t.popitem) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 150 | |
| 151 | ########################## |
| 152 | # Test Dict Mixin |
| 153 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 154 | class SeqDict(UserDict.DictMixin): |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 155 | """Dictionary lookalike implemented with lists. |
| 156 | |
| 157 | Used to test and demonstrate DictMixin |
| 158 | """ |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame^] | 159 | def __init__(self, other=None, **kwargs): |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 160 | self.keylist = [] |
| 161 | self.valuelist = [] |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame^] | 162 | 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 Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 167 | 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 Hettinger | 0efa17c | 2002-12-11 07:16:06 +0000 | [diff] [blame] | 174 | 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 Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 180 | 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örwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame^] | 189 | 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 Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 200 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame^] | 201 | class UserDictMixinTest(mapping_tests.TestMappingProtocol): |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 202 | _tested_class = SeqDict |
| 203 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 204 | def test_all(self): |
| 205 | ## Setup test and verify working of the test class |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 206 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 207 | # check init |
| 208 | s = SeqDict() |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 209 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 210 | # exercise setitem |
| 211 | s[10] = 'ten' |
| 212 | s[20] = 'twenty' |
| 213 | s[30] = 'thirty' |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 214 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 215 | # 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 Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 221 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 222 | ## 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 Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 226 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 227 | # __contains__ |
| 228 | self.assert_(10 in s) |
| 229 | self.assert_(20 not in s) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 230 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 231 | # __iter__ |
| 232 | self.assertEqual([k for k in s], [10, 30]) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 233 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 234 | # __len__ |
| 235 | self.assertEqual(len(s), 2) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 236 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 237 | # iteritems |
| 238 | self.assertEqual(list(s.iteritems()), [(10,'ten'), (30, 'thirty')]) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 239 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 240 | # iterkeys |
| 241 | self.assertEqual(list(s.iterkeys()), [10, 30]) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 242 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 243 | # itervalues |
| 244 | self.assertEqual(list(s.itervalues()), ['ten', 'thirty']) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 245 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 246 | # values |
| 247 | self.assertEqual(s.values(), ['ten', 'thirty']) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 248 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 249 | # items |
| 250 | self.assertEqual(s.items(), [(10,'ten'), (30, 'thirty')]) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 251 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 252 | # get |
| 253 | self.assertEqual(s.get(10), 'ten') |
| 254 | self.assertEqual(s.get(15,'fifteen'), 'fifteen') |
| 255 | self.assertEqual(s.get(15), None) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 256 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 257 | # setdefault |
| 258 | self.assertEqual(s.setdefault(40, 'forty'), 'forty') |
| 259 | self.assertEqual(s.setdefault(10, 'null'), 'ten') |
| 260 | del s[40] |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 261 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 262 | # pop |
| 263 | self.assertEqual(s.pop(10), 'ten') |
| 264 | self.assert_(10 not in s) |
| 265 | s[10] = 'ten' |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 266 | self.assertEqual(s.pop("x", 1), 1) |
| 267 | s["x"] = 42 |
| 268 | self.assertEqual(s.pop("x", 1), 42) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 269 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 270 | # popitem |
| 271 | k, v = s.popitem() |
| 272 | self.assert_(k not in s) |
| 273 | s[k] = v |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 274 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 275 | # 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 | |
| 294 | def test_main(): |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame^] | 295 | test_support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 296 | UserDictTest, |
| 297 | UserDictMixinTest |
| 298 | ) |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 299 | |
| 300 | if __name__ == "__main__": |
| 301 | test_main() |