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