1 from test_support
import verbose
4 # XXX: kludge... short circuit if strings don't have methods
10 def test(name
, input, output
, *args
):
12 print 'string.%s%s =? %s... ' % (name
, (input,) + args
, output
),
15 f
= getattr(string
, name
)
16 value
= apply(f
, (input,) + args
)
17 except AttributeError:
18 f
= getattr(input, name
)
19 value
= apply(f
, args
)
25 print f
, `
input`
, `output`
, `value`
30 test('atoi', " 1 ", 1)
31 test('atoi', " 1x", ValueError)
32 test('atoi', " x1 ", ValueError)
33 test('atol', " 1 ", 1L)
34 test('atol', " 1x ", ValueError)
35 test('atol', " x1 ", ValueError)
36 test('atof', " 1 ", 1.0)
37 test('atof', " 1x ", ValueError)
38 test('atof', " x1 ", ValueError)
40 test('capitalize', ' hello ', ' hello ')
41 test('capitalize', 'hello ', 'Hello ')
42 test('find', 'abcdefghiabc', 0, 'abc')
43 test('find', 'abcdefghiabc', 9, 'abc', 1)
44 test('find', 'abcdefghiabc', -1, 'def', 4)
45 test('rfind', 'abcdefghiabc', 9, 'abc')
46 test('lower', 'HeLLo', 'hello')
47 test('lower', 'hello', 'hello')
48 test('upper', 'HeLLo', 'HELLO')
49 test('upper', 'HELLO', 'HELLO')
51 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'
53 test('maketrans', 'abc', transtable
, 'xyz')
54 test('maketrans', 'abc', ValueError, 'xyzq')
56 test('split', 'this is the split function',
57 ['this', 'is', 'the', 'split', 'function'])
58 test('split', 'a|b|c|d', ['a', 'b', 'c', 'd'], '|')
59 test('split', 'a|b|c|d', ['a', 'b', 'c|d'], '|', 2)
60 test('split', 'a b c d', ['a', 'b c d'], None, 1)
61 test('split', 'a b c d', ['a', 'b', 'c d'], None, 2)
62 test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 3)
63 test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 4)
64 test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 0)
65 test('split', 'a b c d', ['a', 'b', 'c d'], None, 2)
67 # join now works with any sequence type
69 def __init__(self
): self
.seq
= 'wxyz'
70 def __len__(self
): return len(self
.seq
)
71 def __getitem__(self
, i
): return self
.seq
[i
]
73 test('join', ['a', 'b', 'c', 'd'], 'a b c d')
74 test('join', ('a', 'b', 'c', 'd'), 'abcd', '')
75 test('join', Sequence(), 'w x y z')
76 test('join', 7, TypeError)
79 def __str__(self
): raise RuntimeError
81 class BadSeq(Sequence
):
82 def __init__(self
): self
.seq
= [7, 'hello', BadStr()]
84 test('join', BadSeq(), RuntimeError)
87 print string
.join(['x' * 100] * 100, ':')
88 print string
.join(('x' * 100,) * 100, ':')
90 test('strip', ' hello ', 'hello')
91 test('lstrip', ' hello ', 'hello ')
92 test('rstrip', ' hello ', ' hello')
93 test('strip', 'hello', 'hello')
95 test('swapcase', 'HeLLo cOmpUteRs', 'hEllO CoMPuTErS')
96 test('translate', 'xyzabcdef', 'xyzxyz', transtable
, 'def')
98 table
= string
.maketrans('a', 'A')
99 test('translate', 'abc', 'Abc', table
)
100 test('translate', 'xyz', 'xyz', table
)
102 test('replace', 'one!two!three!', 'one@two!three!', '!', '@', 1)
103 test('replace', 'one!two!three!', 'one@two@three!', '!', '@', 2)
104 test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 3)
105 test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 4)
106 test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 0)
107 test('replace', 'one!two!three!', 'one@two@three@', '!', '@')
108 test('replace', 'one!two!three!', 'one!two!three!', 'x', '@')
109 test('replace', 'one!two!three!', 'one!two!three!', 'x', '@', 2)
111 test('startswith', 'hello', 1, 'he')
112 test('startswith', 'hello', 1, 'hello')
113 test('startswith', 'hello', 0, 'hello world')
114 test('startswith', 'hello', 1, '')
115 test('startswith', 'hello', 0, 'ello')
116 test('startswith', 'hello', 1, 'ello', 1)
117 test('startswith', 'hello', 1, 'o', 4)
118 test('startswith', 'hello', 0, 'o', 5)
119 test('startswith', 'hello', 1, '', 5)
120 test('startswith', 'hello', 0, 'lo', 6)
121 test('startswith', 'helloworld', 1, 'lowo', 3)
122 test('startswith', 'helloworld', 1, 'lowo', 3, 7)
123 test('startswith', 'helloworld', 0, 'lowo', 3, 6)
125 test('endswith', 'hello', 1, 'lo')
126 test('endswith', 'hello', 0, 'he')
127 test('endswith', 'hello', 1, '')
128 test('endswith', 'hello', 0, 'hello world')
129 test('endswith', 'helloworld', 0, 'worl')
130 test('endswith', 'helloworld', 1, 'worl', 3, 9)
131 test('endswith', 'helloworld', 1, 'world', 3, 12)
132 test('endswith', 'helloworld', 1, 'lowo', 1, 7)
133 test('endswith', 'helloworld', 1, 'lowo', 2, 7)
134 test('endswith', 'helloworld', 1, 'lowo', 3, 7)
135 test('endswith', 'helloworld', 0, 'lowo', 4, 7)
136 test('endswith', 'helloworld', 0, 'lowo', 3, 8)
137 test('endswith', 'ab', 0, 'ab', 0, 1)
138 test('endswith', 'ab', 0, 'ab', 0, 0)