Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 1 | # Check every path through every method of UserDict |
| 2 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support, mapping_tests |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 4 | import collections |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 5 | |
| 6 | d0 = {} |
| 7 | d1 = {"one": 1} |
| 8 | d2 = {"one": 1, "two": 2} |
Raymond Hettinger | e4827eb | 2002-11-27 08:29:11 +0000 | [diff] [blame] | 9 | d3 = {"one": 1, "two": 3, "three": 5} |
| 10 | d4 = {"one": None, "two": None} |
| 11 | d5 = {"one": 1, "two": 1} |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 12 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 13 | class UserDictTest(mapping_tests.TestHashMappingProtocol): |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 14 | type2test = collections.UserDict |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 15 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 16 | def test_all(self): |
| 17 | # Test constructors |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 18 | u = collections.UserDict() |
| 19 | u0 = collections.UserDict(d0) |
| 20 | u1 = collections.UserDict(d1) |
| 21 | u2 = collections.UserDict(d2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 22 | |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 23 | uu = collections.UserDict(u) |
| 24 | uu0 = collections.UserDict(u0) |
| 25 | uu1 = collections.UserDict(u1) |
| 26 | uu2 = collections.UserDict(u2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 27 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 28 | # keyword arg constructor |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 29 | self.assertEqual(collections.UserDict(one=1, two=2), d2) |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 30 | # item sequence constructor |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 31 | self.assertEqual(collections.UserDict([('one',1), ('two',2)]), d2) |
| 32 | self.assertEqual(collections.UserDict(dict=[('one',1), ('two',2)]), d2) |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 33 | # both together |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 34 | self.assertEqual(collections.UserDict([('one',1), ('two',2)], two=3, three=5), d3) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 35 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 36 | # alternate constructor |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 37 | 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 Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 41 | 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 Hettinger | e4827eb | 2002-11-27 08:29:11 +0000 | [diff] [blame] | 44 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 45 | # Test __repr__ |
| 46 | self.assertEqual(str(u0), str(d0)) |
| 47 | self.assertEqual(repr(u1), repr(d1)) |
Brett Cannon | 0b70cca | 2006-08-25 02:59:59 +0000 | [diff] [blame] | 48 | self.assertEqual(repr(u2), repr(d2)) |
Raymond Hettinger | 5440545 | 2002-11-22 00:07:40 +0000 | [diff] [blame] | 49 | |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 50 | # Test rich comparison and __len__ |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 51 | all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2] |
| 52 | for a in all: |
| 53 | for b in all: |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 54 | self.assertEqual(a == b, len(a) == len(b)) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 55 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 56 | # Test __getitem__ |
| 57 | self.assertEqual(u2["one"], 1) |
| 58 | self.assertRaises(KeyError, u1.__getitem__, "two") |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 59 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 60 | # Test __setitem__ |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 61 | u3 = collections.UserDict(u2) |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 62 | u3["two"] = 2 |
| 63 | u3["three"] = 3 |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 64 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 65 | # Test __delitem__ |
| 66 | del u3["three"] |
| 67 | self.assertRaises(KeyError, u3.__delitem__, "three") |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 68 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 69 | # Test clear |
| 70 | u3.clear() |
| 71 | self.assertEqual(u3, {}) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 72 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 73 | # Test copy() |
| 74 | u2a = u2.copy() |
| 75 | self.assertEqual(u2a, u2) |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 76 | u2b = collections.UserDict(x=42, y=23) |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 77 | u2c = u2b.copy() # making a copy of a UserDict is special cased |
| 78 | self.assertEqual(u2b, u2c) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 79 | |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 80 | class MyUserDict(collections.UserDict): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 81 | def display(self): print(self) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 82 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 83 | m2 = MyUserDict(u2) |
| 84 | m2a = m2.copy() |
| 85 | self.assertEqual(m2a, m2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 86 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 87 | # SF bug #476616 -- copy() of UserDict subclass shared data |
| 88 | m2['foo'] = 'bar' |
| 89 | self.assertNotEqual(m2a, m2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 90 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 91 | # Test keys, items, values |
| 92 | self.assertEqual(u2.keys(), d2.keys()) |
| 93 | self.assertEqual(u2.items(), d2.items()) |
Guido van Rossum | d81206d | 2007-02-15 03:49:08 +0000 | [diff] [blame] | 94 | self.assertEqual(list(u2.values()), list(d2.values())) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 96 | # Test "in". |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 97 | for i in u2.keys(): |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 98 | self.assertIn(i, u2) |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 99 | self.assertEqual(i in u1, i in d1) |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 100 | self.assertEqual(i in u0, i in d0) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 101 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 102 | # Test update |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 103 | t = collections.UserDict() |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 104 | t.update(u2) |
| 105 | self.assertEqual(t, u2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 106 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 107 | # 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 Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 112 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 113 | # Test "in" iteration. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 114 | for i in range(20): |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 115 | u2[i] = str(i) |
| 116 | ikeys = [] |
| 117 | for k in u2: |
| 118 | ikeys.append(k) |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 119 | keys = u2.keys() |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 120 | self.assertEqual(set(ikeys), set(keys)) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 121 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 122 | # Test setdefault |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 123 | t = collections.UserDict() |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 124 | self.assertEqual(t.setdefault("x", 42), 42) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 125 | self.assertIn("x", t) |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 126 | self.assertEqual(t.setdefault("x", 23), 42) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 127 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 128 | # Test pop |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 129 | t = collections.UserDict(x=42) |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 130 | self.assertEqual(t.pop("x"), 42) |
| 131 | self.assertRaises(KeyError, t.pop, "x") |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 132 | self.assertEqual(t.pop("x", 1), 1) |
| 133 | t["x"] = 42 |
| 134 | self.assertEqual(t.pop("x", 1), 42) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 135 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 136 | # Test popitem |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 137 | t = collections.UserDict(x=42) |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 138 | self.assertEqual(t.popitem(), ("x", 42)) |
| 139 | self.assertRaises(KeyError, t.popitem) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 141 | def test_missing(self): |
| 142 | # Make sure UserDict doesn't have a __missing__ method |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 143 | self.assertEqual(hasattr(collections.UserDict, "__missing__"), False) |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 144 | # 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 Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 149 | class D(collections.UserDict): |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 150 | 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 Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 155 | self.assertNotIn(2, d) |
| 156 | self.assertNotIn(2, d.keys()) |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 157 | self.assertEqual(d[2], 42) |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 158 | class E(collections.UserDict): |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 159 | def __missing__(self, key): |
| 160 | raise RuntimeError(key) |
| 161 | e = E() |
| 162 | try: |
| 163 | e[42] |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 164 | except RuntimeError as err: |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 165 | self.assertEqual(err.args, (42,)) |
| 166 | else: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 167 | self.fail("e[42] didn't raise RuntimeError") |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 168 | class F(collections.UserDict): |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 169 | def __init__(self): |
| 170 | # An instance variable __missing__ should have no effect |
| 171 | self.__missing__ = lambda key: None |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 172 | collections.UserDict.__init__(self) |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 173 | f = F() |
| 174 | try: |
| 175 | f[42] |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 176 | except KeyError as err: |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 177 | self.assertEqual(err.args, (42,)) |
| 178 | else: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 179 | self.fail("f[42] didn't raise KeyError") |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 180 | class G(collections.UserDict): |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 181 | pass |
| 182 | g = G() |
| 183 | try: |
| 184 | g[42] |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 185 | except KeyError as err: |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 186 | self.assertEqual(err.args, (42,)) |
| 187 | else: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 188 | self.fail("g[42] didn't raise KeyError") |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 189 | |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 190 | |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 191 | |
| 192 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 193 | support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 194 | UserDictTest, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 195 | ) |
Walter Dörwald | e28be59 | 2003-01-19 23:26:59 +0000 | [diff] [blame] | 196 | |
| 197 | if __name__ == "__main__": |
| 198 | test_main() |