1 | n/a | # tests common to dict and UserDict |
---|
2 | n/a | import unittest |
---|
3 | n/a | import collections |
---|
4 | n/a | |
---|
5 | n/a | |
---|
6 | n/a | class BasicTestMappingProtocol(unittest.TestCase): |
---|
7 | n/a | # This base class can be used to check that an object conforms to the |
---|
8 | n/a | # mapping protocol |
---|
9 | n/a | |
---|
10 | n/a | # Functions that can be useful to override to adapt to dictionary |
---|
11 | n/a | # semantics |
---|
12 | n/a | type2test = None # which class is being tested (overwrite in subclasses) |
---|
13 | n/a | |
---|
14 | n/a | def _reference(self): |
---|
15 | n/a | """Return a dictionary of values which are invariant by storage |
---|
16 | n/a | in the object under test.""" |
---|
17 | n/a | return {"1": "2", "key1":"value1", "key2":(1,2,3)} |
---|
18 | n/a | def _empty_mapping(self): |
---|
19 | n/a | """Return an empty mapping object""" |
---|
20 | n/a | return self.type2test() |
---|
21 | n/a | def _full_mapping(self, data): |
---|
22 | n/a | """Return a mapping object with the value contained in data |
---|
23 | n/a | dictionary""" |
---|
24 | n/a | x = self._empty_mapping() |
---|
25 | n/a | for key, value in data.items(): |
---|
26 | n/a | x[key] = value |
---|
27 | n/a | return x |
---|
28 | n/a | |
---|
29 | n/a | def __init__(self, *args, **kw): |
---|
30 | n/a | unittest.TestCase.__init__(self, *args, **kw) |
---|
31 | n/a | self.reference = self._reference().copy() |
---|
32 | n/a | |
---|
33 | n/a | # A (key, value) pair not in the mapping |
---|
34 | n/a | key, value = self.reference.popitem() |
---|
35 | n/a | self.other = {key:value} |
---|
36 | n/a | |
---|
37 | n/a | # A (key, value) pair in the mapping |
---|
38 | n/a | key, value = self.reference.popitem() |
---|
39 | n/a | self.inmapping = {key:value} |
---|
40 | n/a | self.reference[key] = value |
---|
41 | n/a | |
---|
42 | n/a | def test_read(self): |
---|
43 | n/a | # Test for read only operations on mapping |
---|
44 | n/a | p = self._empty_mapping() |
---|
45 | n/a | p1 = dict(p) #workaround for singleton objects |
---|
46 | n/a | d = self._full_mapping(self.reference) |
---|
47 | n/a | if d is p: |
---|
48 | n/a | p = p1 |
---|
49 | n/a | #Indexing |
---|
50 | n/a | for key, value in self.reference.items(): |
---|
51 | n/a | self.assertEqual(d[key], value) |
---|
52 | n/a | knownkey = list(self.other.keys())[0] |
---|
53 | n/a | self.assertRaises(KeyError, lambda:d[knownkey]) |
---|
54 | n/a | #len |
---|
55 | n/a | self.assertEqual(len(p), 0) |
---|
56 | n/a | self.assertEqual(len(d), len(self.reference)) |
---|
57 | n/a | #__contains__ |
---|
58 | n/a | for k in self.reference: |
---|
59 | n/a | self.assertIn(k, d) |
---|
60 | n/a | for k in self.other: |
---|
61 | n/a | self.assertNotIn(k, d) |
---|
62 | n/a | #cmp |
---|
63 | n/a | self.assertEqual(p, p) |
---|
64 | n/a | self.assertEqual(d, d) |
---|
65 | n/a | self.assertNotEqual(p, d) |
---|
66 | n/a | self.assertNotEqual(d, p) |
---|
67 | n/a | #bool |
---|
68 | n/a | if p: self.fail("Empty mapping must compare to False") |
---|
69 | n/a | if not d: self.fail("Full mapping must compare to True") |
---|
70 | n/a | # keys(), items(), iterkeys() ... |
---|
71 | n/a | def check_iterandlist(iter, lst, ref): |
---|
72 | n/a | self.assertTrue(hasattr(iter, '__next__')) |
---|
73 | n/a | self.assertTrue(hasattr(iter, '__iter__')) |
---|
74 | n/a | x = list(iter) |
---|
75 | n/a | self.assertTrue(set(x)==set(lst)==set(ref)) |
---|
76 | n/a | check_iterandlist(iter(d.keys()), list(d.keys()), |
---|
77 | n/a | self.reference.keys()) |
---|
78 | n/a | check_iterandlist(iter(d), list(d.keys()), self.reference.keys()) |
---|
79 | n/a | check_iterandlist(iter(d.values()), list(d.values()), |
---|
80 | n/a | self.reference.values()) |
---|
81 | n/a | check_iterandlist(iter(d.items()), list(d.items()), |
---|
82 | n/a | self.reference.items()) |
---|
83 | n/a | #get |
---|
84 | n/a | key, value = next(iter(d.items())) |
---|
85 | n/a | knownkey, knownvalue = next(iter(self.other.items())) |
---|
86 | n/a | self.assertEqual(d.get(key, knownvalue), value) |
---|
87 | n/a | self.assertEqual(d.get(knownkey, knownvalue), knownvalue) |
---|
88 | n/a | self.assertNotIn(knownkey, d) |
---|
89 | n/a | |
---|
90 | n/a | def test_write(self): |
---|
91 | n/a | # Test for write operations on mapping |
---|
92 | n/a | p = self._empty_mapping() |
---|
93 | n/a | #Indexing |
---|
94 | n/a | for key, value in self.reference.items(): |
---|
95 | n/a | p[key] = value |
---|
96 | n/a | self.assertEqual(p[key], value) |
---|
97 | n/a | for key in self.reference.keys(): |
---|
98 | n/a | del p[key] |
---|
99 | n/a | self.assertRaises(KeyError, lambda:p[key]) |
---|
100 | n/a | p = self._empty_mapping() |
---|
101 | n/a | #update |
---|
102 | n/a | p.update(self.reference) |
---|
103 | n/a | self.assertEqual(dict(p), self.reference) |
---|
104 | n/a | items = list(p.items()) |
---|
105 | n/a | p = self._empty_mapping() |
---|
106 | n/a | p.update(items) |
---|
107 | n/a | self.assertEqual(dict(p), self.reference) |
---|
108 | n/a | d = self._full_mapping(self.reference) |
---|
109 | n/a | #setdefault |
---|
110 | n/a | key, value = next(iter(d.items())) |
---|
111 | n/a | knownkey, knownvalue = next(iter(self.other.items())) |
---|
112 | n/a | self.assertEqual(d.setdefault(key, knownvalue), value) |
---|
113 | n/a | self.assertEqual(d[key], value) |
---|
114 | n/a | self.assertEqual(d.setdefault(knownkey, knownvalue), knownvalue) |
---|
115 | n/a | self.assertEqual(d[knownkey], knownvalue) |
---|
116 | n/a | #pop |
---|
117 | n/a | self.assertEqual(d.pop(knownkey), knownvalue) |
---|
118 | n/a | self.assertNotIn(knownkey, d) |
---|
119 | n/a | self.assertRaises(KeyError, d.pop, knownkey) |
---|
120 | n/a | default = 909 |
---|
121 | n/a | d[knownkey] = knownvalue |
---|
122 | n/a | self.assertEqual(d.pop(knownkey, default), knownvalue) |
---|
123 | n/a | self.assertNotIn(knownkey, d) |
---|
124 | n/a | self.assertEqual(d.pop(knownkey, default), default) |
---|
125 | n/a | #popitem |
---|
126 | n/a | key, value = d.popitem() |
---|
127 | n/a | self.assertNotIn(key, d) |
---|
128 | n/a | self.assertEqual(value, self.reference[key]) |
---|
129 | n/a | p=self._empty_mapping() |
---|
130 | n/a | self.assertRaises(KeyError, p.popitem) |
---|
131 | n/a | |
---|
132 | n/a | def test_constructor(self): |
---|
133 | n/a | self.assertEqual(self._empty_mapping(), self._empty_mapping()) |
---|
134 | n/a | |
---|
135 | n/a | def test_bool(self): |
---|
136 | n/a | self.assertTrue(not self._empty_mapping()) |
---|
137 | n/a | self.assertTrue(self.reference) |
---|
138 | n/a | self.assertTrue(bool(self._empty_mapping()) is False) |
---|
139 | n/a | self.assertTrue(bool(self.reference) is True) |
---|
140 | n/a | |
---|
141 | n/a | def test_keys(self): |
---|
142 | n/a | d = self._empty_mapping() |
---|
143 | n/a | self.assertEqual(list(d.keys()), []) |
---|
144 | n/a | d = self.reference |
---|
145 | n/a | self.assertIn(list(self.inmapping.keys())[0], d.keys()) |
---|
146 | n/a | self.assertNotIn(list(self.other.keys())[0], d.keys()) |
---|
147 | n/a | self.assertRaises(TypeError, d.keys, None) |
---|
148 | n/a | |
---|
149 | n/a | def test_values(self): |
---|
150 | n/a | d = self._empty_mapping() |
---|
151 | n/a | self.assertEqual(list(d.values()), []) |
---|
152 | n/a | |
---|
153 | n/a | self.assertRaises(TypeError, d.values, None) |
---|
154 | n/a | |
---|
155 | n/a | def test_items(self): |
---|
156 | n/a | d = self._empty_mapping() |
---|
157 | n/a | self.assertEqual(list(d.items()), []) |
---|
158 | n/a | |
---|
159 | n/a | self.assertRaises(TypeError, d.items, None) |
---|
160 | n/a | |
---|
161 | n/a | def test_len(self): |
---|
162 | n/a | d = self._empty_mapping() |
---|
163 | n/a | self.assertEqual(len(d), 0) |
---|
164 | n/a | |
---|
165 | n/a | def test_getitem(self): |
---|
166 | n/a | d = self.reference |
---|
167 | n/a | self.assertEqual(d[list(self.inmapping.keys())[0]], |
---|
168 | n/a | list(self.inmapping.values())[0]) |
---|
169 | n/a | |
---|
170 | n/a | self.assertRaises(TypeError, d.__getitem__) |
---|
171 | n/a | |
---|
172 | n/a | def test_update(self): |
---|
173 | n/a | # mapping argument |
---|
174 | n/a | d = self._empty_mapping() |
---|
175 | n/a | d.update(self.other) |
---|
176 | n/a | self.assertEqual(list(d.items()), list(self.other.items())) |
---|
177 | n/a | |
---|
178 | n/a | # No argument |
---|
179 | n/a | d = self._empty_mapping() |
---|
180 | n/a | d.update() |
---|
181 | n/a | self.assertEqual(d, self._empty_mapping()) |
---|
182 | n/a | |
---|
183 | n/a | # item sequence |
---|
184 | n/a | d = self._empty_mapping() |
---|
185 | n/a | d.update(self.other.items()) |
---|
186 | n/a | self.assertEqual(list(d.items()), list(self.other.items())) |
---|
187 | n/a | |
---|
188 | n/a | # Iterator |
---|
189 | n/a | d = self._empty_mapping() |
---|
190 | n/a | d.update(self.other.items()) |
---|
191 | n/a | self.assertEqual(list(d.items()), list(self.other.items())) |
---|
192 | n/a | |
---|
193 | n/a | # FIXME: Doesn't work with UserDict |
---|
194 | n/a | # self.assertRaises((TypeError, AttributeError), d.update, None) |
---|
195 | n/a | self.assertRaises((TypeError, AttributeError), d.update, 42) |
---|
196 | n/a | |
---|
197 | n/a | outerself = self |
---|
198 | n/a | class SimpleUserDict: |
---|
199 | n/a | def __init__(self): |
---|
200 | n/a | self.d = outerself.reference |
---|
201 | n/a | def keys(self): |
---|
202 | n/a | return self.d.keys() |
---|
203 | n/a | def __getitem__(self, i): |
---|
204 | n/a | return self.d[i] |
---|
205 | n/a | d.clear() |
---|
206 | n/a | d.update(SimpleUserDict()) |
---|
207 | n/a | i1 = sorted(d.items()) |
---|
208 | n/a | i2 = sorted(self.reference.items()) |
---|
209 | n/a | self.assertEqual(i1, i2) |
---|
210 | n/a | |
---|
211 | n/a | class Exc(Exception): pass |
---|
212 | n/a | |
---|
213 | n/a | d = self._empty_mapping() |
---|
214 | n/a | class FailingUserDict: |
---|
215 | n/a | def keys(self): |
---|
216 | n/a | raise Exc |
---|
217 | n/a | self.assertRaises(Exc, d.update, FailingUserDict()) |
---|
218 | n/a | |
---|
219 | n/a | d.clear() |
---|
220 | n/a | |
---|
221 | n/a | class FailingUserDict: |
---|
222 | n/a | def keys(self): |
---|
223 | n/a | class BogonIter: |
---|
224 | n/a | def __init__(self): |
---|
225 | n/a | self.i = 1 |
---|
226 | n/a | def __iter__(self): |
---|
227 | n/a | return self |
---|
228 | n/a | def __next__(self): |
---|
229 | n/a | if self.i: |
---|
230 | n/a | self.i = 0 |
---|
231 | n/a | return 'a' |
---|
232 | n/a | raise Exc |
---|
233 | n/a | return BogonIter() |
---|
234 | n/a | def __getitem__(self, key): |
---|
235 | n/a | return key |
---|
236 | n/a | self.assertRaises(Exc, d.update, FailingUserDict()) |
---|
237 | n/a | |
---|
238 | n/a | class FailingUserDict: |
---|
239 | n/a | def keys(self): |
---|
240 | n/a | class BogonIter: |
---|
241 | n/a | def __init__(self): |
---|
242 | n/a | self.i = ord('a') |
---|
243 | n/a | def __iter__(self): |
---|
244 | n/a | return self |
---|
245 | n/a | def __next__(self): |
---|
246 | n/a | if self.i <= ord('z'): |
---|
247 | n/a | rtn = chr(self.i) |
---|
248 | n/a | self.i += 1 |
---|
249 | n/a | return rtn |
---|
250 | n/a | raise StopIteration |
---|
251 | n/a | return BogonIter() |
---|
252 | n/a | def __getitem__(self, key): |
---|
253 | n/a | raise Exc |
---|
254 | n/a | self.assertRaises(Exc, d.update, FailingUserDict()) |
---|
255 | n/a | |
---|
256 | n/a | d = self._empty_mapping() |
---|
257 | n/a | class badseq(object): |
---|
258 | n/a | def __iter__(self): |
---|
259 | n/a | return self |
---|
260 | n/a | def __next__(self): |
---|
261 | n/a | raise Exc() |
---|
262 | n/a | |
---|
263 | n/a | self.assertRaises(Exc, d.update, badseq()) |
---|
264 | n/a | |
---|
265 | n/a | self.assertRaises(ValueError, d.update, [(1, 2, 3)]) |
---|
266 | n/a | |
---|
267 | n/a | # no test_fromkeys or test_copy as both os.environ and selves don't support it |
---|
268 | n/a | |
---|
269 | n/a | def test_get(self): |
---|
270 | n/a | d = self._empty_mapping() |
---|
271 | n/a | self.assertTrue(d.get(list(self.other.keys())[0]) is None) |
---|
272 | n/a | self.assertEqual(d.get(list(self.other.keys())[0], 3), 3) |
---|
273 | n/a | d = self.reference |
---|
274 | n/a | self.assertTrue(d.get(list(self.other.keys())[0]) is None) |
---|
275 | n/a | self.assertEqual(d.get(list(self.other.keys())[0], 3), 3) |
---|
276 | n/a | self.assertEqual(d.get(list(self.inmapping.keys())[0]), |
---|
277 | n/a | list(self.inmapping.values())[0]) |
---|
278 | n/a | self.assertEqual(d.get(list(self.inmapping.keys())[0], 3), |
---|
279 | n/a | list(self.inmapping.values())[0]) |
---|
280 | n/a | self.assertRaises(TypeError, d.get) |
---|
281 | n/a | self.assertRaises(TypeError, d.get, None, None, None) |
---|
282 | n/a | |
---|
283 | n/a | def test_setdefault(self): |
---|
284 | n/a | d = self._empty_mapping() |
---|
285 | n/a | self.assertRaises(TypeError, d.setdefault) |
---|
286 | n/a | |
---|
287 | n/a | def test_popitem(self): |
---|
288 | n/a | d = self._empty_mapping() |
---|
289 | n/a | self.assertRaises(KeyError, d.popitem) |
---|
290 | n/a | self.assertRaises(TypeError, d.popitem, 42) |
---|
291 | n/a | |
---|
292 | n/a | def test_pop(self): |
---|
293 | n/a | d = self._empty_mapping() |
---|
294 | n/a | k, v = list(self.inmapping.items())[0] |
---|
295 | n/a | d[k] = v |
---|
296 | n/a | self.assertRaises(KeyError, d.pop, list(self.other.keys())[0]) |
---|
297 | n/a | |
---|
298 | n/a | self.assertEqual(d.pop(k), v) |
---|
299 | n/a | self.assertEqual(len(d), 0) |
---|
300 | n/a | |
---|
301 | n/a | self.assertRaises(KeyError, d.pop, k) |
---|
302 | n/a | |
---|
303 | n/a | |
---|
304 | n/a | class TestMappingProtocol(BasicTestMappingProtocol): |
---|
305 | n/a | def test_constructor(self): |
---|
306 | n/a | BasicTestMappingProtocol.test_constructor(self) |
---|
307 | n/a | self.assertTrue(self._empty_mapping() is not self._empty_mapping()) |
---|
308 | n/a | self.assertEqual(self.type2test(x=1, y=2), {"x": 1, "y": 2}) |
---|
309 | n/a | |
---|
310 | n/a | def test_bool(self): |
---|
311 | n/a | BasicTestMappingProtocol.test_bool(self) |
---|
312 | n/a | self.assertTrue(not self._empty_mapping()) |
---|
313 | n/a | self.assertTrue(self._full_mapping({"x": "y"})) |
---|
314 | n/a | self.assertTrue(bool(self._empty_mapping()) is False) |
---|
315 | n/a | self.assertTrue(bool(self._full_mapping({"x": "y"})) is True) |
---|
316 | n/a | |
---|
317 | n/a | def test_keys(self): |
---|
318 | n/a | BasicTestMappingProtocol.test_keys(self) |
---|
319 | n/a | d = self._empty_mapping() |
---|
320 | n/a | self.assertEqual(list(d.keys()), []) |
---|
321 | n/a | d = self._full_mapping({'a': 1, 'b': 2}) |
---|
322 | n/a | k = d.keys() |
---|
323 | n/a | self.assertIn('a', k) |
---|
324 | n/a | self.assertIn('b', k) |
---|
325 | n/a | self.assertNotIn('c', k) |
---|
326 | n/a | |
---|
327 | n/a | def test_values(self): |
---|
328 | n/a | BasicTestMappingProtocol.test_values(self) |
---|
329 | n/a | d = self._full_mapping({1:2}) |
---|
330 | n/a | self.assertEqual(list(d.values()), [2]) |
---|
331 | n/a | |
---|
332 | n/a | def test_items(self): |
---|
333 | n/a | BasicTestMappingProtocol.test_items(self) |
---|
334 | n/a | |
---|
335 | n/a | d = self._full_mapping({1:2}) |
---|
336 | n/a | self.assertEqual(list(d.items()), [(1, 2)]) |
---|
337 | n/a | |
---|
338 | n/a | def test_contains(self): |
---|
339 | n/a | d = self._empty_mapping() |
---|
340 | n/a | self.assertNotIn('a', d) |
---|
341 | n/a | self.assertTrue(not ('a' in d)) |
---|
342 | n/a | self.assertTrue('a' not in d) |
---|
343 | n/a | d = self._full_mapping({'a': 1, 'b': 2}) |
---|
344 | n/a | self.assertIn('a', d) |
---|
345 | n/a | self.assertIn('b', d) |
---|
346 | n/a | self.assertNotIn('c', d) |
---|
347 | n/a | |
---|
348 | n/a | self.assertRaises(TypeError, d.__contains__) |
---|
349 | n/a | |
---|
350 | n/a | def test_len(self): |
---|
351 | n/a | BasicTestMappingProtocol.test_len(self) |
---|
352 | n/a | d = self._full_mapping({'a': 1, 'b': 2}) |
---|
353 | n/a | self.assertEqual(len(d), 2) |
---|
354 | n/a | |
---|
355 | n/a | def test_getitem(self): |
---|
356 | n/a | BasicTestMappingProtocol.test_getitem(self) |
---|
357 | n/a | d = self._full_mapping({'a': 1, 'b': 2}) |
---|
358 | n/a | self.assertEqual(d['a'], 1) |
---|
359 | n/a | self.assertEqual(d['b'], 2) |
---|
360 | n/a | d['c'] = 3 |
---|
361 | n/a | d['a'] = 4 |
---|
362 | n/a | self.assertEqual(d['c'], 3) |
---|
363 | n/a | self.assertEqual(d['a'], 4) |
---|
364 | n/a | del d['b'] |
---|
365 | n/a | self.assertEqual(d, {'a': 4, 'c': 3}) |
---|
366 | n/a | |
---|
367 | n/a | self.assertRaises(TypeError, d.__getitem__) |
---|
368 | n/a | |
---|
369 | n/a | def test_clear(self): |
---|
370 | n/a | d = self._full_mapping({1:1, 2:2, 3:3}) |
---|
371 | n/a | d.clear() |
---|
372 | n/a | self.assertEqual(d, {}) |
---|
373 | n/a | |
---|
374 | n/a | self.assertRaises(TypeError, d.clear, None) |
---|
375 | n/a | |
---|
376 | n/a | def test_update(self): |
---|
377 | n/a | BasicTestMappingProtocol.test_update(self) |
---|
378 | n/a | # mapping argument |
---|
379 | n/a | d = self._empty_mapping() |
---|
380 | n/a | d.update({1:100}) |
---|
381 | n/a | d.update({2:20}) |
---|
382 | n/a | d.update({1:1, 2:2, 3:3}) |
---|
383 | n/a | self.assertEqual(d, {1:1, 2:2, 3:3}) |
---|
384 | n/a | |
---|
385 | n/a | # no argument |
---|
386 | n/a | d.update() |
---|
387 | n/a | self.assertEqual(d, {1:1, 2:2, 3:3}) |
---|
388 | n/a | |
---|
389 | n/a | # keyword arguments |
---|
390 | n/a | d = self._empty_mapping() |
---|
391 | n/a | d.update(x=100) |
---|
392 | n/a | d.update(y=20) |
---|
393 | n/a | d.update(x=1, y=2, z=3) |
---|
394 | n/a | self.assertEqual(d, {"x":1, "y":2, "z":3}) |
---|
395 | n/a | |
---|
396 | n/a | # item sequence |
---|
397 | n/a | d = self._empty_mapping() |
---|
398 | n/a | d.update([("x", 100), ("y", 20)]) |
---|
399 | n/a | self.assertEqual(d, {"x":100, "y":20}) |
---|
400 | n/a | |
---|
401 | n/a | # Both item sequence and keyword arguments |
---|
402 | n/a | d = self._empty_mapping() |
---|
403 | n/a | d.update([("x", 100), ("y", 20)], x=1, y=2) |
---|
404 | n/a | self.assertEqual(d, {"x":1, "y":2}) |
---|
405 | n/a | |
---|
406 | n/a | # iterator |
---|
407 | n/a | d = self._full_mapping({1:3, 2:4}) |
---|
408 | n/a | d.update(self._full_mapping({1:2, 3:4, 5:6}).items()) |
---|
409 | n/a | self.assertEqual(d, {1:2, 2:4, 3:4, 5:6}) |
---|
410 | n/a | |
---|
411 | n/a | class SimpleUserDict: |
---|
412 | n/a | def __init__(self): |
---|
413 | n/a | self.d = {1:1, 2:2, 3:3} |
---|
414 | n/a | def keys(self): |
---|
415 | n/a | return self.d.keys() |
---|
416 | n/a | def __getitem__(self, i): |
---|
417 | n/a | return self.d[i] |
---|
418 | n/a | d.clear() |
---|
419 | n/a | d.update(SimpleUserDict()) |
---|
420 | n/a | self.assertEqual(d, {1:1, 2:2, 3:3}) |
---|
421 | n/a | |
---|
422 | n/a | def test_fromkeys(self): |
---|
423 | n/a | self.assertEqual(self.type2test.fromkeys('abc'), {'a':None, 'b':None, 'c':None}) |
---|
424 | n/a | d = self._empty_mapping() |
---|
425 | n/a | self.assertTrue(not(d.fromkeys('abc') is d)) |
---|
426 | n/a | self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None}) |
---|
427 | n/a | self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0}) |
---|
428 | n/a | self.assertEqual(d.fromkeys([]), {}) |
---|
429 | n/a | def g(): |
---|
430 | n/a | yield 1 |
---|
431 | n/a | self.assertEqual(d.fromkeys(g()), {1:None}) |
---|
432 | n/a | self.assertRaises(TypeError, {}.fromkeys, 3) |
---|
433 | n/a | class dictlike(self.type2test): pass |
---|
434 | n/a | self.assertEqual(dictlike.fromkeys('a'), {'a':None}) |
---|
435 | n/a | self.assertEqual(dictlike().fromkeys('a'), {'a':None}) |
---|
436 | n/a | self.assertTrue(dictlike.fromkeys('a').__class__ is dictlike) |
---|
437 | n/a | self.assertTrue(dictlike().fromkeys('a').__class__ is dictlike) |
---|
438 | n/a | self.assertTrue(type(dictlike.fromkeys('a')) is dictlike) |
---|
439 | n/a | class mydict(self.type2test): |
---|
440 | n/a | def __new__(cls): |
---|
441 | n/a | return collections.UserDict() |
---|
442 | n/a | ud = mydict.fromkeys('ab') |
---|
443 | n/a | self.assertEqual(ud, {'a':None, 'b':None}) |
---|
444 | n/a | self.assertIsInstance(ud, collections.UserDict) |
---|
445 | n/a | self.assertRaises(TypeError, dict.fromkeys) |
---|
446 | n/a | |
---|
447 | n/a | class Exc(Exception): pass |
---|
448 | n/a | |
---|
449 | n/a | class baddict1(self.type2test): |
---|
450 | n/a | def __init__(self): |
---|
451 | n/a | raise Exc() |
---|
452 | n/a | |
---|
453 | n/a | self.assertRaises(Exc, baddict1.fromkeys, [1]) |
---|
454 | n/a | |
---|
455 | n/a | class BadSeq(object): |
---|
456 | n/a | def __iter__(self): |
---|
457 | n/a | return self |
---|
458 | n/a | def __next__(self): |
---|
459 | n/a | raise Exc() |
---|
460 | n/a | |
---|
461 | n/a | self.assertRaises(Exc, self.type2test.fromkeys, BadSeq()) |
---|
462 | n/a | |
---|
463 | n/a | class baddict2(self.type2test): |
---|
464 | n/a | def __setitem__(self, key, value): |
---|
465 | n/a | raise Exc() |
---|
466 | n/a | |
---|
467 | n/a | self.assertRaises(Exc, baddict2.fromkeys, [1]) |
---|
468 | n/a | |
---|
469 | n/a | def test_copy(self): |
---|
470 | n/a | d = self._full_mapping({1:1, 2:2, 3:3}) |
---|
471 | n/a | self.assertEqual(d.copy(), {1:1, 2:2, 3:3}) |
---|
472 | n/a | d = self._empty_mapping() |
---|
473 | n/a | self.assertEqual(d.copy(), d) |
---|
474 | n/a | self.assertIsInstance(d.copy(), d.__class__) |
---|
475 | n/a | self.assertRaises(TypeError, d.copy, None) |
---|
476 | n/a | |
---|
477 | n/a | def test_get(self): |
---|
478 | n/a | BasicTestMappingProtocol.test_get(self) |
---|
479 | n/a | d = self._empty_mapping() |
---|
480 | n/a | self.assertTrue(d.get('c') is None) |
---|
481 | n/a | self.assertEqual(d.get('c', 3), 3) |
---|
482 | n/a | d = self._full_mapping({'a' : 1, 'b' : 2}) |
---|
483 | n/a | self.assertTrue(d.get('c') is None) |
---|
484 | n/a | self.assertEqual(d.get('c', 3), 3) |
---|
485 | n/a | self.assertEqual(d.get('a'), 1) |
---|
486 | n/a | self.assertEqual(d.get('a', 3), 1) |
---|
487 | n/a | |
---|
488 | n/a | def test_setdefault(self): |
---|
489 | n/a | BasicTestMappingProtocol.test_setdefault(self) |
---|
490 | n/a | d = self._empty_mapping() |
---|
491 | n/a | self.assertTrue(d.setdefault('key0') is None) |
---|
492 | n/a | d.setdefault('key0', []) |
---|
493 | n/a | self.assertTrue(d.setdefault('key0') is None) |
---|
494 | n/a | d.setdefault('key', []).append(3) |
---|
495 | n/a | self.assertEqual(d['key'][0], 3) |
---|
496 | n/a | d.setdefault('key', []).append(4) |
---|
497 | n/a | self.assertEqual(len(d['key']), 2) |
---|
498 | n/a | |
---|
499 | n/a | def test_popitem(self): |
---|
500 | n/a | BasicTestMappingProtocol.test_popitem(self) |
---|
501 | n/a | for copymode in -1, +1: |
---|
502 | n/a | # -1: b has same structure as a |
---|
503 | n/a | # +1: b is a.copy() |
---|
504 | n/a | for log2size in range(12): |
---|
505 | n/a | size = 2**log2size |
---|
506 | n/a | a = self._empty_mapping() |
---|
507 | n/a | b = self._empty_mapping() |
---|
508 | n/a | for i in range(size): |
---|
509 | n/a | a[repr(i)] = i |
---|
510 | n/a | if copymode < 0: |
---|
511 | n/a | b[repr(i)] = i |
---|
512 | n/a | if copymode > 0: |
---|
513 | n/a | b = a.copy() |
---|
514 | n/a | for i in range(size): |
---|
515 | n/a | ka, va = ta = a.popitem() |
---|
516 | n/a | self.assertEqual(va, int(ka)) |
---|
517 | n/a | kb, vb = tb = b.popitem() |
---|
518 | n/a | self.assertEqual(vb, int(kb)) |
---|
519 | n/a | self.assertTrue(not(copymode < 0 and ta != tb)) |
---|
520 | n/a | self.assertTrue(not a) |
---|
521 | n/a | self.assertTrue(not b) |
---|
522 | n/a | |
---|
523 | n/a | def test_pop(self): |
---|
524 | n/a | BasicTestMappingProtocol.test_pop(self) |
---|
525 | n/a | |
---|
526 | n/a | # Tests for pop with specified key |
---|
527 | n/a | d = self._empty_mapping() |
---|
528 | n/a | k, v = 'abc', 'def' |
---|
529 | n/a | |
---|
530 | n/a | self.assertEqual(d.pop(k, v), v) |
---|
531 | n/a | d[k] = v |
---|
532 | n/a | self.assertEqual(d.pop(k, 1), v) |
---|
533 | n/a | |
---|
534 | n/a | |
---|
535 | n/a | class TestHashMappingProtocol(TestMappingProtocol): |
---|
536 | n/a | |
---|
537 | n/a | def test_getitem(self): |
---|
538 | n/a | TestMappingProtocol.test_getitem(self) |
---|
539 | n/a | class Exc(Exception): pass |
---|
540 | n/a | |
---|
541 | n/a | class BadEq(object): |
---|
542 | n/a | def __eq__(self, other): |
---|
543 | n/a | raise Exc() |
---|
544 | n/a | def __hash__(self): |
---|
545 | n/a | return 24 |
---|
546 | n/a | |
---|
547 | n/a | d = self._empty_mapping() |
---|
548 | n/a | d[BadEq()] = 42 |
---|
549 | n/a | self.assertRaises(KeyError, d.__getitem__, 23) |
---|
550 | n/a | |
---|
551 | n/a | class BadHash(object): |
---|
552 | n/a | fail = False |
---|
553 | n/a | def __hash__(self): |
---|
554 | n/a | if self.fail: |
---|
555 | n/a | raise Exc() |
---|
556 | n/a | else: |
---|
557 | n/a | return 42 |
---|
558 | n/a | |
---|
559 | n/a | d = self._empty_mapping() |
---|
560 | n/a | x = BadHash() |
---|
561 | n/a | d[x] = 42 |
---|
562 | n/a | x.fail = True |
---|
563 | n/a | self.assertRaises(Exc, d.__getitem__, x) |
---|
564 | n/a | |
---|
565 | n/a | def test_fromkeys(self): |
---|
566 | n/a | TestMappingProtocol.test_fromkeys(self) |
---|
567 | n/a | class mydict(self.type2test): |
---|
568 | n/a | def __new__(cls): |
---|
569 | n/a | return collections.UserDict() |
---|
570 | n/a | ud = mydict.fromkeys('ab') |
---|
571 | n/a | self.assertEqual(ud, {'a':None, 'b':None}) |
---|
572 | n/a | self.assertIsInstance(ud, collections.UserDict) |
---|
573 | n/a | |
---|
574 | n/a | def test_pop(self): |
---|
575 | n/a | TestMappingProtocol.test_pop(self) |
---|
576 | n/a | |
---|
577 | n/a | class Exc(Exception): pass |
---|
578 | n/a | |
---|
579 | n/a | class BadHash(object): |
---|
580 | n/a | fail = False |
---|
581 | n/a | def __hash__(self): |
---|
582 | n/a | if self.fail: |
---|
583 | n/a | raise Exc() |
---|
584 | n/a | else: |
---|
585 | n/a | return 42 |
---|
586 | n/a | |
---|
587 | n/a | d = self._empty_mapping() |
---|
588 | n/a | x = BadHash() |
---|
589 | n/a | d[x] = 42 |
---|
590 | n/a | x.fail = True |
---|
591 | n/a | self.assertRaises(Exc, d.pop, x) |
---|
592 | n/a | |
---|
593 | n/a | def test_mutatingiteration(self): |
---|
594 | n/a | d = self._empty_mapping() |
---|
595 | n/a | d[1] = 1 |
---|
596 | n/a | try: |
---|
597 | n/a | for i in d: |
---|
598 | n/a | d[i+1] = 1 |
---|
599 | n/a | except RuntimeError: |
---|
600 | n/a | pass |
---|
601 | n/a | else: |
---|
602 | n/a | self.fail("changing dict size during iteration doesn't raise Error") |
---|
603 | n/a | |
---|
604 | n/a | def test_repr(self): |
---|
605 | n/a | d = self._empty_mapping() |
---|
606 | n/a | self.assertEqual(repr(d), '{}') |
---|
607 | n/a | d[1] = 2 |
---|
608 | n/a | self.assertEqual(repr(d), '{1: 2}') |
---|
609 | n/a | d = self._empty_mapping() |
---|
610 | n/a | d[1] = d |
---|
611 | n/a | self.assertEqual(repr(d), '{1: {...}}') |
---|
612 | n/a | |
---|
613 | n/a | class Exc(Exception): pass |
---|
614 | n/a | |
---|
615 | n/a | class BadRepr(object): |
---|
616 | n/a | def __repr__(self): |
---|
617 | n/a | raise Exc() |
---|
618 | n/a | |
---|
619 | n/a | d = self._full_mapping({1: BadRepr()}) |
---|
620 | n/a | self.assertRaises(Exc, repr, d) |
---|
621 | n/a | |
---|
622 | n/a | def test_eq(self): |
---|
623 | n/a | self.assertEqual(self._empty_mapping(), self._empty_mapping()) |
---|
624 | n/a | self.assertEqual(self._full_mapping({1: 2}), |
---|
625 | n/a | self._full_mapping({1: 2})) |
---|
626 | n/a | |
---|
627 | n/a | class Exc(Exception): pass |
---|
628 | n/a | |
---|
629 | n/a | class BadCmp(object): |
---|
630 | n/a | def __eq__(self, other): |
---|
631 | n/a | raise Exc() |
---|
632 | n/a | def __hash__(self): |
---|
633 | n/a | return 1 |
---|
634 | n/a | |
---|
635 | n/a | d1 = self._full_mapping({BadCmp(): 1}) |
---|
636 | n/a | d2 = self._full_mapping({1: 1}) |
---|
637 | n/a | self.assertRaises(Exc, lambda: BadCmp()==1) |
---|
638 | n/a | self.assertRaises(Exc, lambda: d1==d2) |
---|
639 | n/a | |
---|
640 | n/a | def test_setdefault(self): |
---|
641 | n/a | TestMappingProtocol.test_setdefault(self) |
---|
642 | n/a | |
---|
643 | n/a | class Exc(Exception): pass |
---|
644 | n/a | |
---|
645 | n/a | class BadHash(object): |
---|
646 | n/a | fail = False |
---|
647 | n/a | def __hash__(self): |
---|
648 | n/a | if self.fail: |
---|
649 | n/a | raise Exc() |
---|
650 | n/a | else: |
---|
651 | n/a | return 42 |
---|
652 | n/a | |
---|
653 | n/a | d = self._empty_mapping() |
---|
654 | n/a | x = BadHash() |
---|
655 | n/a | d[x] = 42 |
---|
656 | n/a | x.fail = True |
---|
657 | n/a | self.assertRaises(Exc, d.setdefault, x, []) |
---|