home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / test / test_strop.py < prev    next >
Text File  |  1997-12-01  |  4KB  |  88 lines

  1. from test_support import verbose
  2. import strop, sys
  3.  
  4. def test(name, input, output, *args):
  5.     if verbose:
  6.     print 'string.%s%s =? %s... ' % (name, (input,) + args, output),
  7.     f = getattr(strop, name)
  8.     try:
  9.     value = apply(f, (input,) + args)
  10.     except:
  11.      value = sys.exc_type
  12.     if value != output:
  13.     if verbose:
  14.         print 'no'
  15.     print f, `input`, `output`, `value`
  16.     else:
  17.     if verbose:
  18.         print 'yes'
  19.  
  20. test('atoi', " 1 ", 1)
  21. test('atoi', " 1x", ValueError)
  22. test('atoi', " x1 ", ValueError)
  23. test('atol', "  1  ", 1L)
  24. test('atol', "  1x ", ValueError)
  25. test('atol', "  x1 ", ValueError)
  26. test('atof', "  1  ", 1.0)
  27. test('atof', "  1x ", ValueError)
  28. test('atof', "  x1 ", ValueError)
  29.  
  30. test('capitalize', ' hello ', ' hello ')
  31. test('capitalize', 'hello ', 'Hello ')
  32. test('find', 'abcdefghiabc', 0, 'abc')
  33. test('find', 'abcdefghiabc', 9, 'abc', 1)
  34. test('find', 'abcdefghiabc', -1, 'def', 4)
  35. test('rfind', 'abcdefghiabc', 9, 'abc')
  36. test('lower', 'HeLLo', 'hello')
  37. test('upper', 'HeLLo', 'HELLO')
  38.  
  39. transtable = '\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377'
  40.  
  41. test('maketrans', 'abc', transtable, 'xyz')
  42. test('maketrans', 'abc', ValueError, 'xyzq')
  43.  
  44. test('split', 'this is the split function',
  45.      ['this', 'is', 'the', 'split', 'function'])
  46. test('split', 'a|b|c|d', ['a', 'b', 'c', 'd'], '|')
  47. test('split', 'a|b|c|d', ['a', 'b', 'c|d'], '|', 2)
  48. test('split', 'a b c d', ['a', 'b c d'], None, 1)
  49. test('split', 'a b c d', ['a', 'b', 'c d'], None, 2)
  50. test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 3)
  51. test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 4)
  52. test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 0)
  53. test('split', 'a  b  c  d', ['a', 'b', 'c  d'], None, 2)
  54.  
  55. # join now works with any sequence type
  56. class Sequence:
  57.     def __init__(self): self.seq = 'wxyz'
  58.     def __len__(self): return len(self.seq)
  59.     def __getitem__(self, i): return self.seq[i]
  60.  
  61. test('join', ['a', 'b', 'c', 'd'], 'a b c d')
  62. test('join', ('a', 'b', 'c', 'd'), 'abcd', '')
  63. test('join', Sequence(), 'w x y z')
  64.  
  65. # try a few long ones
  66. print strop.join(['x' * 100] * 100, ':')
  67. print strop.join(('x' * 100,) * 100, ':')
  68.  
  69. test('strip', '   hello   ', 'hello')
  70. test('lstrip', '   hello   ', 'hello   ')
  71. test('rstrip', '   hello   ', '   hello')
  72.  
  73. test('swapcase', 'HeLLo cOmpUteRs', 'hEllO CoMPuTErS')
  74. test('translate', 'xyzabcdef', 'xyzxyz', transtable, 'def')
  75.  
  76. test('replace', 'one!two!three!', 'one@two!three!', '!', '@', 1)
  77. test('replace', 'one!two!three!', 'one@two@three!', '!', '@', 2)
  78. test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 3)
  79. test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 4)
  80. test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 0)
  81. test('replace', 'one!two!three!', 'one@two@three@', '!', '@')
  82. test('replace', 'one!two!three!', 'one!two!three!', 'x', '@')
  83. test('replace', 'one!two!three!', 'one!two!three!', 'x', '@', 2)
  84.  
  85. strop.whitespace
  86. strop.lowercase
  87. strop.uppercase
  88.