blob: af8c1bce66d15d9dc09bddbb5c99fcf3568be598 [file] [log] [blame]
Barry Warsaw04f357c2002-07-23 19:04:11 +00001from test.test_support import verbose, TestSkipped
2from test import string_tests
Barry Warsaw50f0e161999-06-10 22:53:10 +00003import string, sys
4
Barry Warsaw6e1d78a1999-06-15 16:49:11 +00005# XXX: kludge... short circuit if strings don't have methods
6try:
7 ''.join
8except AttributeError:
Thomas Woutersb9fa0a82000-08-04 13:34:43 +00009 raise TestSkipped
Barry Warsaw6e1d78a1999-06-15 16:49:11 +000010
Barry Warsaw50f0e161999-06-10 22:53:10 +000011def test(name, input, output, *args):
12 if verbose:
13 print 'string.%s%s =? %s... ' % (name, (input,) + args, output),
Barry Warsaw50f0e161999-06-10 22:53:10 +000014 try:
Guido van Rossuma831cac2000-03-10 23:23:21 +000015 # Prefer string methods over string module functions
Barry Warsaw8a9514a1999-06-11 17:48:07 +000016 try:
Barry Warsaw8a9514a1999-06-11 17:48:07 +000017 f = getattr(input, name)
18 value = apply(f, args)
Guido van Rossuma831cac2000-03-10 23:23:21 +000019 except AttributeError:
20 f = getattr(string, name)
21 value = apply(f, (input,) + args)
Barry Warsaw50f0e161999-06-10 22:53:10 +000022 except:
Fred Drake004d5e62000-10-23 17:22:08 +000023 value = sys.exc_type
Finn Bocked69aee2001-12-09 16:06:29 +000024 f = name
Walter Dörwald2ee4be02002-04-17 21:34:05 +000025 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 Peters8ac14952002-05-23 15:15:30 +000041 print 'no'
Walter Dörwald2ee4be02002-04-17 21:34:05 +000042 print '*',f, `input`, `output`, `value`
43 return
Barry Warsaw50f0e161999-06-10 22:53:10 +000044 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 Hyltonf82b04e2000-07-10 17:08:42 +000052string_tests.run_module_tests(test)
53string_tests.run_method_tests(test)
Barry Warsaw50f0e161999-06-10 22:53:10 +000054
55string.whitespace
56string.lowercase
57string.uppercase