summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_userdict.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-02-05 22:54:43 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-02-05 22:54:43 (GMT)
commit554c8b856cbeb29f3fef0350846aef442832acac (patch)
tree7a12918b63e5f35787cb232b36bdbdf0890fff90 /Lib/test/test_userdict.py
parentf01fe6915b329c1511548e99e0e4d34ac423f9b3 (diff)
downloadcpython-554c8b856cbeb29f3fef0350846aef442832acac.zip
cpython-554c8b856cbeb29f3fef0350846aef442832acac.tar.gz
cpython-554c8b856cbeb29f3fef0350846aef442832acac.tar.bz2
Convert test_userdict to use the collections.UserDict.
Fix-up collections.UserDict.__contains__ to work correctly when __missing__ is defined. Fix-up Mapping.__eq__ to only match instances of Mapping.
Diffstat (limited to 'Lib/test/test_userdict.py')
-rw-r--r--Lib/test/test_userdict.py72
1 files changed, 33 insertions, 39 deletions
diff --git a/Lib/test/test_userdict.py b/Lib/test/test_userdict.py
index ac33be7..643caf9 100644
--- a/Lib/test/test_userdict.py
+++ b/Lib/test/test_userdict.py
@@ -2,7 +2,7 @@
import unittest
from test import test_support, mapping_tests
-import UserDict
+import collections
d0 = {}
d1 = {"one": 1}
@@ -12,36 +12,36 @@ d4 = {"one": None, "two": None}
d5 = {"one": 1, "two": 1}
class UserDictTest(mapping_tests.TestHashMappingProtocol):
- type2test = UserDict.IterableUserDict
+ type2test = collections.UserDict
def test_all(self):
# Test constructors
- u = UserDict.UserDict()
- u0 = UserDict.UserDict(d0)
- u1 = UserDict.UserDict(d1)
- u2 = UserDict.IterableUserDict(d2)
+ u = collections.UserDict()
+ u0 = collections.UserDict(d0)
+ u1 = collections.UserDict(d1)
+ u2 = collections.UserDict(d2)
- uu = UserDict.UserDict(u)
- uu0 = UserDict.UserDict(u0)
- uu1 = UserDict.UserDict(u1)
- uu2 = UserDict.UserDict(u2)
+ uu = collections.UserDict(u)
+ uu0 = collections.UserDict(u0)
+ uu1 = collections.UserDict(u1)
+ uu2 = collections.UserDict(u2)
# keyword arg constructor
- self.assertEqual(UserDict.UserDict(one=1, two=2), d2)
+ self.assertEqual(collections.UserDict(one=1, two=2), d2)
# item sequence constructor
- self.assertEqual(UserDict.UserDict([('one',1), ('two',2)]), d2)
- self.assertEqual(UserDict.UserDict(dict=[('one',1), ('two',2)]), d2)
+ self.assertEqual(collections.UserDict([('one',1), ('two',2)]), d2)
+ self.assertEqual(collections.UserDict(dict=[('one',1), ('two',2)]), d2)
# both together
- self.assertEqual(UserDict.UserDict([('one',1), ('two',2)], two=3, three=5), d3)
+ self.assertEqual(collections.UserDict([('one',1), ('two',2)], two=3, three=5), d3)
# alternate constructor
- self.assertEqual(UserDict.UserDict.fromkeys('one two'.split()), d4)
- self.assertEqual(UserDict.UserDict().fromkeys('one two'.split()), d4)
- self.assertEqual(UserDict.UserDict.fromkeys('one two'.split(), 1), d5)
- self.assertEqual(UserDict.UserDict().fromkeys('one two'.split(), 1), d5)
+ self.assertEqual(collections.UserDict.fromkeys('one two'.split()), d4)
+ self.assertEqual(collections.UserDict().fromkeys('one two'.split()), d4)
+ self.assertEqual(collections.UserDict.fromkeys('one two'.split(), 1), d5)
+ self.assertEqual(collections.UserDict().fromkeys('one two'.split(), 1), d5)
self.assert_(u1.fromkeys('one two'.split()) is not u1)
- self.assert_(isinstance(u1.fromkeys('one two'.split()), UserDict.UserDict))
- self.assert_(isinstance(u2.fromkeys('one two'.split()), UserDict.IterableUserDict))
+ self.assert_(isinstance(u1.fromkeys('one two'.split()), collections.UserDict))
+ self.assert_(isinstance(u2.fromkeys('one two'.split()), collections.UserDict))
# Test __repr__
self.assertEqual(str(u0), str(d0))
@@ -59,7 +59,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertRaises(KeyError, u1.__getitem__, "two")
# Test __setitem__
- u3 = UserDict.UserDict(u2)
+ u3 = collections.UserDict(u2)
u3["two"] = 2
u3["three"] = 3
@@ -74,11 +74,11 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
# Test copy()
u2a = u2.copy()
self.assertEqual(u2a, u2)
- u2b = UserDict.UserDict(x=42, y=23)
+ u2b = collections.UserDict(x=42, y=23)
u2c = u2b.copy() # making a copy of a UserDict is special cased
self.assertEqual(u2b, u2c)
- class MyUserDict(UserDict.UserDict):
+ class MyUserDict(collections.UserDict):
def display(self): print(self)
m2 = MyUserDict(u2)
@@ -101,15 +101,9 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertEqual(i in u0, i in d0)
# Test update
- t = UserDict.UserDict()
+ t = collections.UserDict()
t.update(u2)
self.assertEqual(t, u2)
- class Items:
- def items(self):
- return (("x", 42), ("y", 23))
- t = UserDict.UserDict()
- t.update(Items())
- self.assertEqual(t, {"x": 42, "y": 23})
# Test get
for i in u2.keys():
@@ -127,13 +121,13 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertEqual(set(ikeys), set(keys))
# Test setdefault
- t = UserDict.UserDict()
+ t = collections.UserDict()
self.assertEqual(t.setdefault("x", 42), 42)
self.assert_("x" in t)
self.assertEqual(t.setdefault("x", 23), 42)
# Test pop
- t = UserDict.UserDict(x=42)
+ t = collections.UserDict(x=42)
self.assertEqual(t.pop("x"), 42)
self.assertRaises(KeyError, t.pop, "x")
self.assertEqual(t.pop("x", 1), 1)
@@ -141,19 +135,19 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertEqual(t.pop("x", 1), 42)
# Test popitem
- t = UserDict.UserDict(x=42)
+ t = collections.UserDict(x=42)
self.assertEqual(t.popitem(), ("x", 42))
self.assertRaises(KeyError, t.popitem)
def test_missing(self):
# Make sure UserDict doesn't have a __missing__ method
- self.assertEqual(hasattr(UserDict, "__missing__"), False)
+ self.assertEqual(hasattr(collections.UserDict, "__missing__"), False)
# Test several cases:
# (D) subclass defines __missing__ method returning a value
# (E) subclass defines __missing__ method raising RuntimeError
# (F) subclass sets __missing__ instance variable (no effect)
# (G) subclass doesn't define __missing__ at a all
- class D(UserDict.UserDict):
+ class D(collections.UserDict):
def __missing__(self, key):
return 42
d = D({1: 2, 3: 4})
@@ -162,7 +156,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assert_(2 not in d)
self.assert_(2 not in d.keys())
self.assertEqual(d[2], 42)
- class E(UserDict.UserDict):
+ class E(collections.UserDict):
def __missing__(self, key):
raise RuntimeError(key)
e = E()
@@ -172,11 +166,11 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertEqual(err.args, (42,))
else:
self.fail("e[42] didn't raise RuntimeError")
- class F(UserDict.UserDict):
+ class F(collections.UserDict):
def __init__(self):
# An instance variable __missing__ should have no effect
self.__missing__ = lambda key: None
- UserDict.UserDict.__init__(self)
+ collections.UserDict.__init__(self)
f = F()
try:
f[42]
@@ -184,7 +178,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertEqual(err.args, (42,))
else:
self.fail("f[42] didn't raise KeyError")
- class G(UserDict.UserDict):
+ class G(collections.UserDict):
pass
g = G()
try: