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