Debian Codegraph @

Lib/test/test_contains.py

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