Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Eric S. Raymond | 2846b0a | 2001-02-09 12:00:47 +0000 | [diff] [blame] | 2 | import sys |
Fredrik Lundh | f785042 | 2001-01-17 21:51:36 +0000 | [diff] [blame] | 3 | from test_support import verbose |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 4 | import string_tests |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 5 | # UserString is a wrapper around the native builtin string type. |
| 6 | # UserString instances should behave similar to builtin string objects. |
| 7 | # The test cases were in part derived from 'test_string.py'. |
| 8 | from UserString import UserString |
| 9 | |
| 10 | if __name__ == "__main__": |
Guido van Rossum | d854f3b | 2002-04-12 16:25:39 +0000 | [diff] [blame^] | 11 | verbose = '-v' in sys.argv |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 12 | |
| 13 | tested_methods = {} |
| 14 | |
Guido van Rossum | d854f3b | 2002-04-12 16:25:39 +0000 | [diff] [blame^] | 15 | def test(methodname, input, output, *args): |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 16 | global tested_methods |
| 17 | tested_methods[methodname] = 1 |
| 18 | if verbose: |
Guido van Rossum | d854f3b | 2002-04-12 16:25:39 +0000 | [diff] [blame^] | 19 | print '%r.%s(%s)' % (input, methodname, ", ".join(map(repr, args))), |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 20 | u = UserString(input) |
| 21 | objects = [input, u, UserString(u)] |
| 22 | res = [""] * 3 |
| 23 | for i in range(3): |
| 24 | object = objects[i] |
| 25 | try: |
| 26 | f = getattr(object, methodname) |
Guido van Rossum | d854f3b | 2002-04-12 16:25:39 +0000 | [diff] [blame^] | 27 | except AttributeError: |
| 28 | f = None |
| 29 | res[i] = AttributeError |
| 30 | else: |
| 31 | try: |
| 32 | res[i] = apply(f, args) |
| 33 | except: |
| 34 | res[i] = sys.exc_type |
| 35 | if res[0] == res[1] == res[2] == output: |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 36 | if verbose: |
| 37 | print 'yes' |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 38 | else: |
| 39 | if verbose: |
Guido van Rossum | d854f3b | 2002-04-12 16:25:39 +0000 | [diff] [blame^] | 40 | print 'no' |
| 41 | print (methodname, input, output, args, res[0], res[1], res[2]) |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 42 | |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 43 | string_tests.run_method_tests(test) |