blob: c12f1e205518f54ee5af0a67582ee7de393919d6 [file] [log] [blame]
Guido van Rossum3e06ab12000-06-29 19:35:29 +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
Guido van Rossum9a744a91999-04-08 20:27:54 +00008
Guido van Rossum3e06ab12000-06-29 19:35:29 +00009if __name__ == "__main__":
10 verbose = 0
Guido van Rossum9a744a91999-04-08 20:27:54 +000011
Guido van Rossum3e06ab12000-06-29 19:35:29 +000012tested_methods = {}
Guido van Rossum9a744a91999-04-08 20:27:54 +000013
Guido van Rossum3e06ab12000-06-29 19:35:29 +000014def 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'
Guido van Rossum9a744a91999-04-08 20:27:54 +000043
Guido van Rossum3e06ab12000-06-29 19:35:29 +000044test('capitalize', ' hello ')
45test('capitalize', 'hello ')
Guido van Rossum9a744a91999-04-08 20:27:54 +000046
Guido van Rossum3e06ab12000-06-29 19:35:29 +000047test('center', 'foo', 0)
48test('center', 'foo', 3)
49test('center', 'foo', 16)
Guido van Rossum9a744a91999-04-08 20:27:54 +000050
Guido van Rossum3e06ab12000-06-29 19:35:29 +000051test('ljust', 'foo', 0)
52test('ljust', 'foo', 3)
53test('ljust', 'foo', 16)
Guido van Rossum9a744a91999-04-08 20:27:54 +000054
Guido van Rossum3e06ab12000-06-29 19:35:29 +000055test('rjust', 'foo', 0)
56test('rjust', 'foo', 3)
57test('rjust', 'foo', 16)
Guido van Rossum9a744a91999-04-08 20:27:54 +000058
Guido van Rossum3e06ab12000-06-29 19:35:29 +000059test('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)
Guido van Rossum9a744a91999-04-08 20:27:54 +000065
Guido van Rossum3e06ab12000-06-29 19:35:29 +000066test('find', 'abcdefghiabc', 'abc')
67test('find', 'abcdefghiabc', 'abc', 1)
68test('find', 'abcdefghiabc', 'def', 4)
69test('rfind', 'abcdefghiabc', 'abc')
Guido van Rossum9a744a91999-04-08 20:27:54 +000070
Guido van Rossum3e06ab12000-06-29 19:35:29 +000071test('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)
Guido van Rossum9a744a91999-04-08 20:27:54 +000077
Guido van Rossum3e06ab12000-06-29 19:35:29 +000078test('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)
Guido van Rossum9a744a91999-04-08 20:27:54 +000084
Guido van Rossum9a744a91999-04-08 20:27:54 +000085
Guido van Rossum3e06ab12000-06-29 19:35:29 +000086test('lower', 'HeLLo')
87test('lower', 'hello')
88test('upper', 'HeLLo')
89test('upper', 'HELLO')
Guido van Rossum9a744a91999-04-08 20:27:54 +000090
Guido van Rossum3e06ab12000-06-29 19:35:29 +000091test('title', ' hello ')
92test('title', 'hello ')
93test('title', "fOrMaT thIs aS titLe String")
94test('title', "fOrMaT,thIs-aS*titLe;String")
95test('title', "getInt")
Guido van Rossum9a744a91999-04-08 20:27:54 +000096
Guido van Rossum3e06ab12000-06-29 19:35:29 +000097test('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)
Guido van Rossum9a744a91999-04-08 20:27:54 +0000101
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000102test('islower', 'a')
103test('islower', 'A')
104test('islower', '\n')
105test('islower', 'abc')
106test('islower', 'aBc')
107test('islower', 'abc\n')
Guido van Rossum9a744a91999-04-08 20:27:54 +0000108
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000109test('isupper', 'a')
110test('isupper', 'A')
111test('isupper', '\n')
112test('isupper', 'ABC')
113test('isupper', 'AbC')
114test('isupper', 'ABC\n')
Guido van Rossum9a744a91999-04-08 20:27:54 +0000115
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000116test('isdigit', ' 0123456789')
117test('isdigit', '56789')
118test('isdigit', '567.89')
119test('isdigit', '0123456789abc')
Guido van Rossum9a744a91999-04-08 20:27:54 +0000120
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000121test('isspace', '')
122test('isspace', ' ')
123test('isspace', ' \t')
124test('isspace', ' \t\f\n')
Guido van Rossum9a744a91999-04-08 20:27:54 +0000125
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000126test('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')
Guido van Rossum9a744a91999-04-08 20:27:54 +0000135
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000136test('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")
Guido van Rossum9a744a91999-04-08 20:27:54 +0000144
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000145test('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 ')
Guido van Rossum9a744a91999-04-08 20:27:54 +0000155
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000156# 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]
Guido van Rossum9a744a91999-04-08 20:27:54 +0000161
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000162test('join', '', ('a', 'b', 'c', 'd'))
163test('join', '', Sequence())
164test('join', '', 7)
Guido van Rossum9a744a91999-04-08 20:27:54 +0000165
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000166class BadSeq(Sequence):
167 def __init__(self): self.seq = [7, 'hello', 123L]
Guido van Rossum9a744a91999-04-08 20:27:54 +0000168
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000169test('join', '', BadSeq())
Guido van Rossum9a744a91999-04-08 20:27:54 +0000170
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000171test('strip', ' hello ')
172test('lstrip', ' hello ')
173test('rstrip', ' hello ')
174test('strip', 'hello')
Guido van Rossum9a744a91999-04-08 20:27:54 +0000175
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000176test('swapcase', 'HeLLo cOmpUteRs')
177transtable = string.maketrans("abc", "xyz")
178test('translate', 'xyzabcdef', transtable, 'def')
Guido van Rossum9a744a91999-04-08 20:27:54 +0000179
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000180transtable = 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+"'"