diff options
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_collections.py | 45 | ||||
-rw-r--r-- | Lib/test/test_weakref.py | 37 |
2 files changed, 82 insertions, 0 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 92520b09bb..47c500bcdd 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -232,6 +232,51 @@ class TestChainMap(unittest.TestCase): for k, v in dict(a=1, B=20, C=30, z=100).items(): # check get self.assertEqual(d.get(k, 100), v) + def test_union_operators(self): + cm1 = ChainMap(dict(a=1, b=2), dict(c=3, d=4)) + cm2 = ChainMap(dict(a=10, e=5), dict(b=20, d=4)) + cm3 = cm1.copy() + d = dict(a=10, c=30) + pairs = [('c', 3), ('p',0)] + + tmp = cm1 | cm2 # testing between chainmaps + self.assertEqual(tmp.maps, [cm1.maps[0] | dict(cm2), *cm1.maps[1:]]) + cm1 |= cm2 + self.assertEqual(tmp, cm1) + + tmp = cm2 | d # testing between chainmap and mapping + self.assertEqual(tmp.maps, [cm2.maps[0] | d, *cm2.maps[1:]]) + self.assertEqual((d | cm2).maps, [d | dict(cm2)]) + cm2 |= d + self.assertEqual(tmp, cm2) + + # testing behavior between chainmap and iterable key-value pairs + with self.assertRaises(TypeError): + cm3 | pairs + cm3 |= pairs + self.assertEqual(cm3.maps, [cm3.maps[0] | dict(pairs), *cm3.maps[1:]]) + + # testing proper return types for ChainMap and it's subclasses + class Subclass(ChainMap): + pass + + class SubclassRor(ChainMap): + def __ror__(self, other): + return super().__ror__(other) + + tmp = ChainMap() | ChainMap() + self.assertIs(type(tmp), ChainMap) + self.assertIs(type(tmp.maps[0]), dict) + tmp = ChainMap() | Subclass() + self.assertIs(type(tmp), ChainMap) + self.assertIs(type(tmp.maps[0]), dict) + tmp = Subclass() | ChainMap() + self.assertIs(type(tmp), Subclass) + self.assertIs(type(tmp.maps[0]), dict) + tmp = ChainMap() | SubclassRor() + self.assertIs(type(tmp), SubclassRor) + self.assertIs(type(tmp.maps[0]), dict) + ################################################################################ ### Named Tuples diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index 63c725527d..250ed40b20 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -1624,6 +1624,43 @@ class MappingTestCase(TestBase): self.assertEqual(len(d), 1) self.assertEqual(list(d.keys()), [o2]) + def test_weak_keyed_union_operators(self): + o1 = C() + o2 = C() + o3 = C() + wkd1 = weakref.WeakKeyDictionary({o1: 1, o2: 2}) + wkd2 = weakref.WeakKeyDictionary({o3: 3, o1: 4}) + wkd3 = wkd1.copy() + d1 = {o2: '5', o3: '6'} + pairs = [(o2, 7), (o3, 8)] + + tmp1 = wkd1 | wkd2 # Between two WeakKeyDictionaries + self.assertEqual(dict(tmp1), dict(wkd1) | dict(wkd2)) + self.assertIs(type(tmp1), weakref.WeakKeyDictionary) + wkd1 |= wkd2 + self.assertEqual(wkd1, tmp1) + + tmp2 = wkd2 | d1 # Between WeakKeyDictionary and mapping + self.assertEqual(dict(tmp2), dict(wkd2) | d1) + self.assertIs(type(tmp2), weakref.WeakKeyDictionary) + wkd2 |= d1 + self.assertEqual(wkd2, tmp2) + + tmp3 = wkd3.copy() # Between WeakKeyDictionary and iterable key, value + tmp3 |= pairs + self.assertEqual(dict(tmp3), dict(wkd3) | dict(pairs)) + self.assertIs(type(tmp3), weakref.WeakKeyDictionary) + + tmp4 = d1 | wkd3 # Testing .__ror__ + self.assertEqual(dict(tmp4), d1 | dict(wkd3)) + self.assertIs(type(tmp4), weakref.WeakKeyDictionary) + + del o1 + self.assertNotIn(4, tmp1.values()) + self.assertNotIn(4, tmp2.values()) + self.assertNotIn(1, tmp3.values()) + self.assertNotIn(1, tmp4.values()) + def test_weak_valued_delitem(self): d = weakref.WeakValueDictionary() o1 = Object('1') |