home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_string.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  1.6 KB  |  58 lines

  1. from test_support import verbose, TestSkipped
  2. import string_tests
  3. import string, sys
  4.  
  5. # XXX: kludge... short circuit if strings don't have methods
  6. try:
  7.     ''.join
  8. except AttributeError:
  9.     raise TestSkipped
  10.  
  11. def test(name, input, output, *args):
  12.     if verbose:
  13.         print 'string.%s%s =? %s... ' % (name, (input,) + args, output),
  14.     try:
  15.         # Prefer string methods over string module functions
  16.         try:
  17.             f = getattr(input, name)
  18.             value = apply(f, args)
  19.         except AttributeError:
  20.             f = getattr(string, name)
  21.             value = apply(f, (input,) + args)
  22.     except:
  23.         value = sys.exc_type
  24.         f = name
  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:
  41.                     print 'no'
  42.                 print '*',f, `input`, `output`, `value`
  43.                 return
  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.  
  52. string_tests.run_module_tests(test)
  53. string_tests.run_method_tests(test)
  54.  
  55. string.whitespace
  56. string.lowercase
  57. string.uppercase
  58.