Apparently the code to forestall Tk eating events was too aggressive (Tk user input...
[python/dscho.git] / Lib / test / test_contains.py
blob499d587a0ac81ce726f854e93593facf14dae349
1 from test_support import TestFailed
3 class base_set:
5 def __init__(self, el):
6 self.el = el
8 class set(base_set):
10 def __contains__(self, el):
11 return self.el == el
13 class seq(base_set):
15 def __getitem__(self, n):
16 return [self.el][n]
18 def check(ok, *args):
19 if not ok:
20 raise TestFailed, " ".join(map(str, args))
22 a = base_set(1)
23 b = set(1)
24 c = seq(1)
26 check(1 in b, "1 not in set(1)")
27 check(0 not in b, "0 in set(1)")
28 check(1 in c, "1 not in seq(1)")
29 check(0 not in c, "0 in seq(1)")
31 try:
32 1 in a
33 check(0, "in base_set did not raise error")
34 except AttributeError:
35 pass
37 try:
38 1 not in a
39 check(0, "not in base_set did not raise error")
40 except AttributeError:
41 pass
43 # Test char in string
45 check('c' in 'abc', "'c' not in 'abc'")
46 check('d' not in 'abc', "'d' in 'abc'")
48 try:
49 '' in 'abc'
50 check(0, "'' in 'abc' did not raise error")
51 except TypeError:
52 pass
54 try:
55 'ab' in 'abc'
56 check(0, "'ab' in 'abc' did not raise error")
57 except TypeError:
58 pass
60 try:
61 None in 'abc'
62 check(0, "None in 'abc' did not raise error")
63 except TypeError:
64 pass
66 # Test char in Unicode
68 check('c' in u'abc', "'c' not in u'abc'")
69 check('d' not in u'abc', "'d' in u'abc'")
71 try:
72 '' in u'abc'
73 check(0, "'' in u'abc' did not raise error")
74 except TypeError:
75 pass
77 try:
78 'ab' in u'abc'
79 check(0, "'ab' in u'abc' did not raise error")
80 except TypeError:
81 pass
83 try:
84 None in u'abc'
85 check(0, "None in u'abc' did not raise error")
86 except TypeError:
87 pass
89 # Test Unicode char in Unicode
91 check(u'c' in u'abc', "u'c' not in u'abc'")
92 check(u'd' not in u'abc', "u'd' in u'abc'")
94 try:
95 u'' in u'abc'
96 check(0, "u'' in u'abc' did not raise error")
97 except TypeError:
98 pass
100 try:
101 u'ab' in u'abc'
102 check(0, "u'ab' in u'abc' did not raise error")
103 except TypeError:
104 pass
106 # Test Unicode char in string
108 check(u'c' in 'abc', "u'c' not in 'abc'")
109 check(u'd' not in 'abc', "u'd' in 'abc'")
111 try:
112 u'' in 'abc'
113 check(0, "u'' in 'abc' did not raise error")
114 except TypeError:
115 pass
117 try:
118 u'ab' in 'abc'
119 check(0, "u'ab' in 'abc' did not raise error")
120 except TypeError:
121 pass
123 # A collection of tests on builtin sequence types
124 a = range(10)
125 for i in a:
126 check(i in a, "%s not in %s" % (`i`, `a`))
127 check(16 not in a, "16 not in %s" % `a`)
128 check(a not in a, "%s not in %s" % (`a`, `a`))
130 a = tuple(a)
131 for i in a:
132 check(i in a, "%s not in %s" % (`i`, `a`))
133 check(16 not in a, "16 not in %s" % `a`)
134 check(a not in a, "%s not in %s" % (`a`, `a`))
136 class Deviant1:
137 """Behaves strangely when compared
139 This class is designed to make sure that the contains code
140 works when the list is modified during the check.
143 aList = range(15)
145 def __cmp__(self, other):
146 if other == 12:
147 self.aList.remove(12)
148 self.aList.remove(13)
149 self.aList.remove(14)
150 return 1
152 check(Deviant1() not in Deviant1.aList, "Deviant1 failed")
154 class Deviant2:
155 """Behaves strangely when compared
157 This class raises an exception during comparison. That in
158 turn causes the comparison to fail with a TypeError.
161 def __cmp__(self, other):
162 if other == 4:
163 raise RuntimeError, "gotcha"
165 try:
166 check(Deviant2() not in a, "oops")
167 except TypeError:
168 pass