Skip to content

Commit ae00fef

Browse files
committed
Add test_userdict from CPython 3.8
1 parent 3e1f330 commit ae00fef

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed

Lib/test/test_userdict.py

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
# Check every path through every method of UserDict
2+
3+
from test import mapping_tests
4+
import unittest
5+
import collections
6+
7+
d0 = {}
8+
d1 = {"one": 1}
9+
d2 = {"one": 1, "two": 2}
10+
d3 = {"one": 1, "two": 3, "three": 5}
11+
d4 = {"one": None, "two": None}
12+
d5 = {"one": 1, "two": 1}
13+
14+
class UserDictTest(mapping_tests.TestHashMappingProtocol):
15+
type2test = collections.UserDict
16+
17+
def test_all(self):
18+
# Test constructors
19+
u = collections.UserDict()
20+
u0 = collections.UserDict(d0)
21+
u1 = collections.UserDict(d1)
22+
u2 = collections.UserDict(d2)
23+
24+
uu = collections.UserDict(u)
25+
uu0 = collections.UserDict(u0)
26+
uu1 = collections.UserDict(u1)
27+
uu2 = collections.UserDict(u2)
28+
29+
# keyword arg constructor
30+
self.assertEqual(collections.UserDict(one=1, two=2), d2)
31+
# item sequence constructor
32+
self.assertEqual(collections.UserDict([('one',1), ('two',2)]), d2)
33+
with self.assertWarnsRegex(DeprecationWarning, "'dict'"):
34+
self.assertEqual(collections.UserDict(dict=[('one',1), ('two',2)]), d2)
35+
# both together
36+
self.assertEqual(collections.UserDict([('one',1), ('two',2)], two=3, three=5), d3)
37+
38+
# alternate constructor
39+
self.assertEqual(collections.UserDict.fromkeys('one two'.split()), d4)
40+
self.assertEqual(collections.UserDict().fromkeys('one two'.split()), d4)
41+
self.assertEqual(collections.UserDict.fromkeys('one two'.split(), 1), d5)
42+
self.assertEqual(collections.UserDict().fromkeys('one two'.split(), 1), d5)
43+
self.assertTrue(u1.fromkeys('one two'.split()) is not u1)
44+
self.assertIsInstance(u1.fromkeys('one two'.split()), collections.UserDict)
45+
self.assertIsInstance(u2.fromkeys('one two'.split()), collections.UserDict)
46+
47+
# Test __repr__
48+
self.assertEqual(str(u0), str(d0))
49+
self.assertEqual(repr(u1), repr(d1))
50+
self.assertIn(repr(u2), ("{'one': 1, 'two': 2}",
51+
"{'two': 2, 'one': 1}"))
52+
53+
# Test rich comparison and __len__
54+
all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2]
55+
for a in all:
56+
for b in all:
57+
self.assertEqual(a == b, len(a) == len(b))
58+
59+
# Test __getitem__
60+
self.assertEqual(u2["one"], 1)
61+
self.assertRaises(KeyError, u1.__getitem__, "two")
62+
63+
# Test __setitem__
64+
u3 = collections.UserDict(u2)
65+
u3["two"] = 2
66+
u3["three"] = 3
67+
68+
# Test __delitem__
69+
del u3["three"]
70+
self.assertRaises(KeyError, u3.__delitem__, "three")
71+
72+
# Test clear
73+
u3.clear()
74+
self.assertEqual(u3, {})
75+
76+
# Test copy()
77+
u2a = u2.copy()
78+
self.assertEqual(u2a, u2)
79+
u2b = collections.UserDict(x=42, y=23)
80+
u2c = u2b.copy() # making a copy of a UserDict is special cased
81+
self.assertEqual(u2b, u2c)
82+
83+
class MyUserDict(collections.UserDict):
84+
def display(self): print(self)
85+
86+
m2 = MyUserDict(u2)
87+
m2a = m2.copy()
88+
self.assertEqual(m2a, m2)
89+
90+
# SF bug #476616 -- copy() of UserDict subclass shared data
91+
m2['foo'] = 'bar'
92+
self.assertNotEqual(m2a, m2)
93+
94+
# Test keys, items, values
95+
self.assertEqual(sorted(u2.keys()), sorted(d2.keys()))
96+
self.assertEqual(sorted(u2.items()), sorted(d2.items()))
97+
self.assertEqual(sorted(u2.values()), sorted(d2.values()))
98+
99+
# Test "in".
100+
for i in u2.keys():
101+
self.assertIn(i, u2)
102+
self.assertEqual(i in u1, i in d1)
103+
self.assertEqual(i in u0, i in d0)
104+
105+
# Test update
106+
t = collections.UserDict()
107+
t.update(u2)
108+
self.assertEqual(t, u2)
109+
110+
# Test get
111+
for i in u2.keys():
112+
self.assertEqual(u2.get(i), u2[i])
113+
self.assertEqual(u1.get(i), d1.get(i))
114+
self.assertEqual(u0.get(i), d0.get(i))
115+
116+
# Test "in" iteration.
117+
for i in range(20):
118+
u2[i] = str(i)
119+
ikeys = []
120+
for k in u2:
121+
ikeys.append(k)
122+
keys = u2.keys()
123+
self.assertEqual(set(ikeys), set(keys))
124+
125+
# Test setdefault
126+
t = collections.UserDict()
127+
self.assertEqual(t.setdefault("x", 42), 42)
128+
self.assertIn("x", t)
129+
self.assertEqual(t.setdefault("x", 23), 42)
130+
131+
# Test pop
132+
t = collections.UserDict(x=42)
133+
self.assertEqual(t.pop("x"), 42)
134+
self.assertRaises(KeyError, t.pop, "x")
135+
self.assertEqual(t.pop("x", 1), 1)
136+
t["x"] = 42
137+
self.assertEqual(t.pop("x", 1), 42)
138+
139+
# Test popitem
140+
t = collections.UserDict(x=42)
141+
self.assertEqual(t.popitem(), ("x", 42))
142+
self.assertRaises(KeyError, t.popitem)
143+
144+
def test_init(self):
145+
for kw in 'self', 'other', 'iterable':
146+
self.assertEqual(list(collections.UserDict(**{kw: 42}).items()),
147+
[(kw, 42)])
148+
self.assertEqual(list(collections.UserDict({}, dict=42).items()),
149+
[('dict', 42)])
150+
self.assertEqual(list(collections.UserDict({}, dict=None).items()),
151+
[('dict', None)])
152+
with self.assertWarnsRegex(DeprecationWarning, "'dict'"):
153+
self.assertEqual(list(collections.UserDict(dict={'a': 42}).items()),
154+
[('a', 42)])
155+
self.assertRaises(TypeError, collections.UserDict, 42)
156+
self.assertRaises(TypeError, collections.UserDict, (), ())
157+
self.assertRaises(TypeError, collections.UserDict.__init__)
158+
159+
def test_update(self):
160+
for kw in 'self', 'dict', 'other', 'iterable':
161+
d = collections.UserDict()
162+
d.update(**{kw: 42})
163+
self.assertEqual(list(d.items()), [(kw, 42)])
164+
self.assertRaises(TypeError, collections.UserDict().update, 42)
165+
self.assertRaises(TypeError, collections.UserDict().update, {}, {})
166+
self.assertRaises(TypeError, collections.UserDict.update)
167+
168+
def test_missing(self):
169+
# Make sure UserDict doesn't have a __missing__ method
170+
self.assertEqual(hasattr(collections.UserDict, "__missing__"), False)
171+
# Test several cases:
172+
# (D) subclass defines __missing__ method returning a value
173+
# (E) subclass defines __missing__ method raising RuntimeError
174+
# (F) subclass sets __missing__ instance variable (no effect)
175+
# (G) subclass doesn't define __missing__ at all
176+
class D(collections.UserDict):
177+
def __missing__(self, key):
178+
return 42
179+
d = D({1: 2, 3: 4})
180+
self.assertEqual(d[1], 2)
181+
self.assertEqual(d[3], 4)
182+
self.assertNotIn(2, d)
183+
self.assertNotIn(2, d.keys())
184+
self.assertEqual(d[2], 42)
185+
class E(collections.UserDict):
186+
def __missing__(self, key):
187+
raise RuntimeError(key)
188+
e = E()
189+
try:
190+
e[42]
191+
except RuntimeError as err:
192+
self.assertEqual(err.args, (42,))
193+
else:
194+
self.fail("e[42] didn't raise RuntimeError")
195+
class F(collections.UserDict):
196+
def __init__(self):
197+
# An instance variable __missing__ should have no effect
198+
self.__missing__ = lambda key: None
199+
collections.UserDict.__init__(self)
200+
f = F()
201+
try:
202+
f[42]
203+
except KeyError as err:
204+
self.assertEqual(err.args, (42,))
205+
else:
206+
self.fail("f[42] didn't raise KeyError")
207+
class G(collections.UserDict):
208+
pass
209+
g = G()
210+
try:
211+
g[42]
212+
except KeyError as err:
213+
self.assertEqual(err.args, (42,))
214+
else:
215+
self.fail("g[42] didn't raise KeyError")
216+
217+
218+
219+
if __name__ == "__main__":
220+
unittest.main()

0 commit comments

Comments
 (0)