2000-04-03 05:51:50 +02:00
|
|
|
# UserString is a wrapper around the native builtin string type.
|
|
|
|
# UserString instances should behave similar to builtin string objects.
|
2003-02-21 13:53:50 +01:00
|
|
|
|
2013-01-10 06:43:26 +01:00
|
|
|
import unittest
|
2016-04-24 12:45:58 +02:00
|
|
|
from test import string_tests
|
2003-02-21 13:53:50 +01:00
|
|
|
|
2008-02-21 23:11:37 +01:00
|
|
|
from collections import UserString
|
2000-04-03 05:51:50 +02:00
|
|
|
|
2003-02-21 13:53:50 +01:00
|
|
|
class UserStringTest(
|
2023-05-23 16:11:29 +02:00
|
|
|
string_tests.StringLikeTest,
|
2013-01-10 06:43:26 +01:00
|
|
|
unittest.TestCase
|
2003-02-21 13:53:50 +01:00
|
|
|
):
|
|
|
|
|
|
|
|
type2test = UserString
|
|
|
|
|
|
|
|
# Overwrite the three testing methods, because UserString
|
|
|
|
# can't cope with arguments propagated to UserString
|
|
|
|
# (and we don't test with subclasses)
|
2011-09-24 10:14:39 +02:00
|
|
|
def checkequal(self, result, object, methodname, *args, **kwargs):
|
2003-02-21 13:53:50 +01:00
|
|
|
result = self.fixtype(result)
|
|
|
|
object = self.fixtype(object)
|
|
|
|
# we don't fix the arguments, because UserString can't cope with it
|
2011-09-24 10:14:39 +02:00
|
|
|
realresult = getattr(object, methodname)(*args, **kwargs)
|
2003-02-21 13:53:50 +01:00
|
|
|
self.assertEqual(
|
|
|
|
result,
|
|
|
|
realresult
|
|
|
|
)
|
|
|
|
|
2021-06-27 14:04:57 +02:00
|
|
|
def checkraises(self, exc, obj, methodname, *args, expected_msg=None):
|
2014-09-28 18:56:42 +02:00
|
|
|
obj = self.fixtype(obj)
|
2003-02-21 13:53:50 +01:00
|
|
|
# we don't fix the arguments, because UserString can't cope with it
|
2014-09-28 18:56:42 +02:00
|
|
|
with self.assertRaises(exc) as cm:
|
|
|
|
getattr(obj, methodname)(*args)
|
|
|
|
self.assertNotEqual(str(cm.exception), '')
|
2021-06-27 14:04:57 +02:00
|
|
|
if expected_msg is not None:
|
|
|
|
self.assertEqual(str(cm.exception), expected_msg)
|
2003-02-21 13:53:50 +01:00
|
|
|
|
|
|
|
def checkcall(self, object, methodname, *args):
|
|
|
|
object = self.fixtype(object)
|
|
|
|
# we don't fix the arguments, because UserString can't cope with it
|
|
|
|
getattr(object, methodname)(*args)
|
|
|
|
|
2019-05-21 22:27:36 +02:00
|
|
|
def test_rmod(self):
|
|
|
|
class ustr2(UserString):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class ustr3(ustr2):
|
|
|
|
def __rmod__(self, other):
|
|
|
|
return super().__rmod__(other)
|
|
|
|
|
|
|
|
fmt2 = ustr2('value is %s')
|
|
|
|
str3 = ustr3('TEST')
|
|
|
|
self.assertEqual(fmt2 % str3, 'value is TEST')
|
|
|
|
|
2019-08-28 06:38:09 +02:00
|
|
|
def test_encode_default_args(self):
|
|
|
|
self.checkequal(b'hello', 'hello', 'encode')
|
|
|
|
# Check that encoding defaults to utf-8
|
|
|
|
self.checkequal(b'\xf0\xa3\x91\x96', '\U00023456', 'encode')
|
|
|
|
# Check that errors defaults to 'strict'
|
|
|
|
self.checkraises(UnicodeError, '\ud800', 'encode')
|
|
|
|
|
|
|
|
def test_encode_explicit_none_args(self):
|
|
|
|
self.checkequal(b'hello', 'hello', 'encode', None, None)
|
|
|
|
# Check that encoding defaults to utf-8
|
|
|
|
self.checkequal(b'\xf0\xa3\x91\x96', '\U00023456', 'encode', None, None)
|
|
|
|
# Check that errors defaults to 'strict'
|
|
|
|
self.checkraises(UnicodeError, '\ud800', 'encode', None, None)
|
|
|
|
|
2005-02-17 23:03:31 +01:00
|
|
|
|
2000-04-03 05:51:50 +02:00
|
|
|
if __name__ == "__main__":
|
2013-01-10 06:43:26 +01:00
|
|
|
unittest.main()
|