1 | n/a | # Check every path through every method of UserDict |
---|
2 | n/a | |
---|
3 | n/a | from test import mapping_tests |
---|
4 | n/a | import unittest |
---|
5 | n/a | import collections |
---|
6 | n/a | |
---|
7 | n/a | d0 = {} |
---|
8 | n/a | d1 = {"one": 1} |
---|
9 | n/a | d2 = {"one": 1, "two": 2} |
---|
10 | n/a | d3 = {"one": 1, "two": 3, "three": 5} |
---|
11 | n/a | d4 = {"one": None, "two": None} |
---|
12 | n/a | d5 = {"one": 1, "two": 1} |
---|
13 | n/a | |
---|
14 | n/a | class UserDictTest(mapping_tests.TestHashMappingProtocol): |
---|
15 | n/a | type2test = collections.UserDict |
---|
16 | n/a | |
---|
17 | n/a | def test_all(self): |
---|
18 | n/a | # Test constructors |
---|
19 | n/a | u = collections.UserDict() |
---|
20 | n/a | u0 = collections.UserDict(d0) |
---|
21 | n/a | u1 = collections.UserDict(d1) |
---|
22 | n/a | u2 = collections.UserDict(d2) |
---|
23 | n/a | |
---|
24 | n/a | uu = collections.UserDict(u) |
---|
25 | n/a | uu0 = collections.UserDict(u0) |
---|
26 | n/a | uu1 = collections.UserDict(u1) |
---|
27 | n/a | uu2 = collections.UserDict(u2) |
---|
28 | n/a | |
---|
29 | n/a | # keyword arg constructor |
---|
30 | n/a | self.assertEqual(collections.UserDict(one=1, two=2), d2) |
---|
31 | n/a | # item sequence constructor |
---|
32 | n/a | self.assertEqual(collections.UserDict([('one',1), ('two',2)]), d2) |
---|
33 | n/a | with self.assertWarnsRegex(DeprecationWarning, "'dict'"): |
---|
34 | n/a | self.assertEqual(collections.UserDict(dict=[('one',1), ('two',2)]), d2) |
---|
35 | n/a | # both together |
---|
36 | n/a | self.assertEqual(collections.UserDict([('one',1), ('two',2)], two=3, three=5), d3) |
---|
37 | n/a | |
---|
38 | n/a | # alternate constructor |
---|
39 | n/a | self.assertEqual(collections.UserDict.fromkeys('one two'.split()), d4) |
---|
40 | n/a | self.assertEqual(collections.UserDict().fromkeys('one two'.split()), d4) |
---|
41 | n/a | self.assertEqual(collections.UserDict.fromkeys('one two'.split(), 1), d5) |
---|
42 | n/a | self.assertEqual(collections.UserDict().fromkeys('one two'.split(), 1), d5) |
---|
43 | n/a | self.assertTrue(u1.fromkeys('one two'.split()) is not u1) |
---|
44 | n/a | self.assertIsInstance(u1.fromkeys('one two'.split()), collections.UserDict) |
---|
45 | n/a | self.assertIsInstance(u2.fromkeys('one two'.split()), collections.UserDict) |
---|
46 | n/a | |
---|
47 | n/a | # Test __repr__ |
---|
48 | n/a | self.assertEqual(str(u0), str(d0)) |
---|
49 | n/a | self.assertEqual(repr(u1), repr(d1)) |
---|
50 | n/a | self.assertIn(repr(u2), ("{'one': 1, 'two': 2}", |
---|
51 | n/a | "{'two': 2, 'one': 1}")) |
---|
52 | n/a | |
---|
53 | n/a | # Test rich comparison and __len__ |
---|
54 | n/a | all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2] |
---|
55 | n/a | for a in all: |
---|
56 | n/a | for b in all: |
---|
57 | n/a | self.assertEqual(a == b, len(a) == len(b)) |
---|
58 | n/a | |
---|
59 | n/a | # Test __getitem__ |
---|
60 | n/a | self.assertEqual(u2["one"], 1) |
---|
61 | n/a | self.assertRaises(KeyError, u1.__getitem__, "two") |
---|
62 | n/a | |
---|
63 | n/a | # Test __setitem__ |
---|
64 | n/a | u3 = collections.UserDict(u2) |
---|
65 | n/a | u3["two"] = 2 |
---|
66 | n/a | u3["three"] = 3 |
---|
67 | n/a | |
---|
68 | n/a | # Test __delitem__ |
---|
69 | n/a | del u3["three"] |
---|
70 | n/a | self.assertRaises(KeyError, u3.__delitem__, "three") |
---|
71 | n/a | |
---|
72 | n/a | # Test clear |
---|
73 | n/a | u3.clear() |
---|
74 | n/a | self.assertEqual(u3, {}) |
---|
75 | n/a | |
---|
76 | n/a | # Test copy() |
---|
77 | n/a | u2a = u2.copy() |
---|
78 | n/a | self.assertEqual(u2a, u2) |
---|
79 | n/a | u2b = collections.UserDict(x=42, y=23) |
---|
80 | n/a | u2c = u2b.copy() # making a copy of a UserDict is special cased |
---|
81 | n/a | self.assertEqual(u2b, u2c) |
---|
82 | n/a | |
---|
83 | n/a | class MyUserDict(collections.UserDict): |
---|
84 | n/a | def display(self): print(self) |
---|
85 | n/a | |
---|
86 | n/a | m2 = MyUserDict(u2) |
---|
87 | n/a | m2a = m2.copy() |
---|
88 | n/a | self.assertEqual(m2a, m2) |
---|
89 | n/a | |
---|
90 | n/a | # SF bug #476616 -- copy() of UserDict subclass shared data |
---|
91 | n/a | m2['foo'] = 'bar' |
---|
92 | n/a | self.assertNotEqual(m2a, m2) |
---|
93 | n/a | |
---|
94 | n/a | # Test keys, items, values |
---|
95 | n/a | self.assertEqual(sorted(u2.keys()), sorted(d2.keys())) |
---|
96 | n/a | self.assertEqual(sorted(u2.items()), sorted(d2.items())) |
---|
97 | n/a | self.assertEqual(sorted(u2.values()), sorted(d2.values())) |
---|
98 | n/a | |
---|
99 | n/a | # Test "in". |
---|
100 | n/a | for i in u2.keys(): |
---|
101 | n/a | self.assertIn(i, u2) |
---|
102 | n/a | self.assertEqual(i in u1, i in d1) |
---|
103 | n/a | self.assertEqual(i in u0, i in d0) |
---|
104 | n/a | |
---|
105 | n/a | # Test update |
---|
106 | n/a | t = collections.UserDict() |
---|
107 | n/a | t.update(u2) |
---|
108 | n/a | self.assertEqual(t, u2) |
---|
109 | n/a | |
---|
110 | n/a | # Test get |
---|
111 | n/a | for i in u2.keys(): |
---|
112 | n/a | self.assertEqual(u2.get(i), u2[i]) |
---|
113 | n/a | self.assertEqual(u1.get(i), d1.get(i)) |
---|
114 | n/a | self.assertEqual(u0.get(i), d0.get(i)) |
---|
115 | n/a | |
---|
116 | n/a | # Test "in" iteration. |
---|
117 | n/a | for i in range(20): |
---|
118 | n/a | u2[i] = str(i) |
---|
119 | n/a | ikeys = [] |
---|
120 | n/a | for k in u2: |
---|
121 | n/a | ikeys.append(k) |
---|
122 | n/a | keys = u2.keys() |
---|
123 | n/a | self.assertEqual(set(ikeys), set(keys)) |
---|
124 | n/a | |
---|
125 | n/a | # Test setdefault |
---|
126 | n/a | t = collections.UserDict() |
---|
127 | n/a | self.assertEqual(t.setdefault("x", 42), 42) |
---|
128 | n/a | self.assertIn("x", t) |
---|
129 | n/a | self.assertEqual(t.setdefault("x", 23), 42) |
---|
130 | n/a | |
---|
131 | n/a | # Test pop |
---|
132 | n/a | t = collections.UserDict(x=42) |
---|
133 | n/a | self.assertEqual(t.pop("x"), 42) |
---|
134 | n/a | self.assertRaises(KeyError, t.pop, "x") |
---|
135 | n/a | self.assertEqual(t.pop("x", 1), 1) |
---|
136 | n/a | t["x"] = 42 |
---|
137 | n/a | self.assertEqual(t.pop("x", 1), 42) |
---|
138 | n/a | |
---|
139 | n/a | # Test popitem |
---|
140 | n/a | t = collections.UserDict(x=42) |
---|
141 | n/a | self.assertEqual(t.popitem(), ("x", 42)) |
---|
142 | n/a | self.assertRaises(KeyError, t.popitem) |
---|
143 | n/a | |
---|
144 | n/a | def test_init(self): |
---|
145 | n/a | for kw in 'self', 'other', 'iterable': |
---|
146 | n/a | self.assertEqual(list(collections.UserDict(**{kw: 42}).items()), |
---|
147 | n/a | [(kw, 42)]) |
---|
148 | n/a | self.assertEqual(list(collections.UserDict({}, dict=42).items()), |
---|
149 | n/a | [('dict', 42)]) |
---|
150 | n/a | self.assertEqual(list(collections.UserDict({}, dict=None).items()), |
---|
151 | n/a | [('dict', None)]) |
---|
152 | n/a | with self.assertWarnsRegex(DeprecationWarning, "'dict'"): |
---|
153 | n/a | self.assertEqual(list(collections.UserDict(dict={'a': 42}).items()), |
---|
154 | n/a | [('a', 42)]) |
---|
155 | n/a | self.assertRaises(TypeError, collections.UserDict, 42) |
---|
156 | n/a | self.assertRaises(TypeError, collections.UserDict, (), ()) |
---|
157 | n/a | self.assertRaises(TypeError, collections.UserDict.__init__) |
---|
158 | n/a | |
---|
159 | n/a | def test_update(self): |
---|
160 | n/a | for kw in 'self', 'dict', 'other', 'iterable': |
---|
161 | n/a | d = collections.UserDict() |
---|
162 | n/a | d.update(**{kw: 42}) |
---|
163 | n/a | self.assertEqual(list(d.items()), [(kw, 42)]) |
---|
164 | n/a | self.assertRaises(TypeError, collections.UserDict().update, 42) |
---|
165 | n/a | self.assertRaises(TypeError, collections.UserDict().update, {}, {}) |
---|
166 | n/a | self.assertRaises(TypeError, collections.UserDict.update) |
---|
167 | n/a | |
---|
168 | n/a | def test_missing(self): |
---|
169 | n/a | # Make sure UserDict doesn't have a __missing__ method |
---|
170 | n/a | self.assertEqual(hasattr(collections.UserDict, "__missing__"), False) |
---|
171 | n/a | # Test several cases: |
---|
172 | n/a | # (D) subclass defines __missing__ method returning a value |
---|
173 | n/a | # (E) subclass defines __missing__ method raising RuntimeError |
---|
174 | n/a | # (F) subclass sets __missing__ instance variable (no effect) |
---|
175 | n/a | # (G) subclass doesn't define __missing__ at all |
---|
176 | n/a | class D(collections.UserDict): |
---|
177 | n/a | def __missing__(self, key): |
---|
178 | n/a | return 42 |
---|
179 | n/a | d = D({1: 2, 3: 4}) |
---|
180 | n/a | self.assertEqual(d[1], 2) |
---|
181 | n/a | self.assertEqual(d[3], 4) |
---|
182 | n/a | self.assertNotIn(2, d) |
---|
183 | n/a | self.assertNotIn(2, d.keys()) |
---|
184 | n/a | self.assertEqual(d[2], 42) |
---|
185 | n/a | class E(collections.UserDict): |
---|
186 | n/a | def __missing__(self, key): |
---|
187 | n/a | raise RuntimeError(key) |
---|
188 | n/a | e = E() |
---|
189 | n/a | try: |
---|
190 | n/a | e[42] |
---|
191 | n/a | except RuntimeError as err: |
---|
192 | n/a | self.assertEqual(err.args, (42,)) |
---|
193 | n/a | else: |
---|
194 | n/a | self.fail("e[42] didn't raise RuntimeError") |
---|
195 | n/a | class F(collections.UserDict): |
---|
196 | n/a | def __init__(self): |
---|
197 | n/a | # An instance variable __missing__ should have no effect |
---|
198 | n/a | self.__missing__ = lambda key: None |
---|
199 | n/a | collections.UserDict.__init__(self) |
---|
200 | n/a | f = F() |
---|
201 | n/a | try: |
---|
202 | n/a | f[42] |
---|
203 | n/a | except KeyError as err: |
---|
204 | n/a | self.assertEqual(err.args, (42,)) |
---|
205 | n/a | else: |
---|
206 | n/a | self.fail("f[42] didn't raise KeyError") |
---|
207 | n/a | class G(collections.UserDict): |
---|
208 | n/a | pass |
---|
209 | n/a | g = G() |
---|
210 | n/a | try: |
---|
211 | n/a | g[42] |
---|
212 | n/a | except KeyError as err: |
---|
213 | n/a | self.assertEqual(err.args, (42,)) |
---|
214 | n/a | else: |
---|
215 | n/a | self.fail("g[42] didn't raise KeyError") |
---|
216 | n/a | |
---|
217 | n/a | |
---|
218 | n/a | |
---|
219 | n/a | if __name__ == "__main__": |
---|
220 | n/a | unittest.main() |
---|