Skip to content

gh-81005: Refactor str tests to reflect that str and unicode are merged in Python 3 #13172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 23, 2023
26 changes: 9 additions & 17 deletions Lib/test/string_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,20 @@
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
# specific to character interpretation, used for bytes objects
# 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
Expand All @@ -36,7 +30,7 @@ class BaseTest:
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):
Expand Down Expand Up @@ -1096,7 +1090,7 @@ def test_splitlines(self):
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.

Expand Down Expand Up @@ -1127,11 +1121,6 @@ def test_capitalize_nonascii(self):
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')
Expand Down Expand Up @@ -1313,8 +1302,11 @@ def test_join(self):
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)
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,7 @@ def test_setattr(self):
msg = r"^attribute name must be string, not 'int'$"
self.assertRaisesRegex(TypeError, msg, setattr, sys, 1, 'spam')

# test_str(): see test_unicode.py and test_bytes.py for str() tests.
# test_str(): see test_str.py and test_bytes.py for str() tests.

def test_sum(self):
self.assertEqual(sum([]), 0)
Expand Down
37 changes: 18 additions & 19 deletions Lib/test/test_unicode.py → Lib/test/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def duplicate_string(text):
class StrSubclass(str):
pass

class UnicodeTest(string_tests.CommonTest,
string_tests.MixinStrUnicodeUserStringTest,
class StrTest(string_tests.StringLikeTest,
string_tests.MixinStrUnicodeTest,
unittest.TestCase):

Expand Down Expand Up @@ -213,7 +212,7 @@ def test_pickle_iterator(self):
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')
Expand Down Expand Up @@ -243,7 +242,7 @@ class MyStr(str):
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')
Expand Down Expand Up @@ -288,7 +287,7 @@ def test_find(self):
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')
Expand Down Expand Up @@ -329,7 +328,7 @@ def test_rfind(self):
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')
Expand All @@ -353,7 +352,7 @@ def test_index(self):
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')
Expand Down Expand Up @@ -449,7 +448,7 @@ def test_maketrans_translate(self):
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'):
Expand All @@ -466,7 +465,7 @@ def test_split(self):
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
Expand All @@ -486,7 +485,7 @@ def test_rsplit(self):
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'):
Expand All @@ -503,7 +502,7 @@ def test_partition(self):
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'):
Expand All @@ -520,7 +519,7 @@ def test_rpartition(self):
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
Expand All @@ -547,7 +546,7 @@ def test_join_overflow(self):
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)
Expand Down Expand Up @@ -866,7 +865,7 @@ def test_surrogates(self):


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')
Expand Down Expand Up @@ -897,7 +896,7 @@ def test_casefold(self):
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')
Expand All @@ -914,7 +913,7 @@ def test_upper(self):
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')
Expand Down Expand Up @@ -948,7 +947,7 @@ def test_title(self):
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(),
Expand All @@ -974,7 +973,7 @@ def test_swapcase(self):
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'),
Expand Down Expand Up @@ -1475,7 +1474,7 @@ def __format__(self, spec):
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')
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_userstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from collections import UserString

class UserStringTest(
string_tests.CommonTest,
string_tests.MixinStrUnicodeUserStringTest,
string_tests.StringLikeTest,
unittest.TestCase
):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
String tests are modified to reflect that ``str`` and ``unicode`` are merged
in Python 3. Patch by Daniel Fortunov.