| 
									
										
										
										
											2002-07-23 19:04:11 +00:00
										 |  |  | from test.test_support import verbose, TestSkipped | 
					
						
							|  |  |  | from test import string_tests | 
					
						
							| 
									
										
										
										
											1999-06-10 22:53:10 +00:00
										 |  |  | import string, sys | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-06-15 16:49:11 +00:00
										 |  |  | # XXX: kludge... short circuit if strings don't have methods | 
					
						
							|  |  |  | try: | 
					
						
							|  |  |  |     ''.join | 
					
						
							|  |  |  | except AttributeError: | 
					
						
							| 
									
										
										
										
											2000-08-04 13:34:43 +00:00
										 |  |  |     raise TestSkipped | 
					
						
							| 
									
										
										
										
											1999-06-15 16:49:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-06-10 22:53:10 +00:00
										 |  |  | def test(name, input, output, *args): | 
					
						
							|  |  |  |     if verbose: | 
					
						
							|  |  |  |         print 'string.%s%s =? %s... ' % (name, (input,) + args, output), | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2000-03-10 23:23:21 +00:00
										 |  |  |         # Prefer string methods over string module functions | 
					
						
							| 
									
										
										
										
											1999-06-11 17:48:07 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             f = getattr(input, name) | 
					
						
							|  |  |  |             value = apply(f, args) | 
					
						
							| 
									
										
										
										
											2000-03-10 23:23:21 +00:00
										 |  |  |         except AttributeError: | 
					
						
							|  |  |  |             f = getattr(string, name) | 
					
						
							|  |  |  |             value = apply(f, (input,) + args) | 
					
						
							| 
									
										
										
										
											1999-06-10 22:53:10 +00:00
										 |  |  |     except: | 
					
						
							| 
									
										
										
										
											2000-10-23 17:22:08 +00:00
										 |  |  |         value = sys.exc_type | 
					
						
							| 
									
										
										
										
											2001-12-09 16:06:29 +00:00
										 |  |  |         f = name | 
					
						
							| 
									
										
										
										
											2002-04-17 21:34:05 +00:00
										 |  |  |     if value == output: | 
					
						
							|  |  |  |         # if the original is returned make sure that | 
					
						
							|  |  |  |         # this doesn't happen with subclasses | 
					
						
							|  |  |  |         if value is input: | 
					
						
							|  |  |  |             class ssub(str): | 
					
						
							|  |  |  |                 def __repr__(self): | 
					
						
							|  |  |  |                     return 'ssub(%r)' % str.__repr__(self) | 
					
						
							|  |  |  |             input = ssub(input) | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 f = getattr(input, name) | 
					
						
							|  |  |  |                 value = apply(f, args) | 
					
						
							|  |  |  |             except AttributeError: | 
					
						
							|  |  |  |                 f = getattr(string, name) | 
					
						
							|  |  |  |                 value = apply(f, (input,) + args) | 
					
						
							|  |  |  |             if value is input: | 
					
						
							|  |  |  |                 if verbose: | 
					
						
							| 
									
										
										
										
											2002-05-23 15:15:30 +00:00
										 |  |  |                     print 'no' | 
					
						
							| 
									
										
										
										
											2002-04-17 21:34:05 +00:00
										 |  |  |                 print '*',f, `input`, `output`, `value` | 
					
						
							|  |  |  |                 return | 
					
						
							| 
									
										
										
										
											1999-06-10 22:53:10 +00:00
										 |  |  |     if value != output: | 
					
						
							|  |  |  |         if verbose: | 
					
						
							|  |  |  |             print 'no' | 
					
						
							|  |  |  |         print f, `input`, `output`, `value` | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         if verbose: | 
					
						
							|  |  |  |             print 'yes' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-10 17:08:42 +00:00
										 |  |  | string_tests.run_module_tests(test) | 
					
						
							|  |  |  | string_tests.run_method_tests(test) | 
					
						
							| 
									
										
										
										
											2002-08-06 16:58:21 +00:00
										 |  |  | string_tests.run_contains_tests(test) | 
					
						
							| 
									
										
										
										
											2002-08-09 01:37:06 +00:00
										 |  |  | string_tests.run_inplace_tests(str) | 
					
						
							| 
									
										
										
										
											1999-06-10 22:53:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | string.whitespace | 
					
						
							|  |  |  | string.lowercase | 
					
						
							|  |  |  | string.uppercase | 
					
						
							| 
									
										
										
										
											2002-12-29 19:44:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Float formatting | 
					
						
							|  |  |  | for prec in range(100): | 
					
						
							| 
									
										
										
										
											2002-12-30 10:50:32 +00:00
										 |  |  |     formatstring = '%%.%if' % prec | 
					
						
							| 
									
										
										
										
											2002-12-29 19:44:06 +00:00
										 |  |  |     value = 0.01 | 
					
						
							|  |  |  |     for x in range(60): | 
					
						
							|  |  |  |         value = value * 3.141592655 / 3.0 * 10.0 | 
					
						
							|  |  |  |         #print 'Overflow check for x=%i and prec=%i:' % \ | 
					
						
							|  |  |  |         #      (x, prec), | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result = formatstring % value | 
					
						
							|  |  |  |         except OverflowError: | 
					
						
							|  |  |  |             # The formatfloat() code in stringobject.c and | 
					
						
							|  |  |  |             # unicodeobject.c uses a 120 byte buffer and switches from | 
					
						
							|  |  |  |             # 'f' formatting to 'g' at precision 50, so we expect | 
					
						
							|  |  |  |             # OverflowErrors for the ranges x < 50 and prec >= 67. | 
					
						
							|  |  |  |             if x >= 50 or \ | 
					
						
							|  |  |  |                prec < 67: | 
					
						
							|  |  |  |                 print '*** unexpected OverflowError for x=%i and prec=%i' % (x, prec) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 #print 'OverflowError' | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             #print result | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  |      |