@@ -309,6 +309,42 @@ def __ror__(self, other):
309309 self .assertIs (type (tmp ), SubclassRor )
310310 self .assertIs (type (tmp .maps [0 ]), dict )
311311
312+ def test_reverse_union_with_counter (self ):
313+ left = {'shared' : 99 , 'left_only' : 3 }
314+ front = Counter (front = 2 )
315+ back = {'shadowed' : 5 , 'shared' : 7 , 'back' : 9 }
316+ cm = ChainMap (front , back )
317+
318+ self .assertEqual (cm ['shadowed' ], 0 )
319+ result = left | cm
320+ self .assertEqual (list (result .items ()), [
321+ ('shared' , 0 ), ('left_only' , 3 ), ('shadowed' , 0 ),
322+ ('back' , 0 ), ('front' , 2 ),
323+ ])
324+ self .assertEqual (left , {'shared' : 99 , 'left_only' : 3 })
325+ self .assertEqual (dict (front ), {'front' : 2 })
326+ self .assertEqual (back , {'shadowed' : 5 , 'shared' : 7 , 'back' : 9 })
327+
328+ def test_reverse_union_with_defaultdict (self ):
329+ left = {'shared' : 99 , 'left_only' : 3 }
330+ front = collections .defaultdict (lambda : - 1 , {'front' : 2 })
331+ back = {'shadowed' : 5 , 'shared' : 7 }
332+ cm = ChainMap (front , back )
333+
334+ fresh_front = collections .defaultdict (lambda : - 1 , {'front' : 2 })
335+ fresh = ChainMap (fresh_front , {'shadowed' : 5 , 'shared' : 7 })
336+ self .assertEqual (fresh ['shadowed' ], - 1 )
337+ self .assertEqual (fresh ['shared' ], - 1 )
338+ self .assertEqual (fresh_front , {'front' : 2 , 'shadowed' : - 1 , 'shared' : - 1 })
339+
340+ result = left | cm
341+ self .assertEqual (list (result .items ()), [
342+ ('shared' , - 1 ), ('left_only' , 3 ), ('shadowed' , - 1 ), ('front' , 2 ),
343+ ])
344+ self .assertEqual (left , {'shared' : 99 , 'left_only' : 3 })
345+ self .assertEqual (front , {'front' : 2 , 'shadowed' : - 1 , 'shared' : - 1 })
346+ self .assertEqual (back , {'shadowed' : 5 , 'shared' : 7 })
347+
312348
313349################################################################################
314350### Named Tuples
0 commit comments