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

Python code coverage for Lib/test/test_contains.py

#countcontent
1n/afrom collections import deque
2n/aimport unittest
3n/a
4n/a
5n/aclass base_set:
6n/a def __init__(self, el):
7n/a self.el = el
8n/a
9n/aclass myset(base_set):
10n/a def __contains__(self, el):
11n/a return self.el == el
12n/a
13n/aclass seq(base_set):
14n/a def __getitem__(self, n):
15n/a return [self.el][n]
16n/a
17n/aclass TestContains(unittest.TestCase):
18n/a def test_common_tests(self):
19n/a a = base_set(1)
20n/a b = myset(1)
21n/a c = seq(1)
22n/a self.assertIn(1, b)
23n/a self.assertNotIn(0, b)
24n/a self.assertIn(1, c)
25n/a self.assertNotIn(0, c)
26n/a self.assertRaises(TypeError, lambda: 1 in a)
27n/a self.assertRaises(TypeError, lambda: 1 not in a)
28n/a
29n/a # test char in string
30n/a self.assertIn('c', 'abc')
31n/a self.assertNotIn('d', 'abc')
32n/a
33n/a self.assertIn('', '')
34n/a self.assertIn('', 'abc')
35n/a
36n/a self.assertRaises(TypeError, lambda: None in 'abc')
37n/a
38n/a def test_builtin_sequence_types(self):
39n/a # a collection of tests on builtin sequence types
40n/a a = range(10)
41n/a for i in a:
42n/a self.assertIn(i, a)
43n/a self.assertNotIn(16, a)
44n/a self.assertNotIn(a, a)
45n/a
46n/a a = tuple(a)
47n/a for i in a:
48n/a self.assertIn(i, a)
49n/a self.assertNotIn(16, a)
50n/a self.assertNotIn(a, a)
51n/a
52n/a class Deviant1:
53n/a """Behaves strangely when compared
54n/a
55n/a This class is designed to make sure that the contains code
56n/a works when the list is modified during the check.
57n/a """
58n/a aList = list(range(15))
59n/a def __eq__(self, other):
60n/a if other == 12:
61n/a self.aList.remove(12)
62n/a self.aList.remove(13)
63n/a self.aList.remove(14)
64n/a return 0
65n/a
66n/a self.assertNotIn(Deviant1(), Deviant1.aList)
67n/a
68n/a def test_nonreflexive(self):
69n/a # containment and equality tests involving elements that are
70n/a # not necessarily equal to themselves
71n/a
72n/a class MyNonReflexive(object):
73n/a def __eq__(self, other):
74n/a return False
75n/a def __hash__(self):
76n/a return 28
77n/a
78n/a values = float('nan'), 1, None, 'abc', MyNonReflexive()
79n/a constructors = list, tuple, dict.fromkeys, set, frozenset, deque
80n/a for constructor in constructors:
81n/a container = constructor(values)
82n/a for elem in container:
83n/a self.assertIn(elem, container)
84n/a self.assertTrue(container == constructor(values))
85n/a self.assertTrue(container == container)
86n/a
87n/a def test_block_fallback(self):
88n/a # blocking fallback with __contains__ = None
89n/a class ByContains(object):
90n/a def __contains__(self, other):
91n/a return False
92n/a c = ByContains()
93n/a class BlockContains(ByContains):
94n/a """Is not a container
95n/a
96n/a This class is a perfectly good iterable (as tested by
97n/a list(bc)), as well as inheriting from a perfectly good
98n/a container, but __contains__ = None prevents the usual
99n/a fallback to iteration in the container protocol. That
100n/a is, normally, 0 in bc would fall back to the equivalent
101n/a of any(x==0 for x in bc), but here it's blocked from
102n/a doing so.
103n/a """
104n/a def __iter__(self):
105n/a while False:
106n/a yield None
107n/a __contains__ = None
108n/a bc = BlockContains()
109n/a self.assertFalse(0 in c)
110n/a self.assertFalse(0 in list(bc))
111n/a self.assertRaises(TypeError, lambda: 0 in bc)
112n/a
113n/aif __name__ == '__main__':
114n/a unittest.main()