diff options
Diffstat (limited to 'Lib/test/string_tests.py')
-rw-r--r-- | Lib/test/string_tests.py | 87 |
1 files changed, 77 insertions, 10 deletions
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 6792179..385b039 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -20,7 +20,7 @@ class BadSeq2(Sequence): def __init__(self): self.seq = ['a', 'b', 'c'] def __len__(self): return 8 -class BaseTest(unittest.TestCase): +class BaseTest: # These tests are for buffers of values (bytes) and not # specific to character interpretation, used for bytes objects # and various string implementations @@ -29,6 +29,11 @@ class BaseTest(unittest.TestCase): # Change in subclasses to change the behaviour of fixtesttype() type2test = None + # Whether the "contained items" of the container are integers in + # range(0, 256) (i.e. bytes, bytearray) or strings of length 1 + # (str) + contains_bytes = False + # All tests pass their arguments to the testing methods # as str objects. fixtesttype() can be used to propagate # these arguments to the appropriate type @@ -48,11 +53,12 @@ class BaseTest(unittest.TestCase): return obj # check that obj.method(*args) returns result - def checkequal(self, result, obj, methodname, *args): + def checkequal(self, result, obj, methodname, *args, **kwargs): result = self.fixtype(result) obj = self.fixtype(obj) args = self.fixtype(args) - realresult = getattr(obj, methodname)(*args) + kwargs = {k: self.fixtype(v) for k,v in kwargs.items()} + realresult = getattr(obj, methodname)(*args, **kwargs) self.assertEqual( result, realresult @@ -117,7 +123,11 @@ class BaseTest(unittest.TestCase): self.checkequal(0, '', 'count', 'xx', sys.maxsize, 0) self.checkraises(TypeError, 'hello', 'count') - self.checkraises(TypeError, 'hello', 'count', 42) + + if self.contains_bytes: + self.checkequal(0, 'hello', 'count', 42) + else: + self.checkraises(TypeError, 'hello', 'count', 42) # For a variety of combinations, # verify that str.count() matches an equivalent function @@ -163,7 +173,11 @@ class BaseTest(unittest.TestCase): self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a', None, 6) self.checkraises(TypeError, 'hello', 'find') - self.checkraises(TypeError, 'hello', 'find', 42) + + if self.contains_bytes: + self.checkequal(-1, 'hello', 'find', 42) + else: + self.checkraises(TypeError, 'hello', 'find', 42) self.checkequal(0, '', 'find', '') self.checkequal(-1, '', 'find', '', 1, 1) @@ -217,7 +231,11 @@ class BaseTest(unittest.TestCase): self.checkequal( 2, 'rrarrrrrrrrra', 'rfind', 'a', None, 6) self.checkraises(TypeError, 'hello', 'rfind') - self.checkraises(TypeError, 'hello', 'rfind', 42) + + if self.contains_bytes: + self.checkequal(-1, 'hello', 'rfind', 42) + else: + self.checkraises(TypeError, 'hello', 'rfind', 42) # For a variety of combinations, # verify that str.rfind() matches __contains__ @@ -245,6 +263,9 @@ class BaseTest(unittest.TestCase): # issue 7458 self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0) + # issue #15534 + self.checkequal(0, '<......\u043c...', "rfind", "<") + def test_index(self): self.checkequal(0, 'abcdefghiabc', 'index', '') self.checkequal(3, 'abcdefghiabc', 'index', 'def') @@ -264,7 +285,11 @@ class BaseTest(unittest.TestCase): self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6) self.checkraises(TypeError, 'hello', 'index') - self.checkraises(TypeError, 'hello', 'index', 42) + + if self.contains_bytes: + self.checkraises(ValueError, 'hello', 'index', 42) + else: + self.checkraises(TypeError, 'hello', 'index', 42) def test_rindex(self): self.checkequal(12, 'abcdefghiabc', 'rindex', '') @@ -286,7 +311,11 @@ class BaseTest(unittest.TestCase): self.checkequal( 2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6) self.checkraises(TypeError, 'hello', 'rindex') - self.checkraises(TypeError, 'hello', 'rindex', 42) + + if self.contains_bytes: + self.checkraises(ValueError, 'hello', 'rindex', 42) + else: + self.checkraises(TypeError, 'hello', 'rindex', 42) def test_lower(self): self.checkequal('hello', 'HeLLo', 'lower') @@ -364,6 +393,17 @@ class BaseTest(unittest.TestCase): self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4], 'split', 'BLAH', 18) + # with keyword args + self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', sep='|') + self.checkequal(['a', 'b|c|d'], + 'a|b|c|d', 'split', '|', maxsplit=1) + self.checkequal(['a', 'b|c|d'], + 'a|b|c|d', 'split', sep='|', maxsplit=1) + self.checkequal(['a', 'b|c|d'], + 'a|b|c|d', 'split', maxsplit=1, sep='|') + self.checkequal(['a', 'b c d'], + 'a b c d', 'split', maxsplit=1) + # argument type self.checkraises(TypeError, 'hello', 'split', 42, 42, 42) @@ -421,6 +461,17 @@ class BaseTest(unittest.TestCase): self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 18) + # with keyword args + self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', sep='|') + self.checkequal(['a|b|c', 'd'], + 'a|b|c|d', 'rsplit', '|', maxsplit=1) + self.checkequal(['a|b|c', 'd'], + 'a|b|c|d', 'rsplit', sep='|', maxsplit=1) + self.checkequal(['a|b|c', 'd'], + 'a|b|c|d', 'rsplit', maxsplit=1, sep='|') + self.checkequal(['a b c', 'd'], + 'a b c d', 'rsplit', maxsplit=1) + # argument type self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42) @@ -550,6 +601,8 @@ class BaseTest(unittest.TestCase): EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1) EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0) EQ("A----B----C----", "A.B.C.", "replace", ".", "----") + # issue #15534 + EQ('...\u043c......<', '...\u043c......<', "replace", "<", "<") EQ("Reykjavik", "Reykjavik", "replace", "q", "KK") @@ -644,7 +697,7 @@ class CommonTest(BaseTest): # check that titlecased chars are lowered correctly # \u1ffc is the titlecased char - self.checkequal('\u1ffc\u1ff3\u1ff3\u1ff3', + self.checkequal('\u03a9\u0399\u1ff3\u1ff3\u1ff3', '\u1ff3\u1ff3\u1ffc\u1ffc', 'capitalize') # check with cased non-letter chars self.checkequal('\u24c5\u24e8\u24e3\u24d7\u24de\u24dd', @@ -909,7 +962,14 @@ class MixinStrUnicodeUserStringTest: self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines') self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines') self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines') - self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1) + self.checkequal(['', 'abc', 'def', 'ghi', ''], + "\nabc\ndef\r\nghi\n\r", 'splitlines', False) + self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], + "\nabc\ndef\r\nghi\n\r", 'splitlines', True) + self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", + 'splitlines', keepends=False) + self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], + "\nabc\ndef\r\nghi\n\r", 'splitlines', keepends=True) self.checkraises(TypeError, 'abc', 'splitlines', 42, 42) @@ -1143,6 +1203,10 @@ class MixinStrUnicodeUserStringTest: self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.)) self.checkraises(ValueError, '%10', '__mod__', (42,)) + # Outrageously large width or precision should raise ValueError. + self.checkraises(ValueError, '%%%df' % (2**64), '__mod__', (3.2)) + self.checkraises(ValueError, '%%.%df' % (2**64), '__mod__', (3.2)) + self.checkraises(OverflowError, '%*s', '__mod__', (_testcapi.PY_SSIZE_T_MAX + 1, '')) self.checkraises(OverflowError, '%.*f', '__mod__', @@ -1271,6 +1335,9 @@ class MixinStrUnicodeUserStringTest: self.assertRaisesRegex(TypeError, r'^endswith\(', s.endswith, x, None, None, None) + # issue #15534 + self.checkequal(10, "...\u043c......<", "find", "<") + class MixinStrUnicodeTest: # Additional tests that only work with str and unicode. |