Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 1 | # UserString is a wrapper around the native builtin string type. |
| 2 | # UserString instances should behave similar to builtin string objects. |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 3 | |
Ezio Melotti | 0dceb56 | 2013-01-10 07:43:26 +0200 | [diff] [blame] | 4 | import unittest |
Serhiy Storchaka | 597d15a | 2016-04-24 13:45:58 +0300 | [diff] [blame] | 5 | from test import string_tests |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 6 | |
Raymond Hettinger | b3a65f8 | 2008-02-21 22:11:37 +0000 | [diff] [blame] | 7 | from collections import UserString |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 8 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 9 | class UserStringTest( |
| 10 | string_tests.CommonTest, |
| 11 | string_tests.MixinStrUnicodeUserStringTest, |
Ezio Melotti | 0dceb56 | 2013-01-10 07:43:26 +0200 | [diff] [blame] | 12 | unittest.TestCase |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 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) |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 20 | def checkequal(self, result, object, methodname, *args, **kwargs): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 21 | result = self.fixtype(result) |
| 22 | object = self.fixtype(object) |
| 23 | # we don't fix the arguments, because UserString can't cope with it |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 24 | realresult = getattr(object, methodname)(*args, **kwargs) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 25 | self.assertEqual( |
| 26 | result, |
| 27 | realresult |
| 28 | ) |
| 29 | |
Benjamin Peterson | c31f12d | 2014-09-28 12:56:42 -0400 | [diff] [blame] | 30 | def checkraises(self, exc, obj, methodname, *args): |
| 31 | obj = self.fixtype(obj) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 32 | # we don't fix the arguments, because UserString can't cope with it |
Benjamin Peterson | c31f12d | 2014-09-28 12:56:42 -0400 | [diff] [blame] | 33 | with self.assertRaises(exc) as cm: |
| 34 | getattr(obj, methodname)(*args) |
| 35 | self.assertNotEqual(str(cm.exception), '') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 36 | |
| 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şkaya | 7abf8c6 | 2019-05-21 23:27:36 +0300 | [diff] [blame] | 42 | 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) | 2cb82d2 | 2019-08-27 21:59:54 -0700 | [diff] [blame] | 54 | 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örwald | 7f79152 | 2005-02-17 22:03:31 +0000 | [diff] [blame] | 68 | |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 69 | if __name__ == "__main__": |
Ezio Melotti | 0dceb56 | 2013-01-10 07:43:26 +0200 | [diff] [blame] | 70 | unittest.main() |