blob: 74a2bb649ace846856d0797e2009c7ea03def8e0 [file] [log] [blame]
Fredrik Lundhf7850422001-01-17 21:51:36 +00001from test_support import verbose
Guido van Rossum2e0a6542001-05-15 02:14:44 +00002import warnings
3warnings.filterwarnings("ignore", "", DeprecationWarning, __name__)
Guido van Rossumedaf1c91996-10-08 14:07:56 +00004import strop, sys
5
Barry Warsawad522fa1996-12-09 21:49:10 +00006def test(name, input, output, *args):
Barry Warsawda0e5201997-01-06 22:46:07 +00007 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +00008 print 'string.%s%s =? %s... ' % (name, (input,) + args, output),
Guido van Rossumedaf1c91996-10-08 14:07:56 +00009 f = getattr(strop, name)
10 try:
Guido van Rossum41360a41998-03-26 19:42:58 +000011 value = apply(f, (input,) + args)
Guido van Rossumedaf1c91996-10-08 14:07:56 +000012 except:
Fred Drake004d5e62000-10-23 17:22:08 +000013 value = sys.exc_type
Guido van Rossumedaf1c91996-10-08 14:07:56 +000014 if value != output:
Guido van Rossum41360a41998-03-26 19:42:58 +000015 if verbose:
16 print 'no'
17 print f, `input`, `output`, `value`
Barry Warsawda0e5201997-01-06 22:46:07 +000018 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000019 if verbose:
20 print 'yes'
Guido van Rossumedaf1c91996-10-08 14:07:56 +000021
22test('atoi', " 1 ", 1)
23test('atoi', " 1x", ValueError)
24test('atoi', " x1 ", ValueError)
25test('atol', " 1 ", 1L)
26test('atol', " 1x ", ValueError)
27test('atol', " x1 ", ValueError)
28test('atof', " 1 ", 1.0)
29test('atof', " 1x ", ValueError)
30test('atof', " x1 ", ValueError)
Barry Warsawad522fa1996-12-09 21:49:10 +000031
32test('capitalize', ' hello ', ' hello ')
33test('capitalize', 'hello ', 'Hello ')
34test('find', 'abcdefghiabc', 0, 'abc')
35test('find', 'abcdefghiabc', 9, 'abc', 1)
36test('find', 'abcdefghiabc', -1, 'def', 4)
37test('rfind', 'abcdefghiabc', 9, 'abc')
38test('lower', 'HeLLo', 'hello')
39test('upper', 'HeLLo', 'HELLO')
40
41transtable = '\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'
42
43test('maketrans', 'abc', transtable, 'xyz')
44test('maketrans', 'abc', ValueError, 'xyzq')
45
46test('split', 'this is the split function',
47 ['this', 'is', 'the', 'split', 'function'])
48test('split', 'a|b|c|d', ['a', 'b', 'c', 'd'], '|')
49test('split', 'a|b|c|d', ['a', 'b', 'c|d'], '|', 2)
Barry Warsawc99a2391997-12-02 00:30:04 +000050test('split', 'a b c d', ['a', 'b c d'], None, 1)
51test('split', 'a b c d', ['a', 'b', 'c d'], None, 2)
52test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 3)
53test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 4)
54test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 0)
55test('split', 'a b c d', ['a', 'b', 'c d'], None, 2)
Barry Warsawad522fa1996-12-09 21:49:10 +000056
Barry Warsawda0e5201997-01-06 22:46:07 +000057# join now works with any sequence type
58class Sequence:
59 def __init__(self): self.seq = 'wxyz'
60 def __len__(self): return len(self.seq)
61 def __getitem__(self, i): return self.seq[i]
62
Barry Warsawad522fa1996-12-09 21:49:10 +000063test('join', ['a', 'b', 'c', 'd'], 'a b c d')
Barry Warsawda0e5201997-01-06 22:46:07 +000064test('join', ('a', 'b', 'c', 'd'), 'abcd', '')
65test('join', Sequence(), 'w x y z')
66
67# try a few long ones
68print strop.join(['x' * 100] * 100, ':')
69print strop.join(('x' * 100,) * 100, ':')
Barry Warsawad522fa1996-12-09 21:49:10 +000070
71test('strip', ' hello ', 'hello')
72test('lstrip', ' hello ', 'hello ')
73test('rstrip', ' hello ', ' hello')
74
75test('swapcase', 'HeLLo cOmpUteRs', 'hEllO CoMPuTErS')
76test('translate', 'xyzabcdef', 'xyzxyz', transtable, 'def')
77
Barry Warsaw91811901997-11-29 00:25:30 +000078test('replace', 'one!two!three!', 'one@two!three!', '!', '@', 1)
79test('replace', 'one!two!three!', 'one@two@three!', '!', '@', 2)
80test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 3)
81test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 4)
Tim Peters1ee77d92001-05-10 01:23:39 +000082# CAUTION: a replace count of 0 means infinity only to strop, not to the
83# string .replace() method or to the string.replace() function.
84test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 0)
Barry Warsaw91811901997-11-29 00:25:30 +000085test('replace', 'one!two!three!', 'one@two@three@', '!', '@')
86test('replace', 'one!two!three!', 'one!two!three!', 'x', '@')
87test('replace', 'one!two!three!', 'one!two!three!', 'x', '@', 2)
88
Barry Warsawad522fa1996-12-09 21:49:10 +000089strop.whitespace
90strop.lowercase
91strop.uppercase