diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-11-27 14:35:26 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-11-27 14:35:26 (GMT) |
commit | 8943ecfab20168f2b18bc477efb7671e32e91c24 (patch) | |
tree | 0a1b17968ff724a5b67e7bf85782c4a417a4ec10 /Lib/test/test_collections.py | |
parent | f25e3bfefa800c20c354843b345644ac80b646a8 (diff) | |
parent | ae5cb214d2cd41d96943a0ef43a4e95bd9a10b7a (diff) | |
download | cpython-8943ecfab20168f2b18bc477efb7671e32e91c24.zip cpython-8943ecfab20168f2b18bc477efb7671e32e91c24.tar.gz cpython-8943ecfab20168f2b18bc477efb7671e32e91c24.tar.bz2 |
Issue #22609: Constructors and update methods of mapping classes in the
collections module now accept the self keyword argument.
Diffstat (limited to 'Lib/test/test_collections.py')
-rw-r--r-- | Lib/test/test_collections.py | 64 |
1 files changed, 61 insertions, 3 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 7b46475..44cbe4f 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -1137,6 +1137,28 @@ class TestCounter(unittest.TestCase): self.assertEqual(c.setdefault('e', 5), 5) self.assertEqual(c['e'], 5) + def test_init(self): + self.assertEqual(list(Counter(self=42).items()), [('self', 42)]) + self.assertEqual(list(Counter(iterable=42).items()), [('iterable', 42)]) + self.assertEqual(list(Counter(iterable=None).items()), [('iterable', None)]) + self.assertRaises(TypeError, Counter, 42) + self.assertRaises(TypeError, Counter, (), ()) + self.assertRaises(TypeError, Counter.__init__) + + def test_update(self): + c = Counter() + c.update(self=42) + self.assertEqual(list(c.items()), [('self', 42)]) + c = Counter() + c.update(iterable=42) + self.assertEqual(list(c.items()), [('iterable', 42)]) + c = Counter() + c.update(iterable=None) + self.assertEqual(list(c.items()), [('iterable', None)]) + self.assertRaises(TypeError, Counter().update, 42) + self.assertRaises(TypeError, Counter().update, {}, {}) + self.assertRaises(TypeError, Counter.update) + def test_copying(self): # Check that counters are copyable, deepcopyable, picklable, and #have a repr/eval round-trip @@ -1258,6 +1280,16 @@ class TestCounter(unittest.TestCase): c.subtract('aaaabbcce') self.assertEqual(c, Counter(a=-1, b=0, c=-1, d=1, e=-1)) + c = Counter() + c.subtract(self=42) + self.assertEqual(list(c.items()), [('self', -42)]) + c = Counter() + c.subtract(iterable=42) + self.assertEqual(list(c.items()), [('iterable', -42)]) + self.assertRaises(TypeError, Counter().subtract, 42) + self.assertRaises(TypeError, Counter().subtract, {}, {}) + self.assertRaises(TypeError, Counter.subtract) + def test_unary(self): c = Counter(a=-5, b=0, c=5, d=10, e=15,g=40) self.assertEqual(dict(+c), dict(c=5, d=10, e=15, g=40)) @@ -1308,8 +1340,11 @@ class TestOrderedDict(unittest.TestCase): c=3, e=5).items()), pairs) # mixed input # make sure no positional args conflict with possible kwdargs - self.assertEqual(inspect.getargspec(OrderedDict.__dict__['__init__']).args, - ['self']) + self.assertEqual(list(OrderedDict(self=42).items()), [('self', 42)]) + self.assertEqual(list(OrderedDict(other=42).items()), [('other', 42)]) + self.assertRaises(TypeError, OrderedDict, 42) + self.assertRaises(TypeError, OrderedDict, (), ()) + self.assertRaises(TypeError, OrderedDict.__init__) # Make sure that direct calls to __init__ do not clear previous contents d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)]) @@ -1354,6 +1389,10 @@ class TestOrderedDict(unittest.TestCase): self.assertEqual(list(d.items()), [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)]) + self.assertRaises(TypeError, OrderedDict().update, 42) + self.assertRaises(TypeError, OrderedDict().update, (), ()) + self.assertRaises(TypeError, OrderedDict.update) + def test_abc(self): self.assertIsInstance(OrderedDict(), MutableMapping) self.assertTrue(issubclass(OrderedDict, MutableMapping)) @@ -1600,6 +1639,24 @@ class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol): d = self._empty_mapping() self.assertRaises(KeyError, d.popitem) +class TestUserDict(unittest.TestCase): + + def test_init(self): + self.assertEqual(list(UserDict(self=42).items()), [('self', 42)]) + self.assertEqual(list(UserDict(dict=42).items()), [('dict', 42)]) + self.assertEqual(list(UserDict(dict=None).items()), [('dict', None)]) + self.assertRaises(TypeError, UserDict, 42) + self.assertRaises(TypeError, UserDict, (), ()) + self.assertRaises(TypeError, UserDict.__init__) + + def test_update(self): + d = UserDict() + d.update(self=42) + self.assertEqual(list(d.items()), [('self', 42)]) + self.assertRaises(TypeError, UserDict().update, 42) + self.assertRaises(TypeError, UserDict().update, {}, {}) + self.assertRaises(TypeError, UserDict.update) + ################################################################################ ### Run tests @@ -1611,7 +1668,8 @@ def test_main(verbose=None): NamedTupleDocs = doctest.DocTestSuite(module=collections) test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs, TestCollectionABCs, TestCounter, TestChainMap, - TestOrderedDict, GeneralMappingTests, SubclassMappingTests] + TestOrderedDict, GeneralMappingTests, SubclassMappingTests, + TestUserDict,] support.run_unittest(*test_classes) support.run_doctest(collections, verbose) |