summaryrefslogtreecommitdiffstats
path: root/Lib/test/string_tests.py
diff options
context:
space:
mode:
authorsweeneyde <36520290+sweeneyde@users.noreply.github.com>2020-04-22 21:05:48 (GMT)
committerGitHub <noreply@github.com>2020-04-22 21:05:48 (GMT)
commita81849b0315277bb3937271174aaaa5059c0b445 (patch)
tree8184e6ab012aa217b5cc1bb242efc6aed531db7d /Lib/test/string_tests.py
parent39652cd8bdf7c82b7c6055089a4ed90ee546a448 (diff)
downloadcpython-a81849b0315277bb3937271174aaaa5059c0b445.zip
cpython-a81849b0315277bb3937271174aaaa5059c0b445.tar.gz
cpython-a81849b0315277bb3937271174aaaa5059c0b445.tar.bz2
bpo-39939: Add str.removeprefix and str.removesuffix (GH-18939)
Added str.removeprefix and str.removesuffix methods and corresponding bytes, bytearray, and collections.UserString methods to remove affixes from a string if present. See PEP 616 for a full description.
Diffstat (limited to 'Lib/test/string_tests.py')
-rw-r--r--Lib/test/string_tests.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index 948e2a3..527f505 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -682,6 +682,42 @@ class BaseTest:
self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
+ def test_removeprefix(self):
+ self.checkequal('am', 'spam', 'removeprefix', 'sp')
+ self.checkequal('spamspam', 'spamspamspam', 'removeprefix', 'spam')
+ self.checkequal('spam', 'spam', 'removeprefix', 'python')
+ self.checkequal('spam', 'spam', 'removeprefix', 'spider')
+ self.checkequal('spam', 'spam', 'removeprefix', 'spam and eggs')
+
+ self.checkequal('', '', 'removeprefix', '')
+ self.checkequal('', '', 'removeprefix', 'abcde')
+ self.checkequal('abcde', 'abcde', 'removeprefix', '')
+ self.checkequal('', 'abcde', 'removeprefix', 'abcde')
+
+ self.checkraises(TypeError, 'hello', 'removeprefix')
+ self.checkraises(TypeError, 'hello', 'removeprefix', 42)
+ self.checkraises(TypeError, 'hello', 'removeprefix', 42, 'h')
+ self.checkraises(TypeError, 'hello', 'removeprefix', 'h', 42)
+ self.checkraises(TypeError, 'hello', 'removeprefix', ("he", "l"))
+
+ def test_removesuffix(self):
+ self.checkequal('sp', 'spam', 'removesuffix', 'am')
+ self.checkequal('spamspam', 'spamspamspam', 'removesuffix', 'spam')
+ self.checkequal('spam', 'spam', 'removesuffix', 'python')
+ self.checkequal('spam', 'spam', 'removesuffix', 'blam')
+ self.checkequal('spam', 'spam', 'removesuffix', 'eggs and spam')
+
+ self.checkequal('', '', 'removesuffix', '')
+ self.checkequal('', '', 'removesuffix', 'abcde')
+ self.checkequal('abcde', 'abcde', 'removesuffix', '')
+ self.checkequal('', 'abcde', 'removesuffix', 'abcde')
+
+ self.checkraises(TypeError, 'hello', 'removesuffix')
+ self.checkraises(TypeError, 'hello', 'removesuffix', 42)
+ self.checkraises(TypeError, 'hello', 'removesuffix', 42, 'h')
+ self.checkraises(TypeError, 'hello', 'removesuffix', 'h', 42)
+ self.checkraises(TypeError, 'hello', 'removesuffix', ("lo", "l"))
+
def test_capitalize(self):
self.checkequal(' hello ', ' hello ', 'capitalize')
self.checkequal('Hello ', 'Hello ','capitalize')