blob: c12f1e205518f54ee5af0a67582ee7de393919d6 [file] [log] [blame]
Fred Drakea22b5762000-04-03 03:51:50 +00001#!/usr/bin/env python
2import sys, string
3from test_support import verbose
4# UserString is a wrapper around the native builtin string type.
5# UserString instances should behave similar to builtin string objects.
6# The test cases were in part derived from 'test_string.py'.
7from UserString import UserString
8
9if __name__ == "__main__":
10 verbose = 0
11
12tested_methods = {}
13
14def test(methodname, input, *args):
15 global tested_methods
16 tested_methods[methodname] = 1
17 if verbose:
18 print '%s.%s(%s) ' % (input, methodname, args),
19 u = UserString(input)
20 objects = [input, u, UserString(u)]
21 res = [""] * 3
22 for i in range(3):
23 object = objects[i]
24 try:
25 f = getattr(object, methodname)
26 res[i] = apply(f, args)
27 except:
28 res[i] = sys.exc_type
29 if res[0] != res[1]:
30 if verbose:
31 print 'no'
32 print `input`, f, `res[0]`, "<>", `res[1]`
33 else:
34 if verbose:
35 print 'yes'
36 if res[1] != res[2]:
37 if verbose:
38 print 'no'
39 print `input`, f, `res[1]`, "<>", `res[2]`
40 else:
41 if verbose:
42 print 'yes'
43
44test('capitalize', ' hello ')
45test('capitalize', 'hello ')
46
47test('center', 'foo', 0)
48test('center', 'foo', 3)
49test('center', 'foo', 16)
50
51test('ljust', 'foo', 0)
52test('ljust', 'foo', 3)
53test('ljust', 'foo', 16)
54
55test('rjust', 'foo', 0)
56test('rjust', 'foo', 3)
57test('rjust', 'foo', 16)
58
59test('count', 'abcabcabc', 'abc')
60test('count', 'abcabcabc', 'abc', 1)
61test('count', 'abcabcabc', 'abc', -1)
62test('count', 'abcabcabc', 'abc', 7)
63test('count', 'abcabcabc', 'abc', 0, 3)
64test('count', 'abcabcabc', 'abc', 0, 333)
65
66test('find', 'abcdefghiabc', 'abc')
67test('find', 'abcdefghiabc', 'abc', 1)
68test('find', 'abcdefghiabc', 'def', 4)
69test('rfind', 'abcdefghiabc', 'abc')
70
71test('index', 'abcabcabc', 'abc')
72test('index', 'abcabcabc', 'abc', 1)
73test('index', 'abcabcabc', 'abc', -1)
74test('index', 'abcabcabc', 'abc', 7)
75test('index', 'abcabcabc', 'abc', 0, 3)
76test('index', 'abcabcabc', 'abc', 0, 333)
77
78test('rindex', 'abcabcabc', 'abc')
79test('rindex', 'abcabcabc', 'abc', 1)
80test('rindex', 'abcabcabc', 'abc', -1)
81test('rindex', 'abcabcabc', 'abc', 7)
82test('rindex', 'abcabcabc', 'abc', 0, 3)
83test('rindex', 'abcabcabc', 'abc', 0, 333)
84
85
86test('lower', 'HeLLo')
87test('lower', 'hello')
88test('upper', 'HeLLo')
89test('upper', 'HELLO')
90
91test('title', ' hello ')
92test('title', 'hello ')
93test('title', "fOrMaT thIs aS titLe String")
94test('title', "fOrMaT,thIs-aS*titLe;String")
95test('title', "getInt")
96
97test('expandtabs', 'abc\rab\tdef\ng\thi')
98test('expandtabs', 'abc\rab\tdef\ng\thi', 8)
99test('expandtabs', 'abc\rab\tdef\ng\thi', 4)
100test('expandtabs', 'abc\r\nab\tdef\ng\thi', 4)
101
102test('islower', 'a')
103test('islower', 'A')
104test('islower', '\n')
105test('islower', 'abc')
106test('islower', 'aBc')
107test('islower', 'abc\n')
108
109test('isupper', 'a')
110test('isupper', 'A')
111test('isupper', '\n')
112test('isupper', 'ABC')
113test('isupper', 'AbC')
114test('isupper', 'ABC\n')
115
116test('isdigit', ' 0123456789')
117test('isdigit', '56789')
118test('isdigit', '567.89')
119test('isdigit', '0123456789abc')
120
121test('isspace', '')
122test('isspace', ' ')
123test('isspace', ' \t')
124test('isspace', ' \t\f\n')
125
126test('istitle', 'a')
127test('istitle', 'A')
128test('istitle', '\n')
129test('istitle', 'A Titlecased Line')
130test('istitle', 'A\nTitlecased Line')
131test('istitle', 'A Titlecased, Line')
132test('istitle', 'Not a capitalized String')
133test('istitle', 'Not\ta Titlecase String')
134test('istitle', 'Not--a Titlecase String')
135
136test('splitlines', "abc\ndef\n\rghi")
137test('splitlines', "abc\ndef\n\r\nghi")
138test('splitlines', "abc\ndef\r\nghi")
139test('splitlines', "abc\ndef\r\nghi\n")
140test('splitlines', "abc\ndef\r\nghi\n\r")
141test('splitlines', "\nabc\ndef\r\nghi\n\r")
142test('splitlines', "\nabc\ndef\r\nghi\n\r")
143test('splitlines', "\nabc\ndef\r\nghi\n\r")
144
145test('split', 'this is the split function')
146test('split', 'a|b|c|d', '|')
147test('split', 'a|b|c|d', '|', 2)
148test('split', 'a b c d', None, 1)
149test('split', 'a b c d', None, 2)
150test('split', 'a b c d', None, 3)
151test('split', 'a b c d', None, 4)
152test('split', 'a b c d', None, 0)
153test('split', 'a b c d', None, 2)
154test('split', 'a b c d ')
155
156# join now works with any sequence type
157class Sequence:
158 def __init__(self): self.seq = 'wxyz'
159 def __len__(self): return len(self.seq)
160 def __getitem__(self, i): return self.seq[i]
161
162test('join', '', ('a', 'b', 'c', 'd'))
163test('join', '', Sequence())
164test('join', '', 7)
165
166class BadSeq(Sequence):
167 def __init__(self): self.seq = [7, 'hello', 123L]
168
169test('join', '', BadSeq())
170
171test('strip', ' hello ')
172test('lstrip', ' hello ')
173test('rstrip', ' hello ')
174test('strip', 'hello')
175
176test('swapcase', 'HeLLo cOmpUteRs')
177transtable = string.maketrans("abc", "xyz")
178test('translate', 'xyzabcdef', transtable, 'def')
179
180transtable = string.maketrans('a', 'A')
181test('translate', 'abc', transtable)
182test('translate', 'xyz', transtable)
183
184test('replace', 'one!two!three!', '!', '@', 1)
185test('replace', 'one!two!three!', '!', '')
186test('replace', 'one!two!three!', '!', '@', 2)
187test('replace', 'one!two!three!', '!', '@', 3)
188test('replace', 'one!two!three!', '!', '@', 4)
189test('replace', 'one!two!three!', '!', '@', 0)
190test('replace', 'one!two!three!', '!', '@')
191test('replace', 'one!two!three!', 'x', '@')
192test('replace', 'one!two!three!', 'x', '@', 2)
193
194test('startswith', 'hello', 'he')
195test('startswith', 'hello', 'hello')
196test('startswith', 'hello', 'hello world')
197test('startswith', 'hello', '')
198test('startswith', 'hello', 'ello')
199test('startswith', 'hello', 'ello', 1)
200test('startswith', 'hello', 'o', 4)
201test('startswith', 'hello', 'o', 5)
202test('startswith', 'hello', '', 5)
203test('startswith', 'hello', 'lo', 6)
204test('startswith', 'helloworld', 'lowo', 3)
205test('startswith', 'helloworld', 'lowo', 3, 7)
206test('startswith', 'helloworld', 'lowo', 3, 6)
207
208test('endswith', 'hello', 'lo')
209test('endswith', 'hello', 'he')
210test('endswith', 'hello', '')
211test('endswith', 'hello', 'hello world')
212test('endswith', 'helloworld', 'worl')
213test('endswith', 'helloworld', 'worl', 3, 9)
214test('endswith', 'helloworld', 'world', 3, 12)
215test('endswith', 'helloworld', 'lowo', 1, 7)
216test('endswith', 'helloworld', 'lowo', 2, 7)
217test('endswith', 'helloworld', 'lowo', 3, 7)
218test('endswith', 'helloworld', 'lowo', 4, 7)
219test('endswith', 'helloworld', 'lowo', 3, 8)
220test('endswith', 'ab', 'ab', 0, 1)
221test('endswith', 'ab', 'ab', 0, 0)
222
223# TODO: test cases for: int, long, float, complex, +, * and cmp
224s = ""
225for builtin_method in dir(s):
226 if not tested_methods.has_key(builtin_method):
227 print "no regression test case for method '"+builtin_method+"'"