ยปCore Development>Code coverage>Lib/test/test_userdict.py

Python code coverage for Lib/test/test_userdict.py

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