2000-04-03 00:51:50 -03:00
|
|
|
# UserString is a wrapper around the native builtin string type.
|
|
|
|
# UserString instances should behave similar to builtin string objects.
|
2003-02-21 08:53:50 -04:00
|
|
|
|
2013-01-10 01:43:26 -04:00
|
|
|
import unittest
|
2016-04-24 07:45:58 -03:00
|
|
|
from test import string_tests
|
2003-02-21 08:53:50 -04:00
|
|
|
|
2008-02-21 18:11:37 -04:00
|
|
|
from collections import UserString
|
2000-04-03 00:51:50 -03:00
|
|
|
|
2003-02-21 08:53:50 -04:00
|
|
|
class UserStringTest(
|
|
|
|
string_tests.CommonTest,
|
|
|
|
string_tests.MixinStrUnicodeUserStringTest,
|
2013-01-10 01:43:26 -04:00
|
|
|
unittest.TestCase
|
2003-02-21 08:53:50 -04: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 05:14:39 -03:00
|
|
|
def checkequal(self, result, object, methodname, *args, **kwargs):
|
2003-02-21 08:53:50 -04: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 05:14:39 -03:00
|
|
|
realresult = getattr(object, methodname)(*args, **kwargs)
|
2003-02-21 08:53:50 -04:00
|
|
|
self.assertEqual(
|
|
|
|
result,
|
|
|
|
realresult
|
|
|
|
)
|
|
|
|
|
2014-09-28 13:56:42 -03:00
|
|
|
def checkraises(self, exc, obj, methodname, *args):
|
|
|
|
obj = self.fixtype(obj)
|
2003-02-21 08:53:50 -04:00
|
|
|
# we don't fix the arguments, because UserString can't cope with it
|
2014-09-28 13:56:42 -03:00
|
|
|
with self.assertRaises(exc) as cm:
|
|
|
|
getattr(obj, methodname)(*args)
|
|
|
|
self.assertNotEqual(str(cm.exception), '')
|
2003-02-21 08:53:50 -04: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 17:27:36 -03: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 01:38:09 -03: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 18:03:31 -04:00
|
|
|
|
2000-04-03 00:51:50 -03:00
|
|
|
if __name__ == "__main__":
|
2013-01-10 01:43:26 -04:00
|
|
|
unittest.main()
|