Kaydet (Commit) 554c8b85 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

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.
üst f01fe691
...@@ -379,10 +379,11 @@ class Mapping(metaclass=ABCMeta): ...@@ -379,10 +379,11 @@ class Mapping(metaclass=ABCMeta):
return ValuesView(self) return ValuesView(self)
def __eq__(self, other): 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): def __ne__(self, other):
return dict(self.items()) != dict(other.items()) return not (self == other)
class MappingView(metaclass=ABCMeta): class MappingView(metaclass=ABCMeta):
......
...@@ -135,6 +135,9 @@ class UserDict(MutableMapping): ...@@ -135,6 +135,9 @@ class UserDict(MutableMapping):
def __iter__(self): def __iter__(self):
return iter(self.data) 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 # Now, add the methods in dicts but not in MutableMapping
def __repr__(self): return repr(self.data) def __repr__(self): return repr(self.data)
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import unittest import unittest
from test import test_support, mapping_tests from test import test_support, mapping_tests
import UserDict import collections
d0 = {} d0 = {}
d1 = {"one": 1} d1 = {"one": 1}
...@@ -12,36 +12,36 @@ d4 = {"one": None, "two": None} ...@@ -12,36 +12,36 @@ d4 = {"one": None, "two": None}
d5 = {"one": 1, "two": 1} d5 = {"one": 1, "two": 1}
class UserDictTest(mapping_tests.TestHashMappingProtocol): class UserDictTest(mapping_tests.TestHashMappingProtocol):
type2test = UserDict.IterableUserDict type2test = collections.UserDict
def test_all(self): def test_all(self):
# Test constructors # Test constructors
u = UserDict.UserDict() u = collections.UserDict()
u0 = UserDict.UserDict(d0) u0 = collections.UserDict(d0)
u1 = UserDict.UserDict(d1) u1 = collections.UserDict(d1)
u2 = UserDict.IterableUserDict(d2) u2 = collections.UserDict(d2)
uu = UserDict.UserDict(u) uu = collections.UserDict(u)
uu0 = UserDict.UserDict(u0) uu0 = collections.UserDict(u0)
uu1 = UserDict.UserDict(u1) uu1 = collections.UserDict(u1)
uu2 = UserDict.UserDict(u2) uu2 = collections.UserDict(u2)
# keyword arg constructor # keyword arg constructor
self.assertEqual(UserDict.UserDict(one=1, two=2), d2) self.assertEqual(collections.UserDict(one=1, two=2), d2)
# item sequence constructor # item sequence constructor
self.assertEqual(UserDict.UserDict([('one',1), ('two',2)]), d2) self.assertEqual(collections.UserDict([('one',1), ('two',2)]), d2)
self.assertEqual(UserDict.UserDict(dict=[('one',1), ('two',2)]), d2) self.assertEqual(collections.UserDict(dict=[('one',1), ('two',2)]), d2)
# both together # 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 # alternate constructor
self.assertEqual(UserDict.UserDict.fromkeys('one two'.split()), d4) self.assertEqual(collections.UserDict.fromkeys('one two'.split()), d4)
self.assertEqual(UserDict.UserDict().fromkeys('one two'.split()), d4) self.assertEqual(collections.UserDict().fromkeys('one two'.split()), d4)
self.assertEqual(UserDict.UserDict.fromkeys('one two'.split(), 1), d5) self.assertEqual(collections.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(), 1), d5)
self.assert_(u1.fromkeys('one two'.split()) is not u1) self.assert_(u1.fromkeys('one two'.split()) is not u1)
self.assert_(isinstance(u1.fromkeys('one two'.split()), UserDict.UserDict)) self.assert_(isinstance(u1.fromkeys('one two'.split()), collections.UserDict))
self.assert_(isinstance(u2.fromkeys('one two'.split()), UserDict.IterableUserDict)) self.assert_(isinstance(u2.fromkeys('one two'.split()), collections.UserDict))
# Test __repr__ # Test __repr__
self.assertEqual(str(u0), str(d0)) self.assertEqual(str(u0), str(d0))
...@@ -59,7 +59,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol): ...@@ -59,7 +59,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertRaises(KeyError, u1.__getitem__, "two") self.assertRaises(KeyError, u1.__getitem__, "two")
# Test __setitem__ # Test __setitem__
u3 = UserDict.UserDict(u2) u3 = collections.UserDict(u2)
u3["two"] = 2 u3["two"] = 2
u3["three"] = 3 u3["three"] = 3
...@@ -74,11 +74,11 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol): ...@@ -74,11 +74,11 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
# Test copy() # Test copy()
u2a = u2.copy() u2a = u2.copy()
self.assertEqual(u2a, u2) 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 u2c = u2b.copy() # making a copy of a UserDict is special cased
self.assertEqual(u2b, u2c) self.assertEqual(u2b, u2c)
class MyUserDict(UserDict.UserDict): class MyUserDict(collections.UserDict):
def display(self): print(self) def display(self): print(self)
m2 = MyUserDict(u2) m2 = MyUserDict(u2)
...@@ -101,15 +101,9 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol): ...@@ -101,15 +101,9 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertEqual(i in u0, i in d0) self.assertEqual(i in u0, i in d0)
# Test update # Test update
t = UserDict.UserDict() t = collections.UserDict()
t.update(u2) t.update(u2)
self.assertEqual(t, 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 # Test get
for i in u2.keys(): for i in u2.keys():
...@@ -127,13 +121,13 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol): ...@@ -127,13 +121,13 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertEqual(set(ikeys), set(keys)) self.assertEqual(set(ikeys), set(keys))
# Test setdefault # Test setdefault
t = UserDict.UserDict() t = collections.UserDict()
self.assertEqual(t.setdefault("x", 42), 42) self.assertEqual(t.setdefault("x", 42), 42)
self.assert_("x" in t) self.assert_("x" in t)
self.assertEqual(t.setdefault("x", 23), 42) self.assertEqual(t.setdefault("x", 23), 42)
# Test pop # Test pop
t = UserDict.UserDict(x=42) t = collections.UserDict(x=42)
self.assertEqual(t.pop("x"), 42) self.assertEqual(t.pop("x"), 42)
self.assertRaises(KeyError, t.pop, "x") self.assertRaises(KeyError, t.pop, "x")
self.assertEqual(t.pop("x", 1), 1) self.assertEqual(t.pop("x", 1), 1)
...@@ -141,19 +135,19 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol): ...@@ -141,19 +135,19 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertEqual(t.pop("x", 1), 42) self.assertEqual(t.pop("x", 1), 42)
# Test popitem # Test popitem
t = UserDict.UserDict(x=42) t = collections.UserDict(x=42)
self.assertEqual(t.popitem(), ("x", 42)) self.assertEqual(t.popitem(), ("x", 42))
self.assertRaises(KeyError, t.popitem) self.assertRaises(KeyError, t.popitem)
def test_missing(self): def test_missing(self):
# Make sure UserDict doesn't have a __missing__ method # 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: # Test several cases:
# (D) subclass defines __missing__ method returning a value # (D) subclass defines __missing__ method returning a value
# (E) subclass defines __missing__ method raising RuntimeError # (E) subclass defines __missing__ method raising RuntimeError
# (F) subclass sets __missing__ instance variable (no effect) # (F) subclass sets __missing__ instance variable (no effect)
# (G) subclass doesn't define __missing__ at a all # (G) subclass doesn't define __missing__ at a all
class D(UserDict.UserDict): class D(collections.UserDict):
def __missing__(self, key): def __missing__(self, key):
return 42 return 42
d = D({1: 2, 3: 4}) d = D({1: 2, 3: 4})
...@@ -162,7 +156,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol): ...@@ -162,7 +156,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assert_(2 not in d) self.assert_(2 not in d)
self.assert_(2 not in d.keys()) self.assert_(2 not in d.keys())
self.assertEqual(d[2], 42) self.assertEqual(d[2], 42)
class E(UserDict.UserDict): class E(collections.UserDict):
def __missing__(self, key): def __missing__(self, key):
raise RuntimeError(key) raise RuntimeError(key)
e = E() e = E()
...@@ -172,11 +166,11 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol): ...@@ -172,11 +166,11 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertEqual(err.args, (42,)) self.assertEqual(err.args, (42,))
else: else:
self.fail("e[42] didn't raise RuntimeError") self.fail("e[42] didn't raise RuntimeError")
class F(UserDict.UserDict): class F(collections.UserDict):
def __init__(self): def __init__(self):
# An instance variable __missing__ should have no effect # An instance variable __missing__ should have no effect
self.__missing__ = lambda key: None self.__missing__ = lambda key: None
UserDict.UserDict.__init__(self) collections.UserDict.__init__(self)
f = F() f = F()
try: try:
f[42] f[42]
...@@ -184,7 +178,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol): ...@@ -184,7 +178,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertEqual(err.args, (42,)) self.assertEqual(err.args, (42,))
else: else:
self.fail("f[42] didn't raise KeyError") self.fail("f[42] didn't raise KeyError")
class G(UserDict.UserDict): class G(collections.UserDict):
pass pass
g = G() g = G()
try: try:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment