From 6d9900cebbcd2e0a773495fe7d2a0f92e1dbecaa Mon Sep 17 00:00:00 2001 From: Daniel Fortunov Date: Tue, 7 May 2019 14:44:14 -0400 Subject: [PATCH 1/8] - Rename test_unicode.py to test_str.py, and `UnicodeTest` to `StrTest` (to reflect the type that is now being tested) --- Lib/test/test_builtin.py | 2 +- Lib/test/{test_unicode.py => test_str.py} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename Lib/test/{test_unicode.py => test_str.py} (99%) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 5674ea89b1c408..f4eaaeaa24281c 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -1287,7 +1287,7 @@ def test_setattr(self): self.assertRaises(TypeError, setattr, sys, 1, 'spam') self.assertRaises(TypeError, setattr) - # 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) diff --git a/Lib/test/test_unicode.py b/Lib/test/test_str.py similarity index 99% rename from Lib/test/test_unicode.py rename to Lib/test/test_str.py index 36b72e40c7e419..1c537022a44da1 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_str.py @@ -46,7 +46,7 @@ def duplicate_string(text): class StrSubclass(str): pass -class UnicodeTest(string_tests.CommonTest, +class StrTest(string_tests.CommonTest, string_tests.MixinStrUnicodeUserStringTest, string_tests.MixinStrUnicodeTest, unittest.TestCase): From 68bc3f9efbfcd341a71540703b97bff06fc5dea1 Mon Sep 17 00:00:00 2001 From: Daniel Fortunov Date: Tue, 7 May 2019 14:57:32 -0400 Subject: [PATCH 2/8] - Move `MixinStrUnicodeUserStringTest` tests into the `CommonTest` class (The comment for this mixin class says # additional tests that only work for # stringlike objects, i.e. str, UserString but in the absence of `unicode` the `CommonTest` class is also only used for these two types now, so no need for the distinction.) --- Lib/test/string_tests.py | 5 ----- Lib/test/test_str.py | 9 ++++----- Lib/test/test_userstring.py | 1 - 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 836a43b81dd6f0..606ad2d770c12a 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -992,11 +992,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') diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py index 1c537022a44da1..0d6cb3595b5712 100644 --- a/Lib/test/test_str.py +++ b/Lib/test/test_str.py @@ -47,7 +47,6 @@ class StrSubclass(str): pass class StrTest(string_tests.CommonTest, - string_tests.MixinStrUnicodeUserStringTest, string_tests.MixinStrUnicodeTest, unittest.TestCase): @@ -411,7 +410,7 @@ def test_rsplit(self): left + delim * 2 + right, 'rsplit', delim *2) def test_partition(self): - string_tests.MixinStrUnicodeUserStringTest.test_partition(self) + string_tests.CommonTest.test_partition(self) # test mixed kinds self.checkequal(('ABCDEFGH', '', ''), 'ABCDEFGH', 'partition', '\u4200') for left, right in ('ba', '\u0101\u0100', '\U00010301\U00010300'): @@ -428,7 +427,7 @@ def test_partition(self): left + delim * 2 + right, 'partition', delim * 2) def test_rpartition(self): - string_tests.MixinStrUnicodeUserStringTest.test_rpartition(self) + string_tests.CommonTest.test_rpartition(self) # test mixed kinds self.checkequal(('', '', 'ABCDEFGH'), 'ABCDEFGH', 'rpartition', '\u4200') for left, right in ('ba', '\u0101\u0100', '\U00010301\U00010300'): @@ -445,7 +444,7 @@ def test_rpartition(self): left + delim * 2 + right, 'rpartition', delim * 2) def test_join(self): - string_tests.MixinStrUnicodeUserStringTest.test_join(self) + string_tests.CommonTest.test_join(self) class MyWrapper: def __init__(self, sval): self.sval = sval @@ -1336,7 +1335,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.CommonTest.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') diff --git a/Lib/test/test_userstring.py b/Lib/test/test_userstring.py index 71528223d35bb5..0c04fcfbac70f1 100644 --- a/Lib/test/test_userstring.py +++ b/Lib/test/test_userstring.py @@ -8,7 +8,6 @@ class UserStringTest( string_tests.CommonTest, - string_tests.MixinStrUnicodeUserStringTest, unittest.TestCase ): From 731890e0bcb282a6781d6861c81462cc516d9faa Mon Sep 17 00:00:00 2001 From: Daniel Fortunov Date: Tue, 7 May 2019 15:25:05 -0400 Subject: [PATCH 3/8] - Rename `CommonTest` to `StringLikeTest`, in order to better communicate its purpose, and to disambiguate it from the more general `BaseTest` class, which covers behaviours shared with the `bytes` and `bytesarray` types. --- Lib/test/string_tests.py | 2 +- Lib/test/test_str.py | 36 ++++++++++++++++++------------------ Lib/test/test_userstring.py | 2 +- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 606ad2d770c12a..4b5df65ba2f4d9 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -961,7 +961,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. diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py index 0d6cb3595b5712..92fb4d9d14716d 100644 --- a/Lib/test/test_str.py +++ b/Lib/test/test_str.py @@ -46,7 +46,7 @@ def duplicate_string(text): class StrSubclass(str): pass -class StrTest(string_tests.CommonTest, +class StrTest(string_tests.StringLikeTest, string_tests.MixinStrUnicodeTest, unittest.TestCase): @@ -172,7 +172,7 @@ def test_iterators(self): self.assertRaises(StopIteration, next, it) 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') @@ -198,7 +198,7 @@ def test_count(self): self.checkequal(0, '\u0102' * 10, 'count', '\u0102\U00100304') 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') @@ -229,7 +229,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') @@ -257,7 +257,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') @@ -281,7 +281,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') @@ -377,7 +377,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'): @@ -394,7 +394,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 @@ -410,7 +410,7 @@ def test_rsplit(self): left + delim * 2 + right, 'rsplit', delim *2) def test_partition(self): - string_tests.CommonTest.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'): @@ -427,7 +427,7 @@ def test_partition(self): left + delim * 2 + right, 'partition', delim * 2) def test_rpartition(self): - string_tests.CommonTest.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'): @@ -444,7 +444,7 @@ def test_rpartition(self): left + delim * 2 + right, 'rpartition', delim * 2) def test_join(self): - string_tests.CommonTest.test_join(self) + string_tests.StringLikeTest.test_join(self) class MyWrapper: def __init__(self, sval): self.sval = sval @@ -471,7 +471,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) @@ -749,7 +749,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') @@ -780,7 +780,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') @@ -797,7 +797,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') @@ -831,7 +831,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(), @@ -857,7 +857,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'), @@ -1335,7 +1335,7 @@ def __format__(self, spec): self.assertEqual('{f:{}}{}{g}'.format(2, 4, f=1, g='g'), ' 14g') def test_formatting(self): - string_tests.CommonTest.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') diff --git a/Lib/test/test_userstring.py b/Lib/test/test_userstring.py index 0c04fcfbac70f1..2fe58f53e515f8 100644 --- a/Lib/test/test_userstring.py +++ b/Lib/test/test_userstring.py @@ -7,7 +7,7 @@ from collections import UserString class UserStringTest( - string_tests.CommonTest, + string_tests.StringLikeTest, unittest.TestCase ): From e8a401f5002f228522e63e5f3bd55ebcb50bb244 Mon Sep 17 00:00:00 2001 From: Daniel Fortunov Date: Tue, 7 May 2019 15:28:54 -0400 Subject: [PATCH 4/8] - Fix comments which refer to fixtesttype() when they mean fixtype() (Seems to date back to the first introduction of this code, in 2003!) --- Lib/test/string_tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 4b5df65ba2f4d9..aa23619d52fb2f 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -25,7 +25,7 @@ class BaseTest: # 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 @@ -34,7 +34,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): From 76c4542b2a23852ffd8681e1f8a4f091d7fc42ff Mon Sep 17 00:00:00 2001 From: Daniel Fortunov Date: Tue, 7 May 2019 15:44:10 -0400 Subject: [PATCH 5/8] - Reinstate check that str.join() raises TypeError when given input with non-str types. Back in 2007 there was a change to allow str.join() to convert non-str values automatically, but this is no longer the case. --- Lib/test/string_tests.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index aa23619d52fb2f..92e0d50c03f50e 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -13,7 +13,6 @@ 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'] @@ -1169,7 +1168,7 @@ def test_join(self): self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join', ('a' * i,) * i) - #self.checkequal(str(BadSeq1()), ' ', 'join', BadSeq1()) + self.checkraises(TypeError, ' ', 'join', BadSeq1()) self.checkequal('a b c', ' ', 'join', BadSeq2()) self.checkraises(TypeError, ' ', 'join') From e0e892ed26fa5e2ee11568b2c6fdbf73bc862724 Mon Sep 17 00:00:00 2001 From: Daniel Fortunov Date: Tue, 7 May 2019 15:58:15 -0400 Subject: [PATCH 6/8] - Actually, there is already coverage for a list containing non-string raising TypeError, so BadSeq1 and the associated check seem to be redundant. This is the existing check, in test_join(): self.assertRaises(TypeError, '.'.join, ['a', 'b', 3]) (Unless there is some specific difference we're testing by supplying a custom sequence, BadSeq1, rather than a plain list?) - Define BadSeq2 closer to its only usage, and give it a more meaningful name. (Previously this type was referenced from multiple locations but this is no longer the case.) --- Lib/test/string_tests.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 92e0d50c03f50e..8fa125ef0fed66 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -6,17 +6,12 @@ from test import support from collections import UserList + 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] - -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 @@ -1168,8 +1163,11 @@ def test_join(self): self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join', ('a' * i,) * i) - self.checkraises(TypeError, ' ', '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) From bfb4e8af9d04950c255e9fecfb0801c80a41292c Mon Sep 17 00:00:00 2001 From: Daniel Fortunov Date: Fri, 19 May 2023 08:06:14 +0100 Subject: [PATCH 7/8] Add NEWS entry --- .../next/Tests/2023-05-19-08-06-06.gh-issue-13172.-q7m9W.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Tests/2023-05-19-08-06-06.gh-issue-13172.-q7m9W.rst diff --git a/Misc/NEWS.d/next/Tests/2023-05-19-08-06-06.gh-issue-13172.-q7m9W.rst b/Misc/NEWS.d/next/Tests/2023-05-19-08-06-06.gh-issue-13172.-q7m9W.rst new file mode 100644 index 00000000000000..dfb653241e2607 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2023-05-19-08-06-06.gh-issue-13172.-q7m9W.rst @@ -0,0 +1,2 @@ +String tests are modified to reflect that ``str`` and ``unicode`` are merged +in Python 3. Patch by Daniel Fortunov. From 130ece2cbb11410a39312fc7c8726dd967a2e701 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Fri, 19 May 2023 10:50:38 +0300 Subject: [PATCH 8/8] Rename NEWS file --- ...2.-q7m9W.rst => 2023-05-19-08-06-06.gh-issue-81005.-q7m9W.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/Tests/{2023-05-19-08-06-06.gh-issue-13172.-q7m9W.rst => 2023-05-19-08-06-06.gh-issue-81005.-q7m9W.rst} (100%) diff --git a/Misc/NEWS.d/next/Tests/2023-05-19-08-06-06.gh-issue-13172.-q7m9W.rst b/Misc/NEWS.d/next/Tests/2023-05-19-08-06-06.gh-issue-81005.-q7m9W.rst similarity index 100% rename from Misc/NEWS.d/next/Tests/2023-05-19-08-06-06.gh-issue-13172.-q7m9W.rst rename to Misc/NEWS.d/next/Tests/2023-05-19-08-06-06.gh-issue-81005.-q7m9W.rst