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 | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 3 | import test.test_support, unittest |
| 4 | |
| 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 | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 14 | class 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 21 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 22 | uu = UserDict.UserDict(u) |
| 23 | uu0 = UserDict.UserDict(u0) |
| 24 | uu1 = UserDict.UserDict(u1) |
| 25 | uu2 = UserDict.UserDict(u2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 26 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 27 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 34 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 35 | # 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 Hettinger | e4827eb | 2002-11-27 08:29:11 +0000 | [diff] [blame] | 43 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 44 | # Test __repr__ |
| 45 | self.assertEqual(str(u0), str(d0)) |
| 46 | self.assertEqual(repr(u1), repr(d1)) |
| 47 | self.assertEqual(`u2`, `d2`) |
Raymond Hettinger | 5440545 | 2002-11-22 00:07:40 +0000 | [diff] [blame] | 48 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 49 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 54 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 55 | # Test __getitem__ |
| 56 | self.assertEqual(u2["one"], 1) |
| 57 | self.assertRaises(KeyError, u1.__getitem__, "two") |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 58 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 59 | # Test __setitem__ |
| 60 | u3 = UserDict.UserDict(u2) |
| 61 | u3["two"] = 2 |
| 62 | u3["three"] = 3 |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 63 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 64 | # Test __delitem__ |
| 65 | del u3["three"] |
| 66 | self.assertRaises(KeyError, u3.__delitem__, "three") |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 67 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 68 | # Test clear |
| 69 | u3.clear() |
| 70 | self.assertEqual(u3, {}) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 71 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 72 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 78 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 79 | class MyUserDict(UserDict.UserDict): |
| 80 | def display(self): print self |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 81 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 82 | m2 = MyUserDict(u2) |
| 83 | m2a = m2.copy() |
| 84 | self.assertEqual(m2a, m2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 85 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 86 | # SF bug #476616 -- copy() of UserDict subclass shared data |
| 87 | m2['foo'] = 'bar' |
| 88 | self.assertNotEqual(m2a, m2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 89 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 90 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 94 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 95 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 103 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 104 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 114 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 115 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 120 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 121 | # 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 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") |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 142 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 143 | # Test popitem |
| 144 | t = UserDict.UserDict(x=42) |
| 145 | self.assertEqual(t.popitem(), ("x", 42)) |
| 146 | self.assertRaises(KeyError, t.popitem) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 147 | |
| 148 | ########################## |
| 149 | # Test Dict Mixin |
| 150 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 151 | class SeqDict(UserDict.DictMixin): |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 152 | """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 Hettinger | 0efa17c | 2002-12-11 07:16:06 +0000 | [diff] [blame] | 166 | 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 Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 172 | 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örwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 182 | class UserDictMixinTest(unittest.TestCase): |
| 183 | def test_all(self): |
| 184 | ## Setup test and verify working of the test class |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 185 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 186 | # check init |
| 187 | s = SeqDict() |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 188 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 189 | # exercise setitem |
| 190 | s[10] = 'ten' |
| 191 | s[20] = 'twenty' |
| 192 | s[30] = 'thirty' |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 193 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 194 | # 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 Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 200 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 201 | ## 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 Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 205 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 206 | # __contains__ |
| 207 | self.assert_(10 in s) |
| 208 | self.assert_(20 not in s) |
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 | # __iter__ |
| 211 | self.assertEqual([k for k in s], [10, 30]) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 212 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 213 | # __len__ |
| 214 | self.assertEqual(len(s), 2) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 215 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 216 | # iteritems |
| 217 | self.assertEqual(list(s.iteritems()), [(10,'ten'), (30, 'thirty')]) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 218 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 219 | # iterkeys |
| 220 | self.assertEqual(list(s.iterkeys()), [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 | # itervalues |
| 223 | self.assertEqual(list(s.itervalues()), ['ten', 'thirty']) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 224 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 225 | # values |
| 226 | self.assertEqual(s.values(), ['ten', 'thirty']) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 227 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 228 | # items |
| 229 | self.assertEqual(s.items(), [(10,'ten'), (30, 'thirty')]) |
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 | # get |
| 232 | self.assertEqual(s.get(10), 'ten') |
| 233 | self.assertEqual(s.get(15,'fifteen'), 'fifteen') |
| 234 | self.assertEqual(s.get(15), None) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 235 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 236 | # setdefault |
| 237 | self.assertEqual(s.setdefault(40, 'forty'), 'forty') |
| 238 | self.assertEqual(s.setdefault(10, 'null'), 'ten') |
| 239 | del s[40] |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 240 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 241 | # pop |
| 242 | self.assertEqual(s.pop(10), 'ten') |
| 243 | self.assert_(10 not in s) |
| 244 | s[10] = 'ten' |
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 | # popitem |
| 247 | k, v = s.popitem() |
| 248 | self.assert_(k not in s) |
| 249 | s[k] = v |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 250 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 251 | # 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 | |
| 270 | def 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 | |
| 276 | if __name__ == "__main__": |
| 277 | test_main() |