Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 1 | from test.test_support import verbose, TestSkipped |
| 2 | from test import string_tests |
Barry Warsaw | 50f0e16 | 1999-06-10 22:53:10 +0000 | [diff] [blame] | 3 | import string, sys |
| 4 | |
Barry Warsaw | 6e1d78a | 1999-06-15 16:49:11 +0000 | [diff] [blame] | 5 | # XXX: kludge... short circuit if strings don't have methods |
| 6 | try: |
| 7 | ''.join |
| 8 | except AttributeError: |
Thomas Wouters | b9fa0a8 | 2000-08-04 13:34:43 +0000 | [diff] [blame] | 9 | raise TestSkipped |
Barry Warsaw | 6e1d78a | 1999-06-15 16:49:11 +0000 | [diff] [blame] | 10 | |
Barry Warsaw | 50f0e16 | 1999-06-10 22:53:10 +0000 | [diff] [blame] | 11 | def test(name, input, output, *args): |
| 12 | if verbose: |
| 13 | print 'string.%s%s =? %s... ' % (name, (input,) + args, output), |
Barry Warsaw | 50f0e16 | 1999-06-10 22:53:10 +0000 | [diff] [blame] | 14 | try: |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 15 | # Prefer string methods over string module functions |
Barry Warsaw | 8a9514a | 1999-06-11 17:48:07 +0000 | [diff] [blame] | 16 | try: |
Barry Warsaw | 8a9514a | 1999-06-11 17:48:07 +0000 | [diff] [blame] | 17 | f = getattr(input, name) |
| 18 | value = apply(f, args) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 19 | except AttributeError: |
| 20 | f = getattr(string, name) |
| 21 | value = apply(f, (input,) + args) |
Barry Warsaw | 50f0e16 | 1999-06-10 22:53:10 +0000 | [diff] [blame] | 22 | except: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 23 | value = sys.exc_type |
Finn Bock | ed69aee | 2001-12-09 16:06:29 +0000 | [diff] [blame] | 24 | f = name |
Walter Dörwald | 2ee4be0 | 2002-04-17 21:34:05 +0000 | [diff] [blame] | 25 | if value == output: |
| 26 | # if the original is returned make sure that |
| 27 | # this doesn't happen with subclasses |
| 28 | if value is input: |
| 29 | class ssub(str): |
| 30 | def __repr__(self): |
| 31 | return 'ssub(%r)' % str.__repr__(self) |
| 32 | input = ssub(input) |
| 33 | try: |
| 34 | f = getattr(input, name) |
| 35 | value = apply(f, args) |
| 36 | except AttributeError: |
| 37 | f = getattr(string, name) |
| 38 | value = apply(f, (input,) + args) |
| 39 | if value is input: |
| 40 | if verbose: |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 41 | print 'no' |
Walter Dörwald | 2ee4be0 | 2002-04-17 21:34:05 +0000 | [diff] [blame] | 42 | print '*',f, `input`, `output`, `value` |
| 43 | return |
Barry Warsaw | 50f0e16 | 1999-06-10 22:53:10 +0000 | [diff] [blame] | 44 | if value != output: |
| 45 | if verbose: |
| 46 | print 'no' |
| 47 | print f, `input`, `output`, `value` |
| 48 | else: |
| 49 | if verbose: |
| 50 | print 'yes' |
| 51 | |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 52 | string_tests.run_module_tests(test) |
| 53 | string_tests.run_method_tests(test) |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 54 | string_tests.run_contains_tests(test) |
Raymond Hettinger | c35491e | 2002-08-09 01:37:06 +0000 | [diff] [blame] | 55 | string_tests.run_inplace_tests(str) |
Barry Warsaw | 50f0e16 | 1999-06-10 22:53:10 +0000 | [diff] [blame] | 56 | |
| 57 | string.whitespace |
| 58 | string.lowercase |
| 59 | string.uppercase |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame^] | 60 | |
| 61 | # Float formatting |
| 62 | for prec in range(100): |
| 63 | formatstring = u'%%.%if' % prec |
| 64 | value = 0.01 |
| 65 | for x in range(60): |
| 66 | value = value * 3.141592655 / 3.0 * 10.0 |
| 67 | #print 'Overflow check for x=%i and prec=%i:' % \ |
| 68 | # (x, prec), |
| 69 | try: |
| 70 | result = formatstring % value |
| 71 | except OverflowError: |
| 72 | # The formatfloat() code in stringobject.c and |
| 73 | # unicodeobject.c uses a 120 byte buffer and switches from |
| 74 | # 'f' formatting to 'g' at precision 50, so we expect |
| 75 | # OverflowErrors for the ranges x < 50 and prec >= 67. |
| 76 | if x >= 50 or \ |
| 77 | prec < 67: |
| 78 | print '*** unexpected OverflowError for x=%i and prec=%i' % (x, prec) |
| 79 | else: |
| 80 | #print 'OverflowError' |
| 81 | pass |
| 82 | else: |
| 83 | #print result |
| 84 | pass |
| 85 | |