MCPcopy Index your code
hub / github.com/python/cpython / test_union_operators

Method test_union_operators

Lib/test/test_collections.py:266–310  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

264 self.assertEqual(d.maps, [{'b': 20, 'c': 30}, {'a': 1, 'b': 2}])
265
266 def test_union_operators(self):
267 cm1 = ChainMap(dict(a=1, b=2), dict(c=3, d=4))
268 cm2 = ChainMap(dict(a=10, e=5), dict(b=20, d=4))
269 cm3 = cm1.copy()
270 d = dict(a=10, c=30)
271 pairs = [('c', 3), ('p',0)]
272
273 tmp = cm1 | cm2 # testing between chainmaps
274 self.assertEqual(tmp.maps, [cm1.maps[0] | dict(cm2), *cm1.maps[1:]])
275 cm1 |= cm2
276 self.assertEqual(tmp, cm1)
277
278 tmp = cm2 | d # testing between chainmap and mapping
279 self.assertEqual(tmp.maps, [cm2.maps[0] | d, *cm2.maps[1:]])
280 self.assertEqual((d | cm2).maps, [d | dict(cm2)])
281 cm2 |= d
282 self.assertEqual(tmp, cm2)
283
284 # testing behavior between chainmap and iterable key-value pairs
285 with self.assertRaises(TypeError):
286 cm3 | pairs
287 tmp = cm3.copy()
288 cm3 |= pairs
289 self.assertEqual(cm3.maps, [tmp.maps[0] | dict(pairs), *tmp.maps[1:]])
290
291 # testing proper return types for ChainMap and it's subclasses
292 class Subclass(ChainMap):
293 pass
294
295 class SubclassRor(ChainMap):
296 def __ror__(self, other):
297 return super().__ror__(other)
298
299 tmp = ChainMap() | ChainMap()
300 self.assertIs(type(tmp), ChainMap)
301 self.assertIs(type(tmp.maps[0]), dict)
302 tmp = ChainMap() | Subclass()
303 self.assertIs(type(tmp), ChainMap)
304 self.assertIs(type(tmp.maps[0]), dict)
305 tmp = Subclass() | ChainMap()
306 self.assertIs(type(tmp), Subclass)
307 self.assertIs(type(tmp.maps[0]), dict)
308 tmp = ChainMap() | SubclassRor()
309 self.assertIs(type(tmp), SubclassRor)
310 self.assertIs(type(tmp.maps[0]), dict)
311
312
313################################################################################

Callers

nothing calls this directly

Calls 8

copyMethod · 0.95
ChainMapClass · 0.90
SubclassRorClass · 0.85
SubclassClass · 0.70
assertEqualMethod · 0.45
assertRaisesMethod · 0.45
copyMethod · 0.45
assertIsMethod · 0.45

Tested by

no test coverage detected