annotate Lib/test/test_contains.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 65fb21750942
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45195
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
1 from test.test_support import have_unicode, run_unittest
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
2 import unittest
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
3
11931
aef5a7c62291 Test cases for __contains__ code, by Moshe Zadka.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
aef5a7c62291 Test cases for __contains__ code, by Moshe Zadka.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 class base_set:
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 12517
diff changeset
6 def __init__(self, el):
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 12517
diff changeset
7 self.el = el
11931
aef5a7c62291 Test cases for __contains__ code, by Moshe Zadka.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
aef5a7c62291 Test cases for __contains__ code, by Moshe Zadka.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 class set(base_set):
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 12517
diff changeset
10 def __contains__(self, el):
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 12517
diff changeset
11 return self.el == el
11931
aef5a7c62291 Test cases for __contains__ code, by Moshe Zadka.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
aef5a7c62291 Test cases for __contains__ code, by Moshe Zadka.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 class seq(base_set):
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 12517
diff changeset
14 def __getitem__(self, n):
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 12517
diff changeset
15 return [self.el][n]
11931
aef5a7c62291 Test cases for __contains__ code, by Moshe Zadka.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
aef5a7c62291 Test cases for __contains__ code, by Moshe Zadka.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
45195
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
18 class TestContains(unittest.TestCase):
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
19 def test_common_tests(self):
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
20 a = base_set(1)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
21 b = set(1)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
22 c = seq(1)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
23 self.assert_(1 in b)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
24 self.assert_(0 not in b)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
25 self.assert_(1 in c)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
26 self.assert_(0 not in c)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
27 self.assertRaises(TypeError, lambda: 1 in a)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
28 self.assertRaises(TypeError, lambda: 1 not in a)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
29
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
30 # test char in string
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
31 self.assert_('c' in 'abc')
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
32 self.assert_('d' not in 'abc')
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
33
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
34 self.assert_('' in '')
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
35 self.assert_('' in 'abc')
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
36
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
37 self.assertRaises(TypeError, lambda: None in 'abc')
11931
aef5a7c62291 Test cases for __contains__ code, by Moshe Zadka.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
45195
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
39 if have_unicode:
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
40 def test_char_in_unicode(self):
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
41 self.assert_('c' in unicode('abc'))
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
42 self.assert_('d' not in unicode('abc'))
11931
aef5a7c62291 Test cases for __contains__ code, by Moshe Zadka.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
45195
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
44 self.assert_('' in unicode(''))
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
45 self.assert_(unicode('') in '')
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
46 self.assert_(unicode('') in unicode(''))
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
47 self.assert_('' in unicode('abc'))
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
48 self.assert_(unicode('') in 'abc')
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
49 self.assert_(unicode('') in unicode('abc'))
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
50
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
51 self.assertRaises(TypeError, lambda: None in unicode('abc'))
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
52
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
53 # test Unicode char in Unicode
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
54 self.assert_(unicode('c') in unicode('abc'))
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
55 self.assert_(unicode('d') not in unicode('abc'))
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
56
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
57 # test Unicode char in string
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
58 self.assert_(unicode('c') in 'abc')
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
59 self.assert_(unicode('d') not in 'abc')
11931
aef5a7c62291 Test cases for __contains__ code, by Moshe Zadka.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
45195
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
61 def test_builtin_sequence_types(self):
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
62 # a collection of tests on builtin sequence types
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
63 a = range(10)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
64 for i in a:
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
65 self.assert_(i in a)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
66 self.assert_(16 not in a)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
67 self.assert_(a not in a)
11941
f6d1d0fdc1c7 Add tests for char in string -- including required exceptions for
Guido van Rossum <guido@python.org>
parents: 11931
diff changeset
68
45195
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
69 a = tuple(a)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
70 for i in a:
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
71 self.assert_(i in a)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
72 self.assert_(16 not in a)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
73 self.assert_(a not in a)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
74
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
75 class Deviant1:
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
76 """Behaves strangely when compared
11941
f6d1d0fdc1c7 Add tests for char in string -- including required exceptions for
Guido van Rossum <guido@python.org>
parents: 11931
diff changeset
77
45195
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
78 This class is designed to make sure that the contains code
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
79 works when the list is modified during the check.
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
80 """
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
81 aList = range(15)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
82 def __cmp__(self, other):
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
83 if other == 12:
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
84 self.aList.remove(12)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
85 self.aList.remove(13)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
86 self.aList.remove(14)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
87 return 1
11941
f6d1d0fdc1c7 Add tests for char in string -- including required exceptions for
Guido van Rossum <guido@python.org>
parents: 11931
diff changeset
88
45195
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
89 self.assert_(Deviant1() not in Deviant1.aList)
11941
f6d1d0fdc1c7 Add tests for char in string -- including required exceptions for
Guido van Rossum <guido@python.org>
parents: 11931
diff changeset
90
45195
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
91 class Deviant2:
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
92 """Behaves strangely when compared
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
93
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
94 This class raises an exception during comparison. That in
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
95 turn causes the comparison to fail with a TypeError.
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
96 """
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
97 def __cmp__(self, other):
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
98 if other == 4:
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
99 raise RuntimeError, "gotcha"
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
100
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
101 try:
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
102 self.assert_(Deviant2() not in a)
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
103 except TypeError:
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
104 pass
12330
77e4f21e3ee6 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 11941
diff changeset
105
77e4f21e3ee6 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 11941
diff changeset
106
45195
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
107 def test_main():
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
108 run_unittest(TestContains)
12515
fb4775111ed9 add some more contains tests on the builtin types
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12330
diff changeset
109
45195
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
110 if __name__ == '__main__':
65fb21750942 Fixes issue 1959. Converted tests to unittest.
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
111 test_main()