Updated for hfsplus module, new gusi libs.
[python/dscho.git] / Lib / test / test_contains.py
blob1a9a9650762084a223134715f2b03a6e61382a42
1 from test_support import TestFailed, have_unicode
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 TypeError:
35 pass
37 try:
38 1 not in a
39 check(0, "not in base_set did not raise error")
40 except TypeError:
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
67 if have_unicode:
69 # Test char in Unicode
71 check('c' in unicode('abc'), "'c' not in u'abc'")
72 check('d' not in unicode('abc'), "'d' in u'abc'")
74 try:
75 '' in unicode('abc')
76 check(0, "'' in u'abc' did not raise error")
77 except TypeError:
78 pass
80 try:
81 'ab' in unicode('abc')
82 check(0, "'ab' in u'abc' did not raise error")
83 except TypeError:
84 pass
86 try:
87 None in unicode('abc')
88 check(0, "None in u'abc' did not raise error")
89 except TypeError:
90 pass
92 # Test Unicode char in Unicode
94 check(unicode('c') in unicode('abc'), "u'c' not in u'abc'")
95 check(unicode('d') not in unicode('abc'), "u'd' in u'abc'")
97 try:
98 unicode('') in unicode('abc')
99 check(0, "u'' in u'abc' did not raise error")
100 except TypeError:
101 pass
103 try:
104 unicode('ab') in unicode('abc')
105 check(0, "u'ab' in u'abc' did not raise error")
106 except TypeError:
107 pass
109 # Test Unicode char in string
111 check(unicode('c') in 'abc', "u'c' not in 'abc'")
112 check(unicode('d') not in 'abc', "u'd' in 'abc'")
114 try:
115 unicode('') in 'abc'
116 check(0, "u'' in 'abc' did not raise error")
117 except TypeError:
118 pass
120 try:
121 unicode('ab') in 'abc'
122 check(0, "u'ab' in 'abc' did not raise error")
123 except TypeError:
124 pass
126 # A collection of tests on builtin sequence types
127 a = range(10)
128 for i in a:
129 check(i in a, "%s not in %s" % (`i`, `a`))
130 check(16 not in a, "16 not in %s" % `a`)
131 check(a not in a, "%s not in %s" % (`a`, `a`))
133 a = tuple(a)
134 for i in a:
135 check(i in a, "%s not in %s" % (`i`, `a`))
136 check(16 not in a, "16 not in %s" % `a`)
137 check(a not in a, "%s not in %s" % (`a`, `a`))
139 class Deviant1:
140 """Behaves strangely when compared
142 This class is designed to make sure that the contains code
143 works when the list is modified during the check.
146 aList = range(15)
148 def __cmp__(self, other):
149 if other == 12:
150 self.aList.remove(12)
151 self.aList.remove(13)
152 self.aList.remove(14)
153 return 1
155 check(Deviant1() not in Deviant1.aList, "Deviant1 failed")
157 class Deviant2:
158 """Behaves strangely when compared
160 This class raises an exception during comparison. That in
161 turn causes the comparison to fail with a TypeError.
164 def __cmp__(self, other):
165 if other == 4:
166 raise RuntimeError, "gotcha"
168 try:
169 check(Deviant2() not in a, "oops")
170 except TypeError:
171 pass