Lines Matching refs:self
12 def __init__(self, seq='wxyz'): self.seq = seq
13 def __len__(self): return len(self.seq)
14 def __getitem__(self, i): return self.seq[i]
17 def __init__(self): self.seq = [7, 'hello', 123]
18 def __str__(self): return '{0} {1} {2}'.format(*self.seq)
21 def __init__(self): self.seq = ['a', 'b', 'c']
22 def __len__(self): return 8
41 def fixtype(self, obj):
43 return self.__class__.type2test(obj)
45 return [self.fixtype(x) for x in obj]
47 return tuple([self.fixtype(x) for x in obj])
50 (self.fixtype(key), self.fixtype(value))
56 def test_fixtype(self):
57 self.assertIs(type(self.fixtype("123")), self.type2test)
60 def checkequal(self, result, obj, methodname, *args, **kwargs):
61 result = self.fixtype(result)
62 obj = self.fixtype(obj)
63 args = self.fixtype(args)
64 kwargs = {k: self.fixtype(v) for k,v in kwargs.items()}
66 self.assertEqual(
74 class subtype(self.__class__.type2test):
81 self.assertIsNot(obj, realresult)
84 def checkraises(self, exc, obj, methodname, *args, expected_msg=None):
85 obj = self.fixtype(obj)
86 args = self.fixtype(args)
87 with self.assertRaises(exc) as cm:
89 self.assertNotEqual(str(cm.exception), '')
91 self.assertEqual(str(cm.exception), expected_msg)
94 def checkcall(self, obj, methodname, *args):
95 obj = self.fixtype(obj)
96 args = self.fixtype(args)
99 def test_count(self):
100 self.checkequal(3, 'aaa', 'count', 'a')
101 self.checkequal(0, 'aaa', 'count', 'b')
102 self.checkequal(3, 'aaa', 'count', 'a')
103 self.checkequal(0, 'aaa', 'count', 'b')
104 self.checkequal(3, 'aaa', 'count', 'a')
105 self.checkequal(0, 'aaa', 'count', 'b')
106 self.checkequal(0, 'aaa', 'count', 'b')
107 self.checkequal(2, 'aaa', 'count', 'a', 1)
108 self.checkequal(0, 'aaa', 'count', 'a', 10)
109 self.checkequal(1, 'aaa', 'count', 'a', -1)
110 self.checkequal(3, 'aaa', 'count', 'a', -10)
111 self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
112 self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
113 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
114 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
115 self.checkequal(3, 'aaa', 'count', '', 1)
116 self.checkequal(1, 'aaa', 'count', '', 3)
117 self.checkequal(0, 'aaa', 'count', '', 10)
118 self.checkequal(2, 'aaa', 'count', '', -1)
119 self.checkequal(4, 'aaa', 'count', '', -10)
121 self.checkequal(1, '', 'count', '')
122 self.checkequal(0, '', 'count', '', 1, 1)
123 self.checkequal(0, '', 'count', '', sys.maxsize, 0)
125 self.checkequal(0, '', 'count', 'xx')
126 self.checkequal(0, '', 'count', 'xx', 1, 1)
127 self.checkequal(0, '', 'count', 'xx', sys.maxsize, 0)
129 self.checkraises(TypeError, 'hello', 'count')
131 if self.contains_bytes:
132 self.checkequal(0, 'hello', 'count', 42)
134 self.checkraises(TypeError, 'hello', 'count', 42)
149 teststrings = [self.fixtype(ts) for ts in teststrings]
155 r2, rem = divmod(n - len(i.replace(j, self.fixtype(''))),
160 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
161 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
163 def test_find(self):
164 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
165 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
166 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
168 self.checkequal(0, 'abc', 'find', '', 0)
169 self.checkequal(3, 'abc', 'find', '', 3)
170 self.checkequal(-1, 'abc', 'find', '', 4)
173 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a')
174 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4)
175 self.checkequal(-1, 'rrarrrrrrrrra', 'find', 'a', 4, 6)
176 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4, None)
177 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a', None, 6)
179 self.checkraises(TypeError, 'hello', 'find')
181 if self.contains_bytes:
182 self.checkequal(-1, 'hello', 'find', 42)
184 self.checkraises(TypeError, 'hello', 'find', 42)
186 self.checkequal(0, '', 'find', '')
187 self.checkequal(-1, '', 'find', '', 1, 1)
188 self.checkequal(-1, '', 'find', '', sys.maxsize, 0)
190 self.checkequal(-1, '', 'find', 'xx')
191 self.checkequal(-1, '', 'find', 'xx', 1, 1)
192 self.checkequal(-1, '', 'find', 'xx', sys.maxsize, 0)
195 self.checkequal(-1, 'ab', 'find', 'xxx', sys.maxsize + 1, 0)
210 teststrings = [self.fixtype(ts) for ts in teststrings]
216 self.assertEqual(r1, r2)
218 self.assertEqual(i[loc:loc+len(j)], j)
220 def test_rfind(self):
221 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
222 self.checkequal(12, 'abcdefghiabc', 'rfind', '')
223 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
224 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
226 self.checkequal(3, 'abc', 'rfind', '', 0)
227 self.checkequal(3, 'abc', 'rfind', '', 3)
228 self.checkequal(-1, 'abc', 'rfind', '', 4)
231 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a')
232 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4)
233 self.checkequal(-1, 'rrarrrrrrrrra', 'rfind', 'a', 4, 6)
234 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4, None)
235 self.checkequal( 2, 'rrarrrrrrrrra', 'rfind', 'a', None, 6)
237 self.checkraises(TypeError, 'hello', 'rfind')
239 if self.contains_bytes:
240 self.checkequal(-1, 'hello', 'rfind', 42)
242 self.checkraises(TypeError, 'hello', 'rfind', 42)
257 teststrings = [self.fixtype(ts) for ts in teststrings]
263 self.assertEqual(r1, r2)
265 self.assertEqual(i[loc:loc+len(j)], j)
268 self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0)
271 self.checkequal(0, '<......\u043c...', "rfind", "<")
273 def test_index(self):
274 self.checkequal(0, 'abcdefghiabc', 'index', '')
275 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
276 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
277 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
279 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
280 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
281 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
282 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
285 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a')
286 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4)
287 self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6)
288 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None)
289 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6)
291 self.checkraises(TypeError, 'hello', 'index')
293 if self.contains_bytes:
294 self.checkraises(ValueError, 'hello', 'index', 42)
296 self.checkraises(TypeError, 'hello', 'index', 42)
298 def test_rindex(self):
299 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
300 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
301 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
302 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
304 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
305 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
306 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
307 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
308 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
311 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a')
312 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4)
313 self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', 'a', 4, 6)
314 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4, None)
315 self.checkequal( 2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6)
317 self.checkraises(TypeError, 'hello', 'rindex')
319 if self.contains_bytes:
320 self.checkraises(ValueError, 'hello', 'rindex', 42)
322 self.checkraises(TypeError, 'hello', 'rindex', 42)
324 def test_find_periodic_pattern(self):
340 with self.subTest(p=p, text=text):
341 self.checkequal(reference_find(p, text),
344 def test_find_shift_table_overflow(self):
352 self.checkequal(len(text1)-len(pattern1),
359 self.checkequal(len(text2) - N*len("de") - len(pattern2),
362 def test_lower(self):
363 self.checkequal('hello', 'HeLLo', 'lower')
364 self.checkequal('hello', 'hello', 'lower')
365 self.checkraises(TypeError, 'hello', 'lower', 42)
367 def test_upper(self):
368 self.checkequal('HELLO', 'HeLLo', 'upper')
369 self.checkequal('HELLO', 'HELLO', 'upper')
370 self.checkraises(TypeError, 'hello', 'upper', 42)
372 def test_expandtabs(self):
373 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi',
375 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi',
377 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi',
379 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi',
381 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi',
383 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi',
385 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi',
388 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi',
390 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi',
393 self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)
395 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
398 self.checkraises(OverflowError,
401 def test_split(self):
403 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
404 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
405 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
406 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
407 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
408 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
409 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
411 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
412 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
413 self.checkequal(['abcd'], 'abcd', 'split', '|')
414 self.checkequal([''], '', 'split', '|')
415 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
416 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
417 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
418 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
420 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
421 self.checkequal(['a']*15 +['a|a|a|a|a'],
425 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
426 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
427 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
428 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
429 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
430 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
432 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
433 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
434 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
435 self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')
436 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
438 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
439 self.checkequal(['', ''], 'aaa', 'split', 'aaa')
440 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
441 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
442 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
443 self.checkequal([''], '', 'split', 'aaa')
444 self.checkequal(['aa'], 'aa', 'split', 'aaa')
445 self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')
446 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
448 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
449 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
450 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
454 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', sep='|')
455 self.checkequal(['a', 'b|c|d'],
457 self.checkequal(['a', 'b|c|d'],
459 self.checkequal(['a', 'b|c|d'],
461 self.checkequal(['a', 'b c d'],
465 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
468 self.checkraises(ValueError, 'hello', 'split', '')
469 self.checkraises(ValueError, 'hello', 'split', '', 0)
471 def test_rsplit(self):
473 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit')
474 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit')
475 self.checkequal([], '', 'rsplit')
478 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
479 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
480 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
481 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
482 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
483 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',
485 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
486 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
487 self.checkequal(['abcd'], 'abcd', 'rsplit', '|')
488 self.checkequal([''], '', 'rsplit', '|')
489 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
490 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
491 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
493 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
495 self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')
496 self.checkequal(['a|a|a|a|a']+['a']*15,
500 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
501 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
502 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
503 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
504 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
505 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',
507 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
508 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
509 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
510 self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')
511 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
513 self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')
514 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
515 self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)
516 self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')
517 self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')
518 self.checkequal([''], '', 'rsplit', 'aaa')
519 self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')
520 self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')
521 self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')
523 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')
524 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)
525 self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],
529 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', sep='|')
530 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', sep=None)
531 self.checkequal(['a b c', 'd'],
533 self.checkequal(['a|b|c', 'd'],
535 self.checkequal(['a|b|c', 'd'],
537 self.checkequal(['a|b|c', 'd'],
539 self.checkequal(['a b c', 'd'],
543 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
546 self.checkraises(ValueError, 'hello', 'rsplit', '')
547 self.checkraises(ValueError, 'hello', 'rsplit', '', 0)
549 def test_replace(self):
550 EQ = self.checkequal
699 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
700 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
701 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
702 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
703 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
704 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
705 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
706 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
707 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
708 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
709 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
710 self.checkequal('abc', 'abc', 'replace', '', '-', 0)
711 self.checkequal('', '', 'replace', '', '')
712 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
713 self.checkequal('abc', 'abc', 'replace', 'xy', '--')
717 self.checkequal('', '123', 'replace', '123', '')
718 self.checkequal('', '123123', 'replace', '123', '')
719 self.checkequal('x', '123x123', 'replace', '123', '')
721 self.checkraises(TypeError, 'hello', 'replace')
722 self.checkraises(TypeError, 'hello', 'replace', 42)
723 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
724 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
728 def test_replace_overflow(self):
731 self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
732 self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
733 self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
735 def test_removeprefix(self):
736 self.checkequal('am', 'spam', 'removeprefix', 'sp')
737 self.checkequal('spamspam', 'spamspamspam', 'removeprefix', 'spam')
738 self.checkequal('spam', 'spam', 'removeprefix', 'python')
739 self.checkequal('spam', 'spam', 'removeprefix', 'spider')
740 self.checkequal('spam', 'spam', 'removeprefix', 'spam and eggs')
742 self.checkequal('', '', 'removeprefix', '')
743 self.checkequal('', '', 'removeprefix', 'abcde')
744 self.checkequal('abcde', 'abcde', 'removeprefix', '')
745 self.checkequal('', 'abcde', 'removeprefix', 'abcde')
747 self.checkraises(TypeError, 'hello', 'removeprefix')
748 self.checkraises(TypeError, 'hello', 'removeprefix', 42)
749 self.checkraises(TypeError, 'hello', 'removeprefix', 42, 'h')
750 self.checkraises(TypeError, 'hello', 'removeprefix', 'h', 42)
751 self.checkraises(TypeError, 'hello', 'removeprefix', ("he", "l"))
753 def test_removesuffix(self):
754 self.checkequal('sp', 'spam', 'removesuffix', 'am')
755 self.checkequal('spamspam', 'spamspamspam', 'removesuffix', 'spam')
756 self.checkequal('spam', 'spam', 'removesuffix', 'python')
757 self.checkequal('spam', 'spam', 'removesuffix', 'blam')
758 self.checkequal('spam', 'spam', 'removesuffix', 'eggs and spam')
760 self.checkequal('', '', 'removesuffix', '')
761 self.checkequal('', '', 'removesuffix', 'abcde')
762 self.checkequal('abcde', 'abcde', 'removesuffix', '')
763 self.checkequal('', 'abcde', 'removesuffix', 'abcde')
765 self.checkraises(TypeError, 'hello', 'removesuffix')
766 self.checkraises(TypeError, 'hello', 'removesuffix', 42)
767 self.checkraises(TypeError, 'hello', 'removesuffix', 42, 'h')
768 self.checkraises(TypeError, 'hello', 'removesuffix', 'h', 42)
769 self.checkraises(TypeError, 'hello', 'removesuffix', ("lo", "l"))
771 def test_capitalize(self):
772 self.checkequal(' hello ', ' hello ', 'capitalize')
773 self.checkequal('Hello ', 'Hello ','capitalize')
774 self.checkequal('Hello ', 'hello ','capitalize')
775 self.checkequal('Aaaa', 'aaaa', 'capitalize')
776 self.checkequal('Aaaa', 'AaAa', 'capitalize')
778 self.checkraises(TypeError, 'hello', 'capitalize', 42)
780 def test_additional_split(self):
781 self.checkequal(['this', 'is', 'the', 'split', 'function'],
785 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
786 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
787 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
788 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
789 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
790 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,
792 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
793 self.checkequal(['a b c d'], ' a b c d', 'split', None, 0)
794 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
796 self.checkequal([], ' ', 'split')
797 self.checkequal(['a'], ' a ', 'split')
798 self.checkequal(['a', 'b'], ' a b ', 'split')
799 self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
800 self.checkequal(['a b c '], ' a b c ', 'split', None, 0)
801 self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
802 self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
803 self.checkequal(['a', 'b', 'c'], ' a b c ', 'split', None, 3)
804 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
806 self.checkequal(['a']*20, aaa, 'split')
807 self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
808 self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
812 self.checkequal(['arf', 'barf'], b, 'split')
813 self.checkequal(['arf', 'barf'], b, 'split', None)
814 self.checkequal(['arf', 'barf'], b, 'split', None, 2)
816 def test_additional_rsplit(self):
817 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
821 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
822 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
823 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
824 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
825 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
826 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,
828 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
829 self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0)
830 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
832 self.checkequal([], ' ', 'rsplit')
833 self.checkequal(['a'], ' a ', 'rsplit')
834 self.checkequal(['a', 'b'], ' a b ', 'rsplit')
835 self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
836 self.checkequal([' a b c'], ' a b c ', 'rsplit',
838 self.checkequal([' a b','c'], ' a b c ', 'rsplit',
840 self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
842 self.checkequal(['a', 'b', 'c'], ' a b c ', 'rsplit',
844 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
846 self.checkequal(['a']*20, aaa, 'rsplit')
847 self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
848 self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
852 self.checkequal(['arf', 'barf'], b, 'rsplit')
853 self.checkequal(['arf', 'barf'], b, 'rsplit', None)
854 self.checkequal(['arf', 'barf'], b, 'rsplit', None, 2)
856 def test_strip_whitespace(self):
857 self.checkequal('hello', ' hello ', 'strip')
858 self.checkequal('hello ', ' hello ', 'lstrip')
859 self.checkequal(' hello', ' hello ', 'rstrip')
860 self.checkequal('hello', 'hello', 'strip')
863 self.checkequal('abc', b, 'strip')
864 self.checkequal('abc \t\n\r\f\v', b, 'lstrip')
865 self.checkequal(' \t\n\r\f\vabc', b, 'rstrip')
868 self.checkequal('hello', ' hello ', 'strip', None)
869 self.checkequal('hello ', ' hello ', 'lstrip', None)
870 self.checkequal(' hello', ' hello ', 'rstrip', None)
871 self.checkequal('hello', 'hello', 'strip', None)
873 def test_strip(self):
875 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
876 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
877 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
878 self.checkequal('hello', 'hello', 'strip', 'xyz')
879 self.checkequal('', 'mississippi', 'strip', 'mississippi')
882 self.checkequal('mississipp', 'mississippi', 'strip', 'i')
884 self.checkraises(TypeError, 'hello', 'strip', 42, 42)
885 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
886 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
888 def test_ljust(self):
889 self.checkequal('abc ', 'abc', 'ljust', 10)
890 self.checkequal('abc ', 'abc', 'ljust', 6)
891 self.checkequal('abc', 'abc', 'ljust', 3)
892 self.checkequal('abc', 'abc', 'ljust', 2)
893 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
894 self.checkraises(TypeError, 'abc', 'ljust')
896 def test_rjust(self):
897 self.checkequal(' abc', 'abc', 'rjust', 10)
898 self.checkequal(' abc', 'abc', 'rjust', 6)
899 self.checkequal('abc', 'abc', 'rjust', 3)
900 self.checkequal('abc', 'abc', 'rjust', 2)
901 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
902 self.checkraises(TypeError, 'abc', 'rjust')
904 def test_center(self):
905 self.checkequal(' abc ', 'abc', 'center', 10)
906 self.checkequal(' abc ', 'abc', 'center', 6)
907 self.checkequal('abc', 'abc', 'center', 3)
908 self.checkequal('abc', 'abc', 'center', 2)
909 self.checkequal('***abc****', 'abc', 'center', 10, '*')
910 self.checkraises(TypeError, 'abc', 'center')
912 def test_swapcase(self):
913 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
915 self.checkraises(TypeError, 'hello', 'swapcase', 42)
917 def test_zfill(self):
918 self.checkequal('123', '123', 'zfill', 2)
919 self.checkequal('123', '123', 'zfill', 3)
920 self.checkequal('0123', '123', 'zfill', 4)
921 self.checkequal('+123', '+123', 'zfill', 3)
922 self.checkequal('+123', '+123', 'zfill', 4)
923 self.checkequal('+0123', '+123', 'zfill', 5)
924 self.checkequal('-123', '-123', 'zfill', 3)
925 self.checkequal('-123', '-123', 'zfill', 4)
926 self.checkequal('-0123', '-123', 'zfill', 5)
927 self.checkequal('000', '', 'zfill', 3)
928 self.checkequal('34', '34', 'zfill', 1)
929 self.checkequal('0034', '34', 'zfill', 4)
931 self.checkraises(TypeError, '123', 'zfill')
933 def test_islower(self):
934 self.checkequal(False, '', 'islower')
935 self.checkequal(True, 'a', 'islower')
936 self.checkequal(False, 'A', 'islower')
937 self.checkequal(False, '\n', 'islower')
938 self.checkequal(True, 'abc', 'islower')
939 self.checkequal(False, 'aBc', 'islower')
940 self.checkequal(True, 'abc\n', 'islower')
941 self.checkraises(TypeError, 'abc', 'islower', 42)
943 def test_isupper(self):
944 self.checkequal(False, '', 'isupper')
945 self.checkequal(False, 'a', 'isupper')
946 self.checkequal(True, 'A', 'isupper')
947 self.checkequal(False, '\n', 'isupper')
948 self.checkequal(True, 'ABC', 'isupper')
949 self.checkequal(False, 'AbC', 'isupper')
950 self.checkequal(True, 'ABC\n', 'isupper')
951 self.checkraises(TypeError, 'abc', 'isupper', 42)
953 def test_istitle(self):
954 self.checkequal(False, '', 'istitle')
955 self.checkequal(False, 'a', 'istitle')
956 self.checkequal(True, 'A', 'istitle')
957 self.checkequal(False, '\n', 'istitle')
958 self.checkequal(True, 'A Titlecased Line', 'istitle')
959 self.checkequal(True, 'A\nTitlecased Line', 'istitle')
960 self.checkequal(True, 'A Titlecased, Line', 'istitle')
961 self.checkequal(False, 'Not a capitalized String', 'istitle')
962 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
963 self.checkequal(False, 'Not--a Titlecase String', 'istitle')
964 self.checkequal(False, 'NOT', 'istitle')
965 self.checkraises(TypeError, 'abc', 'istitle', 42)
967 def test_isspace(self):
968 self.checkequal(False, '', 'isspace')
969 self.checkequal(False, 'a', 'isspace')
970 self.checkequal(True, ' ', 'isspace')
971 self.checkequal(True, '\t', 'isspace')
972 self.checkequal(True, '\r', 'isspace')
973 self.checkequal(True, '\n', 'isspace')
974 self.checkequal(True, ' \t\r\n', 'isspace')
975 self.checkequal(False, ' \t\r\na', 'isspace')
976 self.checkraises(TypeError, 'abc', 'isspace', 42)
978 def test_isalpha(self):
979 self.checkequal(False, '', 'isalpha')
980 self.checkequal(True, 'a', 'isalpha')
981 self.checkequal(True, 'A', 'isalpha')
982 self.checkequal(False, '\n', 'isalpha')
983 self.checkequal(True, 'abc', 'isalpha')
984 self.checkequal(False, 'aBc123', 'isalpha')
985 self.checkequal(False, 'abc\n', 'isalpha')
986 self.checkraises(TypeError, 'abc', 'isalpha', 42)
988 def test_isalnum(self):
989 self.checkequal(False, '', 'isalnum')
990 self.checkequal(True, 'a', 'isalnum')
991 self.checkequal(True, 'A', 'isalnum')
992 self.checkequal(False, '\n', 'isalnum')
993 self.checkequal(True, '123abc456', 'isalnum')
994 self.checkequal(True, 'a1b3c', 'isalnum')
995 self.checkequal(False, 'aBc000 ', 'isalnum')
996 self.checkequal(False, 'abc\n', 'isalnum')
997 self.checkraises(TypeError, 'abc', 'isalnum', 42)
999 def test_isascii(self):
1000 self.checkequal(True, '', 'isascii')
1001 self.checkequal(True, '\x00', 'isascii')
1002 self.checkequal(True, '\x7f', 'isascii')
1003 self.checkequal(True, '\x00\x7f', 'isascii')
1004 self.checkequal(False, '\x80', 'isascii')
1005 self.checkequal(False, '\xe9', 'isascii')
1009 self.checkequal(True, ' '*p + '\x7f', 'isascii')
1010 self.checkequal(False, ' '*p + '\x80', 'isascii')
1011 self.checkequal(True, ' '*p + '\x7f' + ' '*8, 'isascii')
1012 self.checkequal(False, ' '*p + '\x80' + ' '*8, 'isascii')
1014 def test_isdigit(self):
1015 self.checkequal(False, '', 'isdigit')
1016 self.checkequal(False, 'a', 'isdigit')
1017 self.checkequal(True, '0', 'isdigit')
1018 self.checkequal(True, '0123456789', 'isdigit')
1019 self.checkequal(False, '0123456789a', 'isdigit')
1021 self.checkraises(TypeError, 'abc', 'isdigit', 42)
1023 def test_title(self):
1024 self.checkequal(' Hello ', ' hello ', 'title')
1025 self.checkequal('Hello ', 'hello ', 'title')
1026 self.checkequal('Hello ', 'Hello ', 'title')
1027 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
1028 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
1029 self.checkequal('Getint', "getInt", 'title')
1030 self.checkraises(TypeError, 'hello', 'title', 42)
1032 def test_splitlines(self):
1033 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
1034 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
1035 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
1036 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
1037 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
1038 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
1039 self.checkequal(['', 'abc', 'def', 'ghi', ''],
1041 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'],
1043 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r",
1045 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'],
1048 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
1055 def test_hash(self):
1057 a = self.type2test('DNSSEC')
1058 b = self.type2test('')
1062 self.assertEqual(hash(a), hash(b))
1064 def test_capitalize_nonascii(self):
1067 self.checkequal('\u1ffc\u1ff3\u1ff3\u1ff3',
1070 self.checkequal('\u24c5\u24e8\u24e3\u24d7\u24de\u24dd',
1072 self.checkequal('\u24c5\u24e8\u24e3\u24d7\u24de\u24dd',
1074 self.checkequal('\u2160\u2171\u2172',
1076 self.checkequal('\u2160\u2171\u2172',
1079 self.checkequal('\u019b\u1d00\u1d86\u0221\u1fb7',
1087 def test_startswith(self):
1088 self.checkequal(True, 'hello', 'startswith', 'he')
1089 self.checkequal(True, 'hello', 'startswith', 'hello')
1090 self.checkequal(False, 'hello', 'startswith', 'hello world')
1091 self.checkequal(True, 'hello', 'startswith', '')
1092 self.checkequal(False, 'hello', 'startswith', 'ello')
1093 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
1094 self.checkequal(True, 'hello', 'startswith', 'o', 4)
1095 self.checkequal(False, 'hello', 'startswith', 'o', 5)
1096 self.checkequal(True, 'hello', 'startswith', '', 5)
1097 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
1098 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
1099 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
1100 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
1101 self.checkequal(True, '', 'startswith', '', 0, 1)
1102 self.checkequal(True, '', 'startswith', '', 0, 0)
1103 self.checkequal(False, '', 'startswith', '', 1, 0)
1106 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
1107 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
1108 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
1109 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
1110 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
1111 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
1112 self.checkequal(False, 'hello', 'startswith', 'o', -2)
1113 self.checkequal(True, 'hello', 'startswith', 'o', -1)
1114 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
1115 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
1117 self.checkraises(TypeError, 'hello', 'startswith')
1118 self.checkraises(TypeError, 'hello', 'startswith', 42)
1121 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
1122 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
1123 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
1124 self.checkequal(False, 'hello', 'startswith', ())
1125 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
1127 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
1129 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
1130 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
1131 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
1133 self.checkraises(TypeError, 'hello', 'startswith', (42,))
1135 def test_endswith(self):
1136 self.checkequal(True, 'hello', 'endswith', 'lo')
1137 self.checkequal(False, 'hello', 'endswith', 'he')
1138 self.checkequal(True, 'hello', 'endswith', '')
1139 self.checkequal(False, 'hello', 'endswith', 'hello world')
1140 self.checkequal(False, 'helloworld', 'endswith', 'worl')
1141 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
1142 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
1143 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
1144 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
1145 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
1146 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
1147 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
1148 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
1149 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
1150 self.checkequal(True, '', 'endswith', '', 0, 1)
1151 self.checkequal(True, '', 'endswith', '', 0, 0)
1152 self.checkequal(False, '', 'endswith', '', 1, 0)
1155 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
1156 self.checkequal(False, 'hello', 'endswith', 'he', -2)
1157 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
1158 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
1159 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
1160 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
1161 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
1162 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
1163 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
1164 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
1165 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
1166 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
1167 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
1169 self.checkraises(TypeError, 'hello', 'endswith')
1170 self.checkraises(TypeError, 'hello', 'endswith', 42)
1173 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
1174 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
1175 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
1176 self.checkequal(False, 'hello', 'endswith', ())
1177 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
1179 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
1181 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
1182 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)
1183 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)
1185 self.checkraises(TypeError, 'hello', 'endswith', (42,))
1187 def test___contains__(self):
1188 self.checkequal(True, '', '__contains__', '')
1189 self.checkequal(True, 'abc', '__contains__', '')
1190 self.checkequal(False, 'abc', '__contains__', '\0')
1191 self.checkequal(True, '\0abc', '__contains__', '\0')
1192 self.checkequal(True, 'abc\0', '__contains__', '\0')
1193 self.checkequal(True, '\0abc', '__contains__', 'a')
1194 self.checkequal(True, 'asdf', '__contains__', 'asdf')
1195 self.checkequal(False, 'asd', '__contains__', 'asdf')
1196 self.checkequal(False, '', '__contains__', 'asdf')
1198 def test_subscript(self):
1199 self.checkequal('a', 'abc', '__getitem__', 0)
1200 self.checkequal('c', 'abc', '__getitem__', -1)
1201 self.checkequal('a', 'abc', '__getitem__', 0)
1202 self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
1203 self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
1204 self.checkequal('a', 'abc', '__getitem__', slice(0, 1))
1205 self.checkequal('', 'abc', '__getitem__', slice(0, 0))
1207 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
1211 self.checkraises(TypeError, 'abc', '__getitem__', idx_type, expected_msg=expected_msg)
1213 def test_slice(self):
1214 self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
1215 self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
1216 self.checkequal('ab', 'abc', '__getitem__', slice(0, 2))
1217 self.checkequal('bc', 'abc', '__getitem__', slice(1, 3))
1218 self.checkequal('b', 'abc', '__getitem__', slice(1, 2))
1219 self.checkequal('', 'abc', '__getitem__', slice(2, 2))
1220 self.checkequal('', 'abc', '__getitem__', slice(1000, 1000))
1221 self.checkequal('', 'abc', '__getitem__', slice(2000, 1000))
1222 self.checkequal('', 'abc', '__getitem__', slice(2, 1))
1224 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
1226 def test_extended_getslice(self):
1235 self.checkequal("".join(L), s, '__getitem__',
1238 def test_mul(self):
1239 self.checkequal('', 'abc', '__mul__', -1)
1240 self.checkequal('', 'abc', '__mul__', 0)
1241 self.checkequal('abc', 'abc', '__mul__', 1)
1242 self.checkequal('abcabcabc', 'abc', '__mul__', 3)
1243 self.checkraises(TypeError, 'abc', '__mul__')
1244 self.checkraises(TypeError, 'abc', '__mul__', '')
1247 #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
1249 def test_join(self):
1253 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
1254 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
1255 self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))
1256 self.checkequal('ac', '', 'join', ('a', '', 'c', ''))
1257 self.checkequal('w x y z', ' ', 'join', Sequence())
1258 self.checkequal('abc', 'a', 'join', ('abc',))
1259 self.checkequal('z', 'a', 'join', UserList(['z']))
1260 self.checkequal('a.b.c', '.', 'join', ['a', 'b', 'c'])
1261 self.assertRaises(TypeError, '.'.join, ['a', 'b', 3])
1263 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1265 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1268 #self.checkequal(str(BadSeq1()), ' ', 'join', BadSeq1())
1269 self.checkequal('a b c', ' ', 'join', BadSeq2())
1271 self.checkraises(TypeError, ' ', 'join')
1272 self.checkraises(TypeError, ' ', 'join', None)
1273 self.checkraises(TypeError, ' ', 'join', 7)
1274 self.checkraises(TypeError, ' ', 'join', [1, 2, bytes()])
1278 self.fixtype(' ').join(f())
1281 self.fail('join() ate exception message')
1283 self.fail('exception not raised')
1285 def test_formatting(self):
1286 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
1287 self.checkequal('+10+', '+%d+', '__mod__', 10)
1288 self.checkequal('a', "%c", '__mod__', "a")
1289 self.checkequal('a', "%c", '__mod__', "a")
1290 self.checkequal('"', "%c", '__mod__', 34)
1291 self.checkequal('$', "%c", '__mod__', 36)
1292 self.checkequal('10', "%d", '__mod__', 10)
1293 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
1297 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
1301 self.checkequal(' 42', '%3ld', '__mod__', 42)
1302 self.checkequal('42', '%d', '__mod__', 42.0)
1303 self.checkequal(slongvalue, '%d', '__mod__', longvalue)
1304 self.checkcall('%d', '__mod__', float(longvalue))
1305 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
1306 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
1308 self.checkraises(TypeError, 'abc', '__mod__')
1309 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
1310 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
1311 self.checkraises(TypeError, '%c', '__mod__', (None,))
1312 self.checkraises(ValueError, '%(foo', '__mod__', {})
1313 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
1314 self.checkraises(TypeError, '%d', '__mod__', "42") # not numeric
1315 self.checkraises(TypeError, '%d', '__mod__', (42+0j)) # no int conversion provided
1318 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
1321 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
1323 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
1324 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
1325 self.checkraises(ValueError, '%10', '__mod__', (42,))
1328 self.checkraises(ValueError, '%%%df' % (2**64), '__mod__', (3.2))
1329 self.checkraises(ValueError, '%%.%df' % (2**64), '__mod__', (3.2))
1330 self.checkraises(OverflowError, '%*s', '__mod__',
1332 self.checkraises(OverflowError, '%.*f', '__mod__',
1336 self.checkraises(TypeError, 'abc', '__mod__', X())
1339 def test_formatting_c_limits(self):
1342 self.checkraises(OverflowError, '%*s', '__mod__',
1344 self.checkraises(OverflowError, '%.*f', '__mod__',
1347 self.checkraises(OverflowError, '%*s', '__mod__',
1349 self.checkraises(OverflowError, '%.*f', '__mod__',
1352 def test_floatformatting(self):
1359 self.checkcall(format, "__mod__", value)
1361 def test_inplace_rewrites(self):
1363 self.checkequal('a', 'A', 'lower')
1364 self.checkequal(True, 'A', 'isupper')
1365 self.checkequal('A', 'a', 'upper')
1366 self.checkequal(True, 'a', 'islower')
1368 self.checkequal('a', 'A', 'replace', 'A', 'a')
1369 self.checkequal(True, 'A', 'isupper')
1371 self.checkequal('A', 'a', 'capitalize')
1372 self.checkequal(True, 'a', 'islower')
1374 self.checkequal('A', 'a', 'swapcase')
1375 self.checkequal(True, 'a', 'islower')
1377 self.checkequal('A', 'a', 'title')
1378 self.checkequal(True, 'a', 'islower')
1380 def test_partition(self):
1382 self.checkequal(('this is the par', 'ti', 'tion method'),
1387 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
1388 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
1389 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
1390 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
1392 self.checkraises(ValueError, S, 'partition', '')
1393 self.checkraises(TypeError, S, 'partition', None)
1395 def test_rpartition(self):
1397 self.checkequal(('this is the rparti', 'ti', 'on method'),
1402 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
1403 self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')
1404 self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')
1405 self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')
1407 self.checkraises(ValueError, S, 'rpartition', '')
1408 self.checkraises(TypeError, S, 'rpartition', None)
1410 def test_none_arguments(self):
1413 self.checkequal(2, s, 'find', 'l', None)
1414 self.checkequal(3, s, 'find', 'l', -2, None)
1415 self.checkequal(2, s, 'find', 'l', None, -2)
1416 self.checkequal(0, s, 'find', 'h', None, None)
1418 self.checkequal(3, s, 'rfind', 'l', None)
1419 self.checkequal(3, s, 'rfind', 'l', -2, None)
1420 self.checkequal(2, s, 'rfind', 'l', None, -2)
1421 self.checkequal(0, s, 'rfind', 'h', None, None)
1423 self.checkequal(2, s, 'index', 'l', None)
1424 self.checkequal(3, s, 'index', 'l', -2, None)
1425 self.checkequal(2, s, 'index', 'l', None, -2)
1426 self.checkequal(0, s, 'index', 'h', None, None)
1428 self.checkequal(3, s, 'rindex', 'l', None)
1429 self.checkequal(3, s, 'rindex', 'l', -2, None)
1430 self.checkequal(2, s, 'rindex', 'l', None, -2)
1431 self.checkequal(0, s, 'rindex', 'h', None, None)
1433 self.checkequal(2, s, 'count', 'l', None)
1434 self.checkequal(1, s, 'count', 'l', -2, None)
1435 self.checkequal(1, s, 'count', 'l', None, -2)
1436 self.checkequal(0, s, 'count', 'x', None, None)
1438 self.checkequal(True, s, 'endswith', 'o', None)
1439 self.checkequal(True, s, 'endswith', 'lo', -2, None)
1440 self.checkequal(True, s, 'endswith', 'l', None, -2)
1441 self.checkequal(False, s, 'endswith', 'x', None, None)
1443 self.checkequal(True, s, 'startswith', 'h', None)
1444 self.checkequal(True, s, 'startswith', 'l', -2, None)
1445 self.checkequal(True, s, 'startswith', 'h', None, -2)
1446 self.checkequal(False, s, 'startswith', 'x', None, None)
1448 def test_find_etc_raise_correct_error_messages(self):
1452 self.assertRaisesRegex(TypeError, r'^find\(', s.find,
1454 self.assertRaisesRegex(TypeError, r'^rfind\(', s.rfind,
1456 self.assertRaisesRegex(TypeError, r'^index\(', s.index,
1458 self.assertRaisesRegex(TypeError, r'^rindex\(', s.rindex,
1460 self.assertRaisesRegex(TypeError, r'^count\(', s.count,
1462 self.assertRaisesRegex(TypeError, r'^startswith\(', s.startswith,
1464 self.assertRaisesRegex(TypeError, r'^endswith\(', s.endswith,
1468 self.checkequal(10, "...\u043c......<", "find", "<")
1474 def test_bug1001011(self):
1479 t = self.type2test
1484 self.assertIsNot(s1, s2)
1485 self.assertIs(type(s2), t)
1489 self.assertIs(s1, s2)