Update decimal test data to the most recent set from Mike Cowlishaw.
[python.git] / Lib / test / test_contains.py
blob73e068584220a2528707f7dcc53c85a1f838d5f5
1 from test.test_support import have_unicode, run_unittest
2 import unittest
5 class base_set:
6 def __init__(self, el):
7 self.el = el
9 class set(base_set):
10 def __contains__(self, el):
11 return self.el == el
13 class seq(base_set):
14 def __getitem__(self, n):
15 return [self.el][n]
18 class TestContains(unittest.TestCase):
19 def test_common_tests(self):
20 a = base_set(1)
21 b = set(1)
22 c = seq(1)
23 self.assertTrue(1 in b)
24 self.assertTrue(0 not in b)
25 self.assertTrue(1 in c)
26 self.assertTrue(0 not in c)
27 self.assertRaises(TypeError, lambda: 1 in a)
28 self.assertRaises(TypeError, lambda: 1 not in a)
30 # test char in string
31 self.assertTrue('c' in 'abc')
32 self.assertTrue('d' not in 'abc')
34 self.assertTrue('' in '')
35 self.assertTrue('' in 'abc')
37 self.assertRaises(TypeError, lambda: None in 'abc')
39 if have_unicode:
40 def test_char_in_unicode(self):
41 self.assertTrue('c' in unicode('abc'))
42 self.assertTrue('d' not in unicode('abc'))
44 self.assertTrue('' in unicode(''))
45 self.assertTrue(unicode('') in '')
46 self.assertTrue(unicode('') in unicode(''))
47 self.assertTrue('' in unicode('abc'))
48 self.assertTrue(unicode('') in 'abc')
49 self.assertTrue(unicode('') in unicode('abc'))
51 self.assertRaises(TypeError, lambda: None in unicode('abc'))
53 # test Unicode char in Unicode
54 self.assertTrue(unicode('c') in unicode('abc'))
55 self.assertTrue(unicode('d') not in unicode('abc'))
57 # test Unicode char in string
58 self.assertTrue(unicode('c') in 'abc')
59 self.assertTrue(unicode('d') not in 'abc')
61 def test_builtin_sequence_types(self):
62 # a collection of tests on builtin sequence types
63 a = range(10)
64 for i in a:
65 self.assertTrue(i in a)
66 self.assertTrue(16 not in a)
67 self.assertTrue(a not in a)
69 a = tuple(a)
70 for i in a:
71 self.assertTrue(i in a)
72 self.assertTrue(16 not in a)
73 self.assertTrue(a not in a)
75 class Deviant1:
76 """Behaves strangely when compared
78 This class is designed to make sure that the contains code
79 works when the list is modified during the check.
80 """
81 aList = range(15)
82 def __cmp__(self, other):
83 if other == 12:
84 self.aList.remove(12)
85 self.aList.remove(13)
86 self.aList.remove(14)
87 return 1
89 self.assertTrue(Deviant1() not in Deviant1.aList)
91 class Deviant2:
92 """Behaves strangely when compared
94 This class raises an exception during comparison. That in
95 turn causes the comparison to fail with a TypeError.
96 """
97 def __cmp__(self, other):
98 if other == 4:
99 raise RuntimeError, "gotcha"
101 try:
102 self.assertTrue(Deviant2() not in a)
103 except TypeError:
104 pass
107 def test_main():
108 run_unittest(TestContains)
110 if __name__ == '__main__':
111 test_main()