summaryrefslogtreecommitdiff
path: root/Lib/test/test_collections.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-01-04 20:57:19 +0000
committerRaymond Hettinger <python@rcn.com>2011-01-04 20:57:19 +0000
commitd194d5916d0b9ef054ca62e19cf0df6ac4ae26f2 (patch)
tree28ac0fd27aced99b75ae7e2627cf42f1a9730414 /Lib/test/test_collections.py
parentb184b43e03aa0bf34a1245cbfd1cfba54b10e335 (diff)
downloadcpython-d194d5916d0b9ef054ca62e19cf0df6ac4ae26f2.tar.gz
Backport r87613 to make OrderedDict subclassing match dict subclassing.
Diffstat (limited to 'Lib/test/test_collections.py')
-rw-r--r--Lib/test/test_collections.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index 988db92e16..8989ac35c8 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -792,6 +792,10 @@ class TestOrderedDict(unittest.TestCase):
self.assertEqual(list(d.items()),
[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])
+ def test_abc(self):
+ self.assertTrue(isinstance(OrderedDict(), MutableMapping))
+ self.assertTrue(issubclass(OrderedDict, MutableMapping))
+
def test_clear(self):
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
shuffle(pairs)
@@ -850,6 +854,17 @@ class TestOrderedDict(unittest.TestCase):
self.assertEqual(len(od), 0)
self.assertEqual(od.pop(k, 12345), 12345)
+ # make sure pop still works when __missing__ is defined
+ class Missing(OrderedDict):
+ def __missing__(self, key):
+ return 0
+ m = Missing(a=1)
+ self.assertEqual(m.pop('b', 5), 5)
+ self.assertEqual(m.pop('a', 6), 1)
+ self.assertEqual(m.pop('a', 6), 6)
+ with self.assertRaises(KeyError):
+ m.pop('a')
+
def test_equality(self):
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
shuffle(pairs)
@@ -934,6 +949,12 @@ class TestOrderedDict(unittest.TestCase):
# make sure 'x' is added to the end
self.assertEqual(list(od.items())[-1], ('x', 10))
+ # make sure setdefault still works when __missing__ is defined
+ class Missing(OrderedDict):
+ def __missing__(self, key):
+ return 0
+ self.assertEqual(Missing().setdefault(5, 9), 9)
+
def test_reinsert(self):
# Given insert a, insert b, delete a, re-insert a,
# verify that a is now later than b.
@@ -945,6 +966,13 @@ class TestOrderedDict(unittest.TestCase):
self.assertEqual(list(od.items()), [('b', 2), ('a', 1)])
+ def test_override_update(self):
+ # Verify that subclasses can override update() without breaking __init__()
+ class MyOD(OrderedDict):
+ def update(self, *args, **kwds):
+ raise Exception()
+ items = [('a', 1), ('c', 3), ('b', 2)]
+ self.assertEqual(list(MyOD(items).items()), items)
class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):
type2test = OrderedDict