diff options
Diffstat (limited to 'Lib/test/string_tests.py')
| -rw-r--r-- | Lib/test/string_tests.py | 51 | 
1 files changed, 42 insertions, 9 deletions
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index d792529..feeb4ce 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -28,6 +28,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 @@ -47,11 +52,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 = self.fixtype(kwargs) +        realresult = getattr(obj, methodname)(*args, **kwargs)          self.assertEqual(              result,              realresult @@ -116,7 +122,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 @@ -162,7 +172,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) @@ -216,7 +230,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__ @@ -263,7 +281,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', '') @@ -285,7 +307,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') @@ -643,7 +669,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', @@ -908,7 +934,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)  | 
