blob: 4d1d8b6b6fe2d951d2899e9931ab95b42051d4ca [file] [log] [blame]
Fred Drakea22b5762000-04-03 03:51:50 +00001# UserString is a wrapper around the native builtin string type.
2# UserString instances should behave similar to builtin string objects.
Walter Dörwald0fd583c2003-02-21 12:53:50 +00003
Ezio Melotti0dceb562013-01-10 07:43:26 +02004import unittest
Serhiy Storchaka597d15a2016-04-24 13:45:58 +03005from test import string_tests
Walter Dörwald0fd583c2003-02-21 12:53:50 +00006
Raymond Hettingerb3a65f82008-02-21 22:11:37 +00007from collections import UserString
Fred Drakea22b5762000-04-03 03:51:50 +00008
Walter Dörwald0fd583c2003-02-21 12:53:50 +00009class UserStringTest(
10 string_tests.CommonTest,
11 string_tests.MixinStrUnicodeUserStringTest,
Ezio Melotti0dceb562013-01-10 07:43:26 +020012 unittest.TestCase
Walter Dörwald0fd583c2003-02-21 12:53:50 +000013 ):
14
15 type2test = UserString
16
17 # Overwrite the three testing methods, because UserString
18 # can't cope with arguments propagated to UserString
19 # (and we don't test with subclasses)
Mark Dickinson0d5f6ad2011-09-24 09:14:39 +010020 def checkequal(self, result, object, methodname, *args, **kwargs):
Walter Dörwald0fd583c2003-02-21 12:53:50 +000021 result = self.fixtype(result)
22 object = self.fixtype(object)
23 # we don't fix the arguments, because UserString can't cope with it
Mark Dickinson0d5f6ad2011-09-24 09:14:39 +010024 realresult = getattr(object, methodname)(*args, **kwargs)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000025 self.assertEqual(
26 result,
27 realresult
28 )
29
Benjamin Petersonc31f12d2014-09-28 12:56:42 -040030 def checkraises(self, exc, obj, methodname, *args):
31 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000032 # we don't fix the arguments, because UserString can't cope with it
Benjamin Petersonc31f12d2014-09-28 12:56:42 -040033 with self.assertRaises(exc) as cm:
34 getattr(obj, methodname)(*args)
35 self.assertNotEqual(str(cm.exception), '')
Walter Dörwald0fd583c2003-02-21 12:53:50 +000036
37 def checkcall(self, object, methodname, *args):
38 object = self.fixtype(object)
39 # we don't fix the arguments, because UserString can't cope with it
40 getattr(object, methodname)(*args)
41
Batuhan Taşkaya7abf8c62019-05-21 23:27:36 +030042 def test_rmod(self):
43 class ustr2(UserString):
44 pass
45
46 class ustr3(ustr2):
47 def __rmod__(self, other):
48 return super().__rmod__(other)
49
50 fmt2 = ustr2('value is %s')
51 str3 = ustr3('TEST')
52 self.assertEqual(fmt2 % str3, 'value is TEST')
53
Miss Islington (bot)2cb82d22019-08-27 21:59:54 -070054 def test_encode_default_args(self):
55 self.checkequal(b'hello', 'hello', 'encode')
56 # Check that encoding defaults to utf-8
57 self.checkequal(b'\xf0\xa3\x91\x96', '\U00023456', 'encode')
58 # Check that errors defaults to 'strict'
59 self.checkraises(UnicodeError, '\ud800', 'encode')
60
61 def test_encode_explicit_none_args(self):
62 self.checkequal(b'hello', 'hello', 'encode', None, None)
63 # Check that encoding defaults to utf-8
64 self.checkequal(b'\xf0\xa3\x91\x96', '\U00023456', 'encode', None, None)
65 # Check that errors defaults to 'strict'
66 self.checkraises(UnicodeError, '\ud800', 'encode', None, None)
67
Walter Dörwald7f791522005-02-17 22:03:31 +000068
Fred Drakea22b5762000-04-03 03:51:50 +000069if __name__ == "__main__":
Ezio Melotti0dceb562013-01-10 07:43:26 +020070 unittest.main()