Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2)

Side by Side Diff: Lib/test/string_tests.py

Issue 8706: accept keyword arguments on all base type methods and builtins
Patch Set: Created 8 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/collections/__init__.py ('k') | Lib/test/test_userstring.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """ 1 """
2 Common tests shared by test_str, test_unicode, test_userstring and test_string. 2 Common tests shared by test_str, test_unicode, test_userstring and test_string.
3 """ 3 """
4 4
5 import unittest, string, sys, struct 5 import unittest, string, sys, struct
6 from test import support 6 from test import support
7 from collections import UserList 7 from collections import UserList
8 8
9 class Sequence: 9 class Sequence:
10 def __init__(self, seq='wxyz'): self.seq = seq 10 def __init__(self, seq='wxyz'): self.seq = seq
(...skipping 27 matching lines...) Expand all
38 # these arguments to the appropriate type 38 # these arguments to the appropriate type
39 def fixtype(self, obj): 39 def fixtype(self, obj):
40 if isinstance(obj, str): 40 if isinstance(obj, str):
41 return self.__class__.type2test(obj) 41 return self.__class__.type2test(obj)
42 elif isinstance(obj, list): 42 elif isinstance(obj, list):
43 return [self.fixtype(x) for x in obj] 43 return [self.fixtype(x) for x in obj]
44 elif isinstance(obj, tuple): 44 elif isinstance(obj, tuple):
45 return tuple([self.fixtype(x) for x in obj]) 45 return tuple([self.fixtype(x) for x in obj])
46 elif isinstance(obj, dict): 46 elif isinstance(obj, dict):
47 return dict([ 47 return dict([
48 (self.fixtype(key), self.fixtype(value)) 48 (key, self.fixtype(value))
49 for (key, value) in obj.items() 49 for (key, value) in obj.items()
50 ]) 50 ])
51 else: 51 else:
52 return obj 52 return obj
53 53
54 # check that obj.method(*args) returns result 54 # check that obj.method(*args) returns result
55 def checkequal(self, result, obj, methodname, *args, **kwargs): 55 def checkequal(self, result, obj, methodname, *args, **kwargs):
56 result = self.fixtype(result) 56 result = self.fixtype(result)
57 obj = self.fixtype(obj) 57 obj = self.fixtype(obj)
58 args = self.fixtype(args) 58 args = self.fixtype(args)
59 kwargs = {k: self.fixtype(v) for k,v in kwargs.items()} 59 kwargs = {k: self.fixtype(v) for k,v in kwargs.items()}
60 realresult = getattr(obj, methodname)(*args, **kwargs) 60 realresult = getattr(obj, methodname)(*args, **kwargs)
61 self.assertEqual( 61 self.assertEqual(
62 result, 62 result,
63 realresult 63 realresult
64 ) 64 )
65 # if the original is returned make sure that 65 # if the original is returned make sure that
66 # this doesn't happen with subclasses 66 # this doesn't happen with subclasses
67 if obj is realresult: 67 if obj is realresult:
68 try: 68 try:
69 class subtype(self.__class__.type2test): 69 class subtype(self.__class__.type2test):
70 pass 70 pass
71 except TypeError: 71 except TypeError:
72 pass # Skip this if we can't subclass 72 pass # Skip this if we can't subclass
73 else: 73 else:
74 obj = subtype(obj) 74 obj = subtype(obj)
75 realresult = getattr(obj, methodname)(*args) 75 realresult = getattr(obj, methodname)(*args)
76 self.assertIsNot(obj, realresult) 76 self.assertIsNot(obj, realresult)
77 77
78 # check that obj.method(*args) raises exc 78 # check that obj.method(*args, **kwargs) raises exc
79 def checkraises(self, exc, obj, methodname, *args): 79 def checkraises(self, exc, obj, methodname, *args, **kwargs):
80 obj = self.fixtype(obj) 80 obj = self.fixtype(obj)
81 args = self.fixtype(args) 81 args = self.fixtype(args)
82 kwargs = self.fixtype(kwargs)
82 self.assertRaises( 83 self.assertRaises(
83 exc, 84 exc,
84 getattr(obj, methodname), 85 getattr(obj, methodname),
85 *args 86 *args, **kwargs
86 ) 87 )
87 88
88 # call obj.method(*args) without any checks 89 # call obj.method(*args) without any checks
89 def checkcall(self, obj, methodname, *args): 90 def checkcall(self, obj, methodname, *args):
90 obj = self.fixtype(obj) 91 obj = self.fixtype(obj)
91 args = self.fixtype(args) 92 args = self.fixtype(args)
92 getattr(obj, methodname)(*args) 93 getattr(obj, methodname)(*args)
93 94
94 def test_count(self): 95 def test_count(self):
95 self.checkequal(3, 'aaa', 'count', 'a') 96 self.checkequal(3, 'aaa', 'count', 'a')
97 self.checkequal(3, 'aaa', 'count', sub='a')
98 self.checkequal(0, 'aaa', 'count', 'b')
99 self.checkequal(0, 'aaa', 'count', sub='b')
100 self.checkequal(3, 'aaa', 'count', 'a')
101 self.checkequal(3, 'aaa', 'count', sub='a')
96 self.checkequal(0, 'aaa', 'count', 'b') 102 self.checkequal(0, 'aaa', 'count', 'b')
97 self.checkequal(3, 'aaa', 'count', 'a') 103 self.checkequal(3, 'aaa', 'count', 'a')
104 self.checkequal(3, 'aaa', 'count', sub='a')
98 self.checkequal(0, 'aaa', 'count', 'b') 105 self.checkequal(0, 'aaa', 'count', 'b')
99 self.checkequal(3, 'aaa', 'count', 'a') 106 self.checkequal(0, 'aaa', 'count', sub='b')
100 self.checkequal(0, 'aaa', 'count', 'b') 107 self.checkequal(0, 'aaa', 'count', 'b')
101 self.checkequal(0, 'aaa', 'count', 'b') 108 self.checkequal(0, 'aaa', 'count', sub='b')
102 self.checkequal(2, 'aaa', 'count', 'a', 1) 109 self.checkequal(2, 'aaa', 'count', 'a', 1)
110 self.checkequal(2, 'aaa', 'count', 'a', start=1)
103 self.checkequal(0, 'aaa', 'count', 'a', 10) 111 self.checkequal(0, 'aaa', 'count', 'a', 10)
112 self.checkequal(0, 'aaa', 'count', 'a', start=10)
104 self.checkequal(1, 'aaa', 'count', 'a', -1) 113 self.checkequal(1, 'aaa', 'count', 'a', -1)
114 self.checkequal(1, 'aaa', 'count', 'a', start=-1)
105 self.checkequal(3, 'aaa', 'count', 'a', -10) 115 self.checkequal(3, 'aaa', 'count', 'a', -10)
116 self.checkequal(3, 'aaa', 'count', 'a', start=-10)
106 self.checkequal(1, 'aaa', 'count', 'a', 0, 1) 117 self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
118 self.checkequal(1, 'aaa', 'count', 'a', start=0, end=1)
107 self.checkequal(3, 'aaa', 'count', 'a', 0, 10) 119 self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
120 self.checkequal(3, 'aaa', 'count', 'a', start=0, end=10)
108 self.checkequal(2, 'aaa', 'count', 'a', 0, -1) 121 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
122 self.checkequal(2, 'aaa', 'count', 'a', start=0, end=-1)
109 self.checkequal(0, 'aaa', 'count', 'a', 0, -10) 123 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
124 self.checkequal(0, 'aaa', 'count', sub='a', start=0, end=-10)
110 self.checkequal(3, 'aaa', 'count', '', 1) 125 self.checkequal(3, 'aaa', 'count', '', 1)
126 self.checkequal(3, 'aaa', 'count', '', start=1)
111 self.checkequal(1, 'aaa', 'count', '', 3) 127 self.checkequal(1, 'aaa', 'count', '', 3)
128 self.checkequal(1, 'aaa', 'count', '', start=3)
112 self.checkequal(0, 'aaa', 'count', '', 10) 129 self.checkequal(0, 'aaa', 'count', '', 10)
130 self.checkequal(0, 'aaa', 'count', sub='', start=10)
113 self.checkequal(2, 'aaa', 'count', '', -1) 131 self.checkequal(2, 'aaa', 'count', '', -1)
132 self.checkequal(2, 'aaa', 'count', sub='', start=-1)
114 self.checkequal(4, 'aaa', 'count', '', -10) 133 self.checkequal(4, 'aaa', 'count', '', -10)
134 self.checkequal(4, 'aaa', 'count', sub='', start=-10)
115 135
116 self.checkequal(1, '', 'count', '') 136 self.checkequal(1, '', 'count', '')
137 self.checkequal(1, '', 'count', sub='')
117 self.checkequal(0, '', 'count', '', 1, 1) 138 self.checkequal(0, '', 'count', '', 1, 1)
139 self.checkequal(0, '', 'count', sub='', start=1, end=1)
118 self.checkequal(0, '', 'count', '', sys.maxsize, 0) 140 self.checkequal(0, '', 'count', '', sys.maxsize, 0)
141 self.checkequal(0, '', 'count', sub='', start=sys.maxsize, end=0)
119 142
120 self.checkequal(0, '', 'count', 'xx') 143 self.checkequal(0, '', 'count', 'xx')
144 self.checkequal(0, '', 'count', sub='xx')
121 self.checkequal(0, '', 'count', 'xx', 1, 1) 145 self.checkequal(0, '', 'count', 'xx', 1, 1)
146 self.checkequal(0, '', 'count', sub='xx', start=1, end=1)
122 self.checkequal(0, '', 'count', 'xx', sys.maxsize, 0) 147 self.checkequal(0, '', 'count', 'xx', sys.maxsize, 0)
148 self.checkequal(0, '', 'count', sub='xx', start=sys.maxsize, end=0)
123 149
124 self.checkraises(TypeError, 'hello', 'count') 150 self.checkraises(TypeError, 'hello', 'count')
125 151
126 if self.contains_bytes: 152 if self.contains_bytes:
127 self.checkequal(0, 'hello', 'count', 42) 153 self.checkequal(0, 'hello', 'count', 42)
154 self.checkequal(0, 'hello', 'count', sub=42)
128 else: 155 else:
129 self.checkraises(TypeError, 'hello', 'count', 42) 156 self.checkraises(TypeError, 'hello', 'count', 42)
157 self.checkraises(TypeError, 'hello', 'count', sub=42)
130 158
131 # For a variety of combinations, 159 # For a variety of combinations,
132 # verify that str.count() matches an equivalent function 160 # verify that str.count() matches an equivalent function
133 # replacing all occurrences and then differencing the string lengths 161 # replacing all occurrences and then differencing the string lengths
134 charset = ['', 'a', 'b'] 162 charset = ['', 'a', 'b']
135 digits = 7 163 digits = 7
136 base = len(charset) 164 base = len(charset)
137 teststrings = set() 165 teststrings = set()
138 for i in range(base ** digits): 166 for i in range(base ** digits):
139 entry = [] 167 entry = []
(...skipping 10 matching lines...) Expand all
150 r2, rem = divmod(n - len(i.replace(j, self.fixtype(''))), 178 r2, rem = divmod(n - len(i.replace(j, self.fixtype(''))),
151 len(j)) 179 len(j))
152 else: 180 else:
153 r2, rem = len(i)+1, 0 181 r2, rem = len(i)+1, 0
154 if rem or r1 != r2: 182 if rem or r1 != r2:
155 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i)) 183 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
156 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i)) 184 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
157 185
158 def test_find(self): 186 def test_find(self):
159 self.checkequal(0, 'abcdefghiabc', 'find', 'abc') 187 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
188 self.checkequal(0, 'abcdefghiabc', 'find', sub='abc')
160 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1) 189 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
190 self.checkequal(9, 'abcdefghiabc', 'find', sub='abc', start=1)
161 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4) 191 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
192 self.checkequal(-1, 'abcdefghiabc', 'find', sub='def', start=4)
162 193
163 self.checkequal(0, 'abc', 'find', '', 0) 194 self.checkequal(0, 'abc', 'find', '', 0)
195 self.checkequal(0, 'abc', 'find', sub='', start=0)
164 self.checkequal(3, 'abc', 'find', '', 3) 196 self.checkequal(3, 'abc', 'find', '', 3)
197 self.checkequal(3, 'abc', 'find', sub='', start=3)
165 self.checkequal(-1, 'abc', 'find', '', 4) 198 self.checkequal(-1, 'abc', 'find', '', 4)
199 self.checkequal(-1, 'abc', 'find', sub='', start=4)
166 200
167 # to check the ability to pass None as defaults 201 # to check the ability to pass None as defaults
168 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a') 202 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a')
203 self.checkequal( 2, 'rrarrrrrrrrra', 'find', sub='a')
169 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4) 204 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4)
205 self.checkequal(12, 'rrarrrrrrrrra', 'find', sub='a', start=4)
170 self.checkequal(-1, 'rrarrrrrrrrra', 'find', 'a', 4, 6) 206 self.checkequal(-1, 'rrarrrrrrrrra', 'find', 'a', 4, 6)
207 self.checkequal(-1, 'rrarrrrrrrrra', 'find', sub='a', start=4, end=6)
171 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4, None) 208 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4, None)
209 self.checkequal(12, 'rrarrrrrrrrra', 'find', sub='a', start=4, end=None)
172 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a', None, 6) 210 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a', None, 6)
211 self.checkequal( 2, 'rrarrrrrrrrra', 'find', sub='a', start=None, end=6)
173 212
174 self.checkraises(TypeError, 'hello', 'find') 213 self.checkraises(TypeError, 'hello', 'find')
175 214
176 if self.contains_bytes: 215 if self.contains_bytes:
177 self.checkequal(-1, 'hello', 'find', 42) 216 self.checkequal(-1, 'hello', 'find', 42)
217 self.checkequal(-1, 'hello', 'find', sub=42)
178 else: 218 else:
179 self.checkraises(TypeError, 'hello', 'find', 42) 219 self.checkraises(TypeError, 'hello', 'find', 42)
220 self.checkraises(TypeError, 'hello', 'find', sub=42)
180 221
181 self.checkequal(0, '', 'find', '') 222 self.checkequal(0, '', 'find', '')
223 self.checkequal(0, '', 'find', sub='')
182 self.checkequal(-1, '', 'find', '', 1, 1) 224 self.checkequal(-1, '', 'find', '', 1, 1)
225 self.checkequal(-1, '', 'find', sub='', start=1, end=1)
183 self.checkequal(-1, '', 'find', '', sys.maxsize, 0) 226 self.checkequal(-1, '', 'find', '', sys.maxsize, 0)
227 self.checkequal(-1, '', 'find', sub='', start=sys.maxsize, end=0)
184 228
185 self.checkequal(-1, '', 'find', 'xx') 229 self.checkequal(-1, '', 'find', 'xx')
230 self.checkequal(-1, '', 'find', sub='xx')
186 self.checkequal(-1, '', 'find', 'xx', 1, 1) 231 self.checkequal(-1, '', 'find', 'xx', 1, 1)
232 self.checkequal(-1, '', 'find', sub='xx', start=1, end=1)
187 self.checkequal(-1, '', 'find', 'xx', sys.maxsize, 0) 233 self.checkequal(-1, '', 'find', 'xx', sys.maxsize, 0)
234 self.checkequal(-1, '', 'find', sub='xx', start=sys.maxsize, end=0)
188 235
189 # issue 7458 236 # issue 7458
190 self.checkequal(-1, 'ab', 'find', 'xxx', sys.maxsize + 1, 0) 237 self.checkequal(-1, 'ab', 'find', 'xxx', sys.maxsize + 1, 0)
238 self.checkequal(-1, 'ab', 'find', sub='xxx', start=sys.maxsize + 1,
239 end=0)
191 240
192 # For a variety of combinations, 241 # For a variety of combinations,
193 # verify that str.find() matches __contains__ 242 # verify that str.find() matches __contains__
194 # and that the found substring is really at that location 243 # and that the found substring is really at that location
195 charset = ['', 'a', 'b', 'c'] 244 charset = ['', 'a', 'b', 'c']
196 digits = 5 245 digits = 5
197 base = len(charset) 246 base = len(charset)
198 teststrings = set() 247 teststrings = set()
199 for i in range(base ** digits): 248 for i in range(base ** digits):
200 entry = [] 249 entry = []
201 for j in range(digits): 250 for j in range(digits):
202 i, m = divmod(i, base) 251 i, m = divmod(i, base)
203 entry.append(charset[m]) 252 entry.append(charset[m])
204 teststrings.add(''.join(entry)) 253 teststrings.add(''.join(entry))
205 teststrings = [self.fixtype(ts) for ts in teststrings] 254 teststrings = [self.fixtype(ts) for ts in teststrings]
206 for i in teststrings: 255 for i in teststrings:
207 for j in teststrings: 256 for j in teststrings:
208 loc = i.find(j) 257 loc = i.find(j)
209 r1 = (loc != -1) 258 r1 = (loc != -1)
210 r2 = j in i 259 r2 = j in i
211 self.assertEqual(r1, r2) 260 self.assertEqual(r1, r2)
212 if loc != -1: 261 if loc != -1:
213 self.assertEqual(i[loc:loc+len(j)], j) 262 self.assertEqual(i[loc:loc+len(j)], j)
214 263
215 def test_rfind(self): 264 def test_rfind(self):
216 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc') 265 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
266 self.checkequal(9, 'abcdefghiabc', 'rfind', sub='abc')
217 self.checkequal(12, 'abcdefghiabc', 'rfind', '') 267 self.checkequal(12, 'abcdefghiabc', 'rfind', '')
268 self.checkequal(12, 'abcdefghiabc', 'rfind', sub='')
218 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd') 269 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
270 self.checkequal(0, 'abcdefghiabc', 'rfind', sub='abcd')
219 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz') 271 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
272 self.checkequal(-1, 'abcdefghiabc', 'rfind', sub='abcz')
220 273
221 self.checkequal(3, 'abc', 'rfind', '', 0) 274 self.checkequal(3, 'abc', 'rfind', '', 0)
275 self.checkequal(3, 'abc', 'rfind', sub='', start=0)
222 self.checkequal(3, 'abc', 'rfind', '', 3) 276 self.checkequal(3, 'abc', 'rfind', '', 3)
277 self.checkequal(3, 'abc', 'rfind', sub='', start=3)
223 self.checkequal(-1, 'abc', 'rfind', '', 4) 278 self.checkequal(-1, 'abc', 'rfind', '', 4)
279 self.checkequal(-1, 'abc', 'rfind', sub='', start=4)
224 280
225 # to check the ability to pass None as defaults 281 # to check the ability to pass None as defaults
226 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a') 282 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a')
283 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', sub='a')
227 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4) 284 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4)
285 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', sub='a', start=4)
228 self.checkequal(-1, 'rrarrrrrrrrra', 'rfind', 'a', 4, 6) 286 self.checkequal(-1, 'rrarrrrrrrrra', 'rfind', 'a', 4, 6)
287 self.checkequal(-1, 'rrarrrrrrrrra', 'rfind', sub='a', start=4, end=6)
229 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4, None) 288 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4, None)
230 self.checkequal( 2, 'rrarrrrrrrrra', 'rfind', 'a', None, 6) 289 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', sub='a', start=4,
290 end=None)
291 self.checkequal(2, 'rrarrrrrrrrra', 'rfind', 'a', None, 6)
292 self.checkequal(2, 'rrarrrrrrrrra', 'rfind', sub='a', start=None, end=6)
231 293
232 self.checkraises(TypeError, 'hello', 'rfind') 294 self.checkraises(TypeError, 'hello', 'rfind')
233 295
234 if self.contains_bytes: 296 if self.contains_bytes:
235 self.checkequal(-1, 'hello', 'rfind', 42) 297 self.checkequal(-1, 'hello', 'rfind', 42)
298 self.checkequal(-1, 'hello', 'rfind', sub=42)
236 else: 299 else:
237 self.checkraises(TypeError, 'hello', 'rfind', 42) 300 self.checkraises(TypeError, 'hello', 'rfind', 42)
301 self.checkraises(TypeError, 'hello', 'rfind', sub=42)
238 302
239 # For a variety of combinations, 303 # For a variety of combinations,
240 # verify that str.rfind() matches __contains__ 304 # verify that str.rfind() matches __contains__
241 # and that the found substring is really at that location 305 # and that the found substring is really at that location
242 charset = ['', 'a', 'b', 'c'] 306 charset = ['', 'a', 'b', 'c']
243 digits = 5 307 digits = 5
244 base = len(charset) 308 base = len(charset)
245 teststrings = set() 309 teststrings = set()
246 for i in range(base ** digits): 310 for i in range(base ** digits):
247 entry = [] 311 entry = []
248 for j in range(digits): 312 for j in range(digits):
249 i, m = divmod(i, base) 313 i, m = divmod(i, base)
250 entry.append(charset[m]) 314 entry.append(charset[m])
251 teststrings.add(''.join(entry)) 315 teststrings.add(''.join(entry))
252 teststrings = [self.fixtype(ts) for ts in teststrings] 316 teststrings = [self.fixtype(ts) for ts in teststrings]
253 for i in teststrings: 317 for i in teststrings:
254 for j in teststrings: 318 for j in teststrings:
255 loc = i.rfind(j) 319 loc = i.rfind(j)
256 r1 = (loc != -1) 320 r1 = (loc != -1)
257 r2 = j in i 321 r2 = j in i
258 self.assertEqual(r1, r2) 322 self.assertEqual(r1, r2)
259 if loc != -1: 323 if loc != -1:
260 self.assertEqual(i[loc:loc+len(j)], j) 324 self.assertEqual(i[loc:loc+len(j)], j)
261 325
262 # issue 7458 326 # issue 7458
263 self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0) 327 self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0)
264 328
265 def test_index(self): 329 def test_index(self):
266 self.checkequal(0, 'abcdefghiabc', 'index', '') 330 self.checkequal(0, 'abcdefghiabc', 'index', '')
331 self.checkequal(0, 'abcdefghiabc', 'index', sub='')
267 self.checkequal(3, 'abcdefghiabc', 'index', 'def') 332 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
333 self.checkequal(3, 'abcdefghiabc', 'index', sub='def')
268 self.checkequal(0, 'abcdefghiabc', 'index', 'abc') 334 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
335 self.checkequal(0, 'abcdefghiabc', 'index', sub='abc')
269 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1) 336 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
337 self.checkequal(9, 'abcdefghiabc', 'index', sub='abc', start=1)
270 338
271 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib') 339 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
340 self.checkraises(ValueError, 'abcdefghiabc', 'index', sub='hib')
272 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1) 341 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
342 self.checkraises(ValueError, 'abcdefghiab', 'index', sub='abc', start=1)
273 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8) 343 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
344 self.checkraises(ValueError, 'abcdefghi', 'index', sub='ghi', start=8)
274 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1) 345 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
346 self.checkraises(ValueError, 'abcdefghi', 'index', sub='ghi', start=-1)
275 347
276 # to check the ability to pass None as defaults 348 # to check the ability to pass None as defaults
277 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a') 349 self.checkequal(2, 'rrarrrrrrrrra', 'index', 'a')
350 self.checkequal(2, 'rrarrrrrrrrra', 'index', sub='a')
278 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4) 351 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4)
352 self.checkequal(12, 'rrarrrrrrrrra', 'index', sub='a', start=4)
279 self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6) 353 self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6)
354 self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', sub='a', start=4,
355 end=6)
280 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None) 356 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None)
281 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6) 357 self.checkequal(12, 'rrarrrrrrrrra', 'index', sub='a', start=4,
358 end=None)
359 self.checkequal(2, 'rrarrrrrrrrra', 'index', sub='a', start=None,
360 end=6)
282 361
283 self.checkraises(TypeError, 'hello', 'index') 362 self.checkraises(TypeError, 'hello', 'index')
284 363
285 if self.contains_bytes: 364 if self.contains_bytes:
286 self.checkraises(ValueError, 'hello', 'index', 42) 365 self.checkraises(ValueError, 'hello', 'index', 42)
366 self.checkraises(ValueError, 'hello', 'index', sub=42)
287 else: 367 else:
288 self.checkraises(TypeError, 'hello', 'index', 42) 368 self.checkraises(TypeError, 'hello', 'index', 42)
369 self.checkraises(TypeError, 'hello', 'index', sub=42)
289 370
290 def test_rindex(self): 371 def test_rindex(self):
291 self.checkequal(12, 'abcdefghiabc', 'rindex', '') 372 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
373 self.checkequal(12, 'abcdefghiabc', 'rindex', sub='')
292 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def') 374 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
375 self.checkequal(3, 'abcdefghiabc', 'rindex', sub='def')
293 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc') 376 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
377 self.checkequal(9, 'abcdefghiabc', 'rindex', sub='abc')
294 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1) 378 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
379 self.checkequal(0, 'abcdefghiabc', 'rindex', sub='abc', start=0,
380 end=-1)
295 381
296 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib') 382 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
383 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', sub='hib')
297 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1) 384 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
385 self.checkraises(ValueError, 'defghiabc', 'rindex', sub='def', start=1)
298 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1) 386 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
387 self.checkraises(ValueError, 'defghiabc', 'rindex', sub='abc', start=0,
388 end=-1)
299 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8) 389 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
300 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1) 390 self.checkraises(ValueError, 'abcdefghi', 'rindex', sub='ghi', start=0,
391 end=8)
392 self.checkraises(ValueError, 'abcdefghi', 'rindex', sub='ghi', start=0,
393 end=-1)
301 394
302 # to check the ability to pass None as defaults 395 # to check the ability to pass None as defaults
303 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a') 396 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a')
397 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', sub='a')
304 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4) 398 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4)
399 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', sub='a', start=4)
305 self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', 'a', 4, 6) 400 self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', 'a', 4, 6)
401 self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', sub='a',
402 start=4, end=6)
306 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4, None) 403 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4, None)
307 self.checkequal( 2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6) 404 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', sub='a', start=4,
405 end=None)
406 self.checkequal(2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6)
407 self.checkequal(2, 'rrarrrrrrrrra', 'rindex', sub='a', start=None,
408 end=6)
308 409
309 self.checkraises(TypeError, 'hello', 'rindex') 410 self.checkraises(TypeError, 'hello', 'rindex')
310 411
311 if self.contains_bytes: 412 if self.contains_bytes:
312 self.checkraises(ValueError, 'hello', 'rindex', 42) 413 self.checkraises(ValueError, 'hello', 'rindex', 42)
414 self.checkraises(ValueError, 'hello', 'rindex', sub=42)
313 else: 415 else:
314 self.checkraises(TypeError, 'hello', 'rindex', 42) 416 self.checkraises(TypeError, 'hello', 'rindex', 42)
417 self.checkraises(TypeError, 'hello', 'rindex', sub=42)
315 418
316 def test_lower(self): 419 def test_lower(self):
317 self.checkequal('hello', 'HeLLo', 'lower') 420 self.checkequal('hello', 'HeLLo', 'lower')
318 self.checkequal('hello', 'hello', 'lower') 421 self.checkequal('hello', 'hello', 'lower')
319 self.checkraises(TypeError, 'hello', 'lower', 42) 422 self.checkraises(TypeError, 'hello', 'lower', 42)
320 423
321 def test_upper(self): 424 def test_upper(self):
322 self.checkequal('HELLO', 'HeLLo', 'upper') 425 self.checkequal('HELLO', 'HeLLo', 'upper')
323 self.checkequal('HELLO', 'HELLO', 'upper') 426 self.checkequal('HELLO', 'HELLO', 'upper')
324 self.checkraises(TypeError, 'hello', 'upper', 42) 427 self.checkraises(TypeError, 'hello', 'upper', 42)
(...skipping 637 matching lines...) Expand 10 before | Expand all | Expand 10 after
962 "\nabc\ndef\r\nghi\n\r", 'splitlines', True) 1065 "\nabc\ndef\r\nghi\n\r", 'splitlines', True)
963 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 1066 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r",
964 'splitlines', keepends=False) 1067 'splitlines', keepends=False)
965 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], 1068 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'],
966 "\nabc\ndef\r\nghi\n\r", 'splitlines', keepends=True) 1069 "\nabc\ndef\r\nghi\n\r", 'splitlines', keepends=True)
967 1070
968 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42) 1071 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
969 1072
970 def test_startswith(self): 1073 def test_startswith(self):
971 self.checkequal(True, 'hello', 'startswith', 'he') 1074 self.checkequal(True, 'hello', 'startswith', 'he')
1075 self.checkequal(True, 'hello', 'startswith', sub='he')
972 self.checkequal(True, 'hello', 'startswith', 'hello') 1076 self.checkequal(True, 'hello', 'startswith', 'hello')
1077 self.checkequal(True, 'hello', 'startswith', sub='hello')
973 self.checkequal(False, 'hello', 'startswith', 'hello world') 1078 self.checkequal(False, 'hello', 'startswith', 'hello world')
1079 self.checkequal(False, 'hello', 'startswith', sub='hello world')
974 self.checkequal(True, 'hello', 'startswith', '') 1080 self.checkequal(True, 'hello', 'startswith', '')
1081 self.checkequal(True, 'hello', 'startswith', sub='')
975 self.checkequal(False, 'hello', 'startswith', 'ello') 1082 self.checkequal(False, 'hello', 'startswith', 'ello')
1083 self.checkequal(False, 'hello', 'startswith', sub='ello')
976 self.checkequal(True, 'hello', 'startswith', 'ello', 1) 1084 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
1085 self.checkequal(True, 'hello', 'startswith', sub='ello', start=1)
977 self.checkequal(True, 'hello', 'startswith', 'o', 4) 1086 self.checkequal(True, 'hello', 'startswith', 'o', 4)
1087 self.checkequal(True, 'hello', 'startswith', sub='o', start=4)
978 self.checkequal(False, 'hello', 'startswith', 'o', 5) 1088 self.checkequal(False, 'hello', 'startswith', 'o', 5)
1089 self.checkequal(False, 'hello', 'startswith', sub='o', start=5)
979 self.checkequal(True, 'hello', 'startswith', '', 5) 1090 self.checkequal(True, 'hello', 'startswith', '', 5)
1091 self.checkequal(True, 'hello', 'startswith', sub='', start=5)
980 self.checkequal(False, 'hello', 'startswith', 'lo', 6) 1092 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
1093 self.checkequal(False, 'hello', 'startswith', sub='lo', start=6)
981 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3) 1094 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
1095 self.checkequal(True, 'helloworld', 'startswith', sub='lowo', start=3)
982 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7) 1096 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
983 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6) 1097 self.checkequal(True, 'helloworld', 'startswith', sub='lowo', start=3,
1098 end=7)
1099 self.checkequal(False, 'helloworld', 'startswith', sub='lowo', start=3,
1100 end=6)
984 1101
985 # test negative indices 1102 # test negative indices
986 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1) 1103 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
1104 self.checkequal(True, 'hello', 'startswith', sub='he', start=0, end=-1)
987 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1) 1105 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
1106 self.checkequal(True, 'hello', 'startswith', sub='he', start=-53,
1107 end=-1)
988 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1) 1108 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
1109 self.checkequal(False, 'hello', 'startswith', sub='hello', start=0,
1110 end=-1)
989 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10) 1111 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
1112 self.checkequal(False, 'hello', 'startswith', sub='hello world',
1113 start=-1, end=-10)
990 self.checkequal(False, 'hello', 'startswith', 'ello', -5) 1114 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
1115 self.checkequal(False, 'hello', 'startswith', sub='ello', start=-5)
991 self.checkequal(True, 'hello', 'startswith', 'ello', -4) 1116 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
1117 self.checkequal(True, 'hello', 'startswith', sub='ello', start=-4)
992 self.checkequal(False, 'hello', 'startswith', 'o', -2) 1118 self.checkequal(False, 'hello', 'startswith', 'o', -2)
1119 self.checkequal(False, 'hello', 'startswith', sub='o', start=-2)
993 self.checkequal(True, 'hello', 'startswith', 'o', -1) 1120 self.checkequal(True, 'hello', 'startswith', 'o', -1)
1121 self.checkequal(True, 'hello', 'startswith', sub='o', start=-1)
994 self.checkequal(True, 'hello', 'startswith', '', -3, -3) 1122 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
1123 self.checkequal(True, 'hello', 'startswith', sub='', start=-3, end=-3)
995 self.checkequal(False, 'hello', 'startswith', 'lo', -9) 1124 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
1125 self.checkequal(False, 'hello', 'startswith', sub='lo', start=-9)
996 1126
997 self.checkraises(TypeError, 'hello', 'startswith') 1127 self.checkraises(TypeError, 'hello', 'startswith')
998 self.checkraises(TypeError, 'hello', 'startswith', 42) 1128 self.checkraises(TypeError, 'hello', 'startswith', 42)
1129 self.checkraises(TypeError, 'hello', 'startswith', sub=42)
999 1130
1000 # test tuple arguments 1131 # test tuple arguments
1001 self.checkequal(True, 'hello', 'startswith', ('he', 'ha')) 1132 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
1133 self.checkequal(True, 'hello', 'startswith', sub=('he', 'ha'))
1002 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo')) 1134 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
1135 self.checkequal(False, 'hello', 'startswith', sub=('lo', 'llo'))
1003 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello')) 1136 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
1137 self.checkequal(True, 'hello', 'startswith', sub=('hellox', 'hello'))
1004 self.checkequal(False, 'hello', 'startswith', ()) 1138 self.checkequal(False, 'hello', 'startswith', ())
1139 self.checkequal(False, 'hello', 'startswith', sub=())
1005 self.checkequal(True, 'helloworld', 'startswith', ('hellowo', 1140 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
1006 'rld', 'lowo'), 3) 1141 'rld', 'lowo'), 3)
1142 self.checkequal(True, 'helloworld', 'startswith',
1143 sub=('hellowo', 'rld', 'lowo'), start=3)
1007 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello', 1144 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
1008 'rld'), 3) 1145 'rld'), 3)
1146 self.checkequal(False, 'helloworld', 'startswith',
1147 sub=('hellowo', 'ello', 'rld'), start=3)
1009 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1) 1148 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
1149 self.checkequal(True, 'hello', 'startswith', sub=('lo', 'he'), start=0,
1150 end=-1)
1010 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1) 1151 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
1152 self.checkequal(False, 'hello', 'startswith', sub=('he', 'hel'),
1153 start=0, end=1)
1011 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2) 1154 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
1155 self.checkequal(True, 'hello', 'startswith', sub=('he', 'hel'),
1156 start=0, end=2)
1012 1157
1013 self.checkraises(TypeError, 'hello', 'startswith', (42,)) 1158 self.checkraises(TypeError, 'hello', 'startswith', (42,))
1159 self.checkraises(TypeError, 'hello', 'startswith', sub=(42,))
1014 1160
1015 def test_endswith(self): 1161 def test_endswith(self):
1016 self.checkequal(True, 'hello', 'endswith', 'lo') 1162 self.checkequal(True, 'hello', 'endswith', 'lo')
1163 self.checkequal(True, 'hello', 'endswith', sub='lo')
1017 self.checkequal(False, 'hello', 'endswith', 'he') 1164 self.checkequal(False, 'hello', 'endswith', 'he')
1165 self.checkequal(False, 'hello', 'endswith', sub='he')
1018 self.checkequal(True, 'hello', 'endswith', '') 1166 self.checkequal(True, 'hello', 'endswith', '')
1167 self.checkequal(True, 'hello', 'endswith', sub='')
1019 self.checkequal(False, 'hello', 'endswith', 'hello world') 1168 self.checkequal(False, 'hello', 'endswith', 'hello world')
1169 self.checkequal(False, 'hello', 'endswith', sub='hello world')
1020 self.checkequal(False, 'helloworld', 'endswith', 'worl') 1170 self.checkequal(False, 'helloworld', 'endswith', 'worl')
1171 self.checkequal(False, 'helloworld', 'endswith', sub='worl')
1021 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9) 1172 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
1173 self.checkequal(True, 'helloworld', 'endswith', sub='worl', start=3,
1174 end=9)
1022 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12) 1175 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
1176 self.checkequal(True, 'helloworld', 'endswith', sub='world', start=3,
1177 end=12)
1023 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7) 1178 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
1179 self.checkequal(True, 'helloworld', 'endswith', sub='lowo', start=1,
1180 end=7)
1024 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7) 1181 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
1182 self.checkequal(True, 'helloworld', 'endswith', sub='lowo', start=2,
1183 end=7)
1025 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7) 1184 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
1185 self.checkequal(True, 'helloworld', 'endswith', sub='lowo', start=3,
1186 end=7)
1026 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7) 1187 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
1188 self.checkequal(False, 'helloworld', 'endswith', sub='lowo', start=4,
1189 end=7)
1027 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8) 1190 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
1191 self.checkequal(False, 'helloworld', 'endswith', sub='lowo', start=3,
1192 end=8)
1028 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1) 1193 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
1194 self.checkequal(False, 'ab', 'endswith', sub='ab', start=0, end=1)
1029 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0) 1195 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
1196 self.checkequal(False, 'ab', 'endswith', sub='ab', start=0, end=0)
1030 1197
1031 # test negative indices 1198 # test negative indices
1032 self.checkequal(True, 'hello', 'endswith', 'lo', -2) 1199 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
1200 self.checkequal(True, 'hello', 'endswith', sub='lo', start=-2)
1033 self.checkequal(False, 'hello', 'endswith', 'he', -2) 1201 self.checkequal(False, 'hello', 'endswith', 'he', -2)
1202 self.checkequal(False, 'hello', 'endswith', sub='he', start=-2)
1034 self.checkequal(True, 'hello', 'endswith', '', -3, -3) 1203 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
1204 self.checkequal(True, 'hello', 'endswith', sub='', start=-3, end=-3)
1035 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2) 1205 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
1206 self.checkequal(False, 'hello', 'endswith', sub='hello world',
1207 start=-10, end=-2)
1036 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6) 1208 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
1209 self.checkequal(False, 'helloworld', 'endswith', sub='worl', start=-6)
1037 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1) 1210 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
1211 self.checkequal(True, 'helloworld', 'endswith', sub='worl', start=-5,
1212 end=-1)
1038 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9) 1213 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
1214 self.checkequal(True, 'helloworld', 'endswith', sub='worl', start=-5,
1215 end=9)
1039 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12) 1216 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
1217 self.checkequal(True, 'helloworld', 'endswith', sub='world', start=-7,
1218 end=12)
1040 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3) 1219 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
1220 self.checkequal(True, 'helloworld', 'endswith', sub='lowo', start=-99,
1221 end=-3)
1041 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3) 1222 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
1223 self.checkequal(True, 'helloworld', 'endswith', sub='lowo', start=-8,
1224 end=-3)
1042 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3) 1225 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
1226 self.checkequal(True, 'helloworld', 'endswith', sub='lowo', start=-7,
1227 end=-3)
1043 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4) 1228 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
1229 self.checkequal(False, 'helloworld', 'endswith', sub='lowo', start=3,
1230 end=-4)
1044 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2) 1231 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
1232 self.checkequal(False, 'helloworld', 'endswith', sub='lowo', start=-8,
1233 end=-2)
1045 1234
1046 self.checkraises(TypeError, 'hello', 'endswith') 1235 self.checkraises(TypeError, 'hello', 'endswith')
1047 self.checkraises(TypeError, 'hello', 'endswith', 42) 1236 self.checkraises(TypeError, 'hello', 'endswith', 42)
1237 self.checkraises(TypeError, 'hello', 'endswith', sub=42)
1048 1238
1049 # test tuple arguments 1239 # test tuple arguments
1050 self.checkequal(False, 'hello', 'endswith', ('he', 'ha')) 1240 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
1241 self.checkequal(False, 'hello', 'endswith', sub=('he', 'ha'))
1051 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo')) 1242 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
1243 self.checkequal(True, 'hello', 'endswith', sub=('lo', 'llo'))
1052 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello')) 1244 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
1245 self.checkequal(True, 'hello', 'endswith', sub=('hellox', 'hello'))
1053 self.checkequal(False, 'hello', 'endswith', ()) 1246 self.checkequal(False, 'hello', 'endswith', ())
1247 self.checkequal(False, 'hello', 'endswith', sub=())
1054 self.checkequal(True, 'helloworld', 'endswith', ('hellowo', 1248 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
1055 'rld', 'lowo'), 3) 1249 'rld', 'lowo'), 3)
1250 self.checkequal(True, 'helloworld', 'endswith',
1251 sub=('hellowo','rld', 'lowo'), start=3)
1056 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello', 1252 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
1057 'rld'), 3, -1) 1253 'rld'), 3, -1)
1254 self.checkequal(False, 'helloworld', 'endswith',
1255 sub=('hellowo', 'ello','rld'), start=3, end=-1)
1058 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1) 1256 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
1059 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1) 1257 self.checkequal(True, 'hello', 'endswith', sub=('hell', 'ell'), start=0,
1060 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4) 1258 end=-1)
1259 self.checkequal(False, 'hello', 'endswith', sub=('he', 'hel'), start=0,
1260 end=1)
1261 self.checkequal(True, 'hello', 'endswith', sub=('he', 'hell'), start=0,
1262 end=4)
1061 1263
1062 self.checkraises(TypeError, 'hello', 'endswith', (42,)) 1264 self.checkraises(TypeError, 'hello', 'endswith', (42,))
1265 self.checkraises(TypeError, 'hello', 'endswith', sub=(42,))
1063 1266
1064 def test___contains__(self): 1267 def test___contains__(self):
1065 self.checkequal(True, '', '__contains__', '') 1268 self.checkequal(True, '', '__contains__', '')
1066 self.checkequal(True, 'abc', '__contains__', '') 1269 self.checkequal(True, 'abc', '__contains__', '')
1067 self.checkequal(False, 'abc', '__contains__', '\0') 1270 self.checkequal(False, 'abc', '__contains__', '\0')
1068 self.checkequal(True, '\0abc', '__contains__', '\0') 1271 self.checkequal(True, '\0abc', '__contains__', '\0')
1069 self.checkequal(True, 'abc\0', '__contains__', '\0') 1272 self.checkequal(True, 'abc\0', '__contains__', '\0')
1070 self.checkequal(True, '\0abc', '__contains__', 'a') 1273 self.checkequal(True, '\0abc', '__contains__', 'a')
1071 self.checkequal(True, 'asdf', '__contains__', 'asdf') 1274 self.checkequal(True, 'asdf', '__contains__', 'asdf')
1072 self.checkequal(False, 'asd', '__contains__', 'asdf') 1275 self.checkequal(False, 'asd', '__contains__', 'asdf')
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
1350 ## self.assertIsNot(s1, s2) 1553 ## self.assertIsNot(s1, s2)
1351 ## self.assertIs(type(s2), str) # promotes! 1554 ## self.assertIs(type(s2), str) # promotes!
1352 1555
1353 ## s1 = t("abcd") 1556 ## s1 = t("abcd")
1354 ## s2 = "".join([s1]) 1557 ## s2 = "".join([s1])
1355 ## self.assertIsNot(s1, s2) 1558 ## self.assertIsNot(s1, s2)
1356 ## self.assertIs(type(s2), str) # promotes! 1559 ## self.assertIs(type(s2), str) # promotes!
1357 1560
1358 else: 1561 else:
1359 self.fail("unexpected type for MixinStrUnicodeTest %r" % t) 1562 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)
OLDNEW
« no previous file with comments | « Lib/collections/__init__.py ('k') | Lib/test/test_userstring.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+