from collections import UserList
import random
+
class Sequence:
def __init__(self, seq='wxyz'): self.seq = seq
def __len__(self): return len(self.seq)
def __getitem__(self, i): return self.seq[i]
-class BadSeq1(Sequence):
- def __init__(self): self.seq = [7, 'hello', 123]
- def __str__(self): return '{0} {1} {2}'.format(*self.seq)
-
-class BadSeq2(Sequence):
- def __init__(self): self.seq = ['a', 'b', 'c']
- def __len__(self): return 8
class BaseTest:
# These tests are for buffers of values (bytes) and not
# and various string implementations
# The type to be tested
- # Change in subclasses to change the behaviour of fixtesttype()
+ # Change in subclasses to change the behaviour of fixtype()
type2test = None
# Whether the "contained items" of the container are integers in
contains_bytes = False
# All tests pass their arguments to the testing methods
- # as str objects. fixtesttype() can be used to propagate
+ # as str objects. fixtype() can be used to propagate
# these arguments to the appropriate type
def fixtype(self, obj):
if isinstance(obj, str):
self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
-class CommonTest(BaseTest):
+class StringLikeTest(BaseTest):
# This testcase contains tests that can be used in all
# stringlike classes. Currently this is str and UserString.
self.checkequal('\u019b\u1d00\u1d86\u0221\u1fb7',
'\u019b\u1d00\u1d86\u0221\u1fb7', 'capitalize')
-
-class MixinStrUnicodeUserStringTest:
- # additional tests that only work for
- # stringlike objects, i.e. str, UserString
-
def test_startswith(self):
self.checkequal(True, 'hello', 'startswith', 'he')
self.checkequal(True, 'hello', 'startswith', 'hello')
self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
('a' * i,) * i)
- #self.checkequal(str(BadSeq1()), ' ', 'join', BadSeq1())
- self.checkequal('a b c', ' ', 'join', BadSeq2())
+ class LiesAboutLengthSeq(Sequence):
+ def __init__(self): self.seq = ['a', 'b', 'c']
+ def __len__(self): return 8
+
+ self.checkequal('a b c', ' ', 'join', LiesAboutLengthSeq())
self.checkraises(TypeError, ' ', 'join')
self.checkraises(TypeError, ' ', 'join', None)
class StrSubclass(str):
pass
-class UnicodeTest(string_tests.CommonTest,
- string_tests.MixinStrUnicodeUserStringTest,
+class StrTest(string_tests.StringLikeTest,
string_tests.MixinStrUnicodeTest,
unittest.TestCase):
self.assertEqual(case, pickled)
def test_count(self):
- string_tests.CommonTest.test_count(self)
+ string_tests.StringLikeTest.test_count(self)
# check mixed argument types
self.checkequalnofix(3, 'aaa', 'count', 'a')
self.checkequalnofix(0, 'aaa', 'count', 'b')
self.checkequal(3, MyStr('aaa'), 'count', 'a')
def test_find(self):
- string_tests.CommonTest.test_find(self)
+ string_tests.StringLikeTest.test_find(self)
# test implementation details of the memchr fast path
self.checkequal(100, 'a' * 100 + '\u0102', 'find', '\u0102')
self.checkequal(-1, 'a' * 100 + '\u0102', 'find', '\u0201')
self.checkequal(-1, '\u0102' * 100, 'find', '\u0102\U00100304')
def test_rfind(self):
- string_tests.CommonTest.test_rfind(self)
+ string_tests.StringLikeTest.test_rfind(self)
# test implementation details of the memrchr fast path
self.checkequal(0, '\u0102' + 'a' * 100 , 'rfind', '\u0102')
self.checkequal(-1, '\u0102' + 'a' * 100 , 'rfind', '\u0201')
self.checkequal(-1, '\u0102' * 100, 'rfind', '\U00100304\u0102')
def test_index(self):
- string_tests.CommonTest.test_index(self)
+ string_tests.StringLikeTest.test_index(self)
self.checkequalnofix(0, 'abcdefghiabc', 'index', '')
self.checkequalnofix(3, 'abcdefghiabc', 'index', 'def')
self.checkequalnofix(0, 'abcdefghiabc', 'index', 'abc')
self.assertRaises(ValueError, ('\u0102' * 100).index, '\u0102\U00100304')
def test_rindex(self):
- string_tests.CommonTest.test_rindex(self)
+ string_tests.StringLikeTest.test_rindex(self)
self.checkequalnofix(12, 'abcdefghiabc', 'rindex', '')
self.checkequalnofix(3, 'abcdefghiabc', 'rindex', 'def')
self.checkequalnofix(9, 'abcdefghiabc', 'rindex', 'abc')
self.assertRaises(TypeError, 'abababc'.translate, 'abc', 'xyz')
def test_split(self):
- string_tests.CommonTest.test_split(self)
+ string_tests.StringLikeTest.test_split(self)
# test mixed kinds
for left, right in ('ba', '\u0101\u0100', '\U00010301\U00010300'):
left + delim * 2 + right, 'split', delim *2)
def test_rsplit(self):
- string_tests.CommonTest.test_rsplit(self)
+ string_tests.StringLikeTest.test_rsplit(self)
# test mixed kinds
for left, right in ('ba', 'юё', '\u0101\u0100', '\U00010301\U00010300'):
left *= 9
left + right, 'rsplit', None)
def test_partition(self):
- string_tests.MixinStrUnicodeUserStringTest.test_partition(self)
+ string_tests.StringLikeTest.test_partition(self)
# test mixed kinds
self.checkequal(('ABCDEFGH', '', ''), 'ABCDEFGH', 'partition', '\u4200')
for left, right in ('ba', '\u0101\u0100', '\U00010301\U00010300'):
left + delim * 2 + right, 'partition', delim * 2)
def test_rpartition(self):
- string_tests.MixinStrUnicodeUserStringTest.test_rpartition(self)
+ string_tests.StringLikeTest.test_rpartition(self)
# test mixed kinds
self.checkequal(('', '', 'ABCDEFGH'), 'ABCDEFGH', 'rpartition', '\u4200')
for left, right in ('ba', '\u0101\u0100', '\U00010301\U00010300'):
left + delim * 2 + right, 'rpartition', delim * 2)
def test_join(self):
- string_tests.MixinStrUnicodeUserStringTest.test_join(self)
+ string_tests.StringLikeTest.test_join(self)
class MyWrapper:
def __init__(self, sval): self.sval = sval
self.assertRaises(OverflowError, ''.join, seq)
def test_replace(self):
- string_tests.CommonTest.test_replace(self)
+ string_tests.StringLikeTest.test_replace(self)
# method call forwarded from str implementation because of unicode argument
self.checkequalnofix('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
def test_lower(self):
- string_tests.CommonTest.test_lower(self)
+ string_tests.StringLikeTest.test_lower(self)
self.assertEqual('\U00010427'.lower(), '\U0001044F')
self.assertEqual('\U00010427\U00010427'.lower(),
'\U0001044F\U0001044F')
self.assertEqual('\u00b5'.casefold(), '\u03bc')
def test_upper(self):
- string_tests.CommonTest.test_upper(self)
+ string_tests.StringLikeTest.test_upper(self)
self.assertEqual('\U0001044F'.upper(), '\U00010427')
self.assertEqual('\U0001044F\U0001044F'.upper(),
'\U00010427\U00010427')
self.assertEqual('\u2177'.upper(), '\u2167')
def test_capitalize(self):
- string_tests.CommonTest.test_capitalize(self)
+ string_tests.StringLikeTest.test_capitalize(self)
self.assertEqual('\U0001044F'.capitalize(), '\U00010427')
self.assertEqual('\U0001044F\U0001044F'.capitalize(),
'\U00010427\U0001044F')
self.assertEqual('A\u03a3A'.title(), 'A\u03c3a')
def test_swapcase(self):
- string_tests.CommonTest.test_swapcase(self)
+ string_tests.StringLikeTest.test_swapcase(self)
self.assertEqual('\U0001044F'.swapcase(), '\U00010427')
self.assertEqual('\U00010427'.swapcase(), '\U0001044F')
self.assertEqual('\U0001044F\U0001044F'.swapcase(),
self.assertEqual('\u1fd2'.swapcase(), '\u0399\u0308\u0300')
def test_center(self):
- string_tests.CommonTest.test_center(self)
+ string_tests.StringLikeTest.test_center(self)
self.assertEqual('x'.center(2, '\U0010FFFF'),
'x\U0010FFFF')
self.assertEqual('x'.center(3, '\U0010FFFF'),
self.assertEqual('{f:{}}{}{g}'.format(2, 4, f=1, g='g'), ' 14g')
def test_formatting(self):
- string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
+ string_tests.StringLikeTest.test_formatting(self)
# Testing Unicode formatting strings...
self.assertEqual("%s, %s" % ("abc", "abc"), 'abc, abc')
self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, 2, 3), 'abc, abc, 1, 2.000000, 3.00')