1 # UserString is a wrapper around the native builtin string type.
2 # UserString instances should behave similar to builtin string objects.
3
4 import unittest
5 from test import string_tests
6
7 from collections import UserString
8
9 class ESC[4;38;5;81mUserStringTest(
10 ESC[4;38;5;149mstring_testsESC[4;38;5;149m.ESC[4;38;5;149mCommonTest,
11 ESC[4;38;5;149mstring_testsESC[4;38;5;149m.ESC[4;38;5;149mMixinStrUnicodeUserStringTest,
12 ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase
13 ):
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)
20 def checkequal(self, result, object, methodname, *args, **kwargs):
21 result = self.fixtype(result)
22 object = self.fixtype(object)
23 # we don't fix the arguments, because UserString can't cope with it
24 realresult = getattr(object, methodname)(*args, **kwargs)
25 self.assertEqual(
26 result,
27 realresult
28 )
29
30 def checkraises(self, exc, obj, methodname, *args, expected_msg=None):
31 obj = self.fixtype(obj)
32 # we don't fix the arguments, because UserString can't cope with it
33 with self.assertRaises(exc) as cm:
34 getattr(obj, methodname)(*args)
35 self.assertNotEqual(str(cm.exception), '')
36 if expected_msg is not None:
37 self.assertEqual(str(cm.exception), expected_msg)
38
39 def checkcall(self, object, methodname, *args):
40 object = self.fixtype(object)
41 # we don't fix the arguments, because UserString can't cope with it
42 getattr(object, methodname)(*args)
43
44 def test_rmod(self):
45 class ESC[4;38;5;81mustr2(ESC[4;38;5;149mUserString):
46 pass
47
48 class ESC[4;38;5;81mustr3(ESC[4;38;5;149mustr2):
49 def __rmod__(self, other):
50 return super().__rmod__(other)
51
52 fmt2 = ustr2('value is %s')
53 str3 = ustr3('TEST')
54 self.assertEqual(fmt2 % str3, 'value is TEST')
55
56 def test_encode_default_args(self):
57 self.checkequal(b'hello', 'hello', 'encode')
58 # Check that encoding defaults to utf-8
59 self.checkequal(b'\xf0\xa3\x91\x96', '\U00023456', 'encode')
60 # Check that errors defaults to 'strict'
61 self.checkraises(UnicodeError, '\ud800', 'encode')
62
63 def test_encode_explicit_none_args(self):
64 self.checkequal(b'hello', 'hello', 'encode', None, None)
65 # Check that encoding defaults to utf-8
66 self.checkequal(b'\xf0\xa3\x91\x96', '\U00023456', 'encode', None, None)
67 # Check that errors defaults to 'strict'
68 self.checkraises(UnicodeError, '\ud800', 'encode', None, None)
69
70
71 if __name__ == "__main__":
72 unittest.main()