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.
This commit is contained in:
Raymond Hettinger 2008-02-05 22:54:43 +00:00
parent f01fe6915b
commit 554c8b856c
3 changed files with 39 additions and 41 deletions

View File

@ -379,10 +379,11 @@ class Mapping(metaclass=ABCMeta):
return ValuesView(self)
def __eq__(self, other):
return dict(self.items()) == dict(other.items())
return isinstance(other, Mapping) and \
dict(self.items()) == dict(other.items())
def __ne__(self, other):
return dict(self.items()) != dict(other.items())
return not (self == other)
class MappingView(metaclass=ABCMeta):

View File

@ -135,6 +135,9 @@ class UserDict(MutableMapping):
def __iter__(self):
return iter(self.data)
# Modify __contains__ to work correctly when __missing__ is present
def __contains__(self, key):
return key in self.data
# Now, add the methods in dicts but not in MutableMapping
def __repr__(self): return repr(self.data)

View File

@ -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: