annotate Lib/test/string_tests.py @ 49258:25f809123df6 legacy-trunk

Issue #3925: Ignores shutil.rmtree error on cygwin too. Reviewed by Benjamin Peterson.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Sun, 21 Sep 2008 20:48:41 +0000
parents b5365f34640f
children 304bea8731d2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1 """
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
2 Common tests shared by test_str, test_unicode, test_userstring and test_string.
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
3 """
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
4
41589
37d82bee7a16 Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41532
diff changeset
5 import unittest, string, sys, struct
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
6 from test import test_support
13527
07c31182f237 add more tests of string.join variants to run_method_tests
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13517
diff changeset
7 from UserList import UserList
07c31182f237 add more tests of string.join variants to run_method_tests
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13517
diff changeset
8
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
9 class Sequence:
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
10 def __init__(self, seq='wxyz'): self.seq = seq
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
11 def __len__(self): return len(self.seq)
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
12 def __getitem__(self, i): return self.seq[i]
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
13
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
14 class BadSeq1(Sequence):
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
15 def __init__(self): self.seq = [7, 'hello', 123L]
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
16
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
17 class BadSeq2(Sequence):
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
18 def __init__(self): self.seq = ['a', 'b', 'c']
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
19 def __len__(self): return 8
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
20
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
21 class CommonTest(unittest.TestCase):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
22 # This testcase contains test that can be used in all
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
23 # stringlike classes. Currently this is str, unicode
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
24 # UserString and the string module.
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
25
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
26 # The type to be tested
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
27 # Change in subclasses to change the behaviour of fixtesttype()
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
28 type2test = None
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
29
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
30 # All tests pass their arguments to the testing methods
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
31 # as str objects. fixtesttype() can be used to propagate
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
32 # these arguments to the appropriate type
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
33 def fixtype(self, obj):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
34 if isinstance(obj, str):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
35 return self.__class__.type2test(obj)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
36 elif isinstance(obj, list):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
37 return [self.fixtype(x) for x in obj]
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
38 elif isinstance(obj, tuple):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
39 return tuple([self.fixtype(x) for x in obj])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
40 elif isinstance(obj, dict):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
41 return dict([
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
42 (self.fixtype(key), self.fixtype(value))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
43 for (key, value) in obj.iteritems()
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
44 ])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
45 else:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
46 return obj
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
47
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
48 # check that object.method(*args) returns result
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
49 def checkequal(self, result, object, methodname, *args):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
50 result = self.fixtype(result)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
51 object = self.fixtype(object)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
52 args = self.fixtype(args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
53 realresult = getattr(object, methodname)(*args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
54 self.assertEqual(
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
55 result,
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
56 realresult
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
57 )
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
58 # if the original is returned make sure that
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
59 # this doesn't happen with subclasses
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
60 if object == realresult:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
61 class subtype(self.__class__.type2test):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
62 pass
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
63 object = subtype(object)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
64 realresult = getattr(object, methodname)(*args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
65 self.assert_(object is not realresult)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
66
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
67 # check that object.method(*args) raises exc
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
68 def checkraises(self, exc, object, methodname, *args):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
69 object = self.fixtype(object)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
70 args = self.fixtype(args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
71 self.assertRaises(
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
72 exc,
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
73 getattr(object, methodname),
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
74 *args
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
75 )
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
76
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
77 # call object.method(*args) without any checks
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
78 def checkcall(self, object, methodname, *args):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
79 object = self.fixtype(object)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
80 args = self.fixtype(args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
81 getattr(object, methodname)(*args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
82
33833
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
83 def test_hash(self):
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
84 # SF bug 1054139: += optimization was not invalidating cached hash value
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
85 a = self.type2test('DNSSEC')
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
86 b = self.type2test('')
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
87 for c in a:
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
88 b += c
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
89 hash(b)
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
90 self.assertEqual(hash(a), hash(b))
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
91
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
92 def test_capitalize(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
93 self.checkequal(' hello ', ' hello ', 'capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
94 self.checkequal('Hello ', 'Hello ','capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
95 self.checkequal('Hello ', 'hello ','capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
96 self.checkequal('Aaaa', 'aaaa', 'capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
97 self.checkequal('Aaaa', 'AaAa', 'capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
98
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
99 self.checkraises(TypeError, 'hello', 'capitalize', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
100
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
101 def test_count(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
102 self.checkequal(3, 'aaa', 'count', 'a')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
103 self.checkequal(0, 'aaa', 'count', 'b')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
104 self.checkequal(3, 'aaa', 'count', 'a')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
105 self.checkequal(0, 'aaa', 'count', 'b')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
106 self.checkequal(3, 'aaa', 'count', 'a')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
107 self.checkequal(0, 'aaa', 'count', 'b')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
108 self.checkequal(0, 'aaa', 'count', 'b')
38492
d5bdfd120232 fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
109 self.checkequal(2, 'aaa', 'count', 'a', 1)
d5bdfd120232 fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
110 self.checkequal(0, 'aaa', 'count', 'a', 10)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
111 self.checkequal(1, 'aaa', 'count', 'a', -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
112 self.checkequal(3, 'aaa', 'count', 'a', -10)
38492
d5bdfd120232 fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
113 self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
d5bdfd120232 fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
114 self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
115 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
116 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
38492
d5bdfd120232 fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
117 self.checkequal(3, 'aaa', 'count', '', 1)
38516
2fe3233bd426 changed count to return 0 for slices outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38514
diff changeset
118 self.checkequal(1, 'aaa', 'count', '', 3)
2fe3233bd426 changed count to return 0 for slices outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38514
diff changeset
119 self.checkequal(0, 'aaa', 'count', '', 10)
38492
d5bdfd120232 fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
120 self.checkequal(2, 'aaa', 'count', '', -1)
d5bdfd120232 fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
121 self.checkequal(4, 'aaa', 'count', '', -10)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
122
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
123 self.checkraises(TypeError, 'hello', 'count')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
124 self.checkraises(TypeError, 'hello', 'count', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
125
34621
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
126 # For a variety of combinations,
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
127 # verify that str.count() matches an equivalent function
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
128 # replacing all occurrences and then differencing the string lengths
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
129 charset = ['', 'a', 'b']
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
130 digits = 7
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
131 base = len(charset)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
132 teststrings = set()
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
133 for i in xrange(base ** digits):
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
134 entry = []
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
135 for j in xrange(digits):
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
136 i, m = divmod(i, base)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
137 entry.append(charset[m])
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
138 teststrings.add(''.join(entry))
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
139 teststrings = list(teststrings)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
140 for i in teststrings:
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
141 i = self.fixtype(i)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
142 n = len(i)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
143 for j in teststrings:
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
144 r1 = i.count(j)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
145 if j:
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
146 r2, rem = divmod(n - len(i.replace(j, '')), len(j))
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
147 else:
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
148 r2, rem = len(i)+1, 0
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
149 if rem or r1 != r2:
39302
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
150 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
151 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
34621
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
152
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
153 def test_find(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
154 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
155 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
156 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
157
38514
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
158 self.checkequal(0, 'abc', 'find', '', 0)
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
159 self.checkequal(3, 'abc', 'find', '', 3)
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
160 self.checkequal(-1, 'abc', 'find', '', 4)
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
161
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
162 # to check the ability to pass None as defaults
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
163 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a')
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
164 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
165 self.checkequal(-1, 'rrarrrrrrrrra', 'find', 'a', 4, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
166 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4, None)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
167 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a', None, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
168
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
169 self.checkraises(TypeError, 'hello', 'find')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
170 self.checkraises(TypeError, 'hello', 'find', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
171
34620
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
172 # For a variety of combinations,
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
173 # verify that str.find() matches __contains__
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
174 # and that the found substring is really at that location
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
175 charset = ['', 'a', 'b', 'c']
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
176 digits = 5
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
177 base = len(charset)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
178 teststrings = set()
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
179 for i in xrange(base ** digits):
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
180 entry = []
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
181 for j in xrange(digits):
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
182 i, m = divmod(i, base)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
183 entry.append(charset[m])
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
184 teststrings.add(''.join(entry))
34621
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
185 teststrings = list(teststrings)
34620
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
186 for i in teststrings:
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
187 i = self.fixtype(i)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
188 for j in teststrings:
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
189 loc = i.find(j)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
190 r1 = (loc != -1)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
191 r2 = j in i
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
192 if r1 != r2:
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
193 self.assertEqual(r1, r2)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
194 if loc != -1:
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
195 self.assertEqual(i[loc:loc+len(j)], j)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
196
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
197 def test_rfind(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
198 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
199 self.checkequal(12, 'abcdefghiabc', 'rfind', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
200 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
201 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
202
38514
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
203 self.checkequal(3, 'abc', 'rfind', '', 0)
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
204 self.checkequal(3, 'abc', 'rfind', '', 3)
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
205 self.checkequal(-1, 'abc', 'rfind', '', 4)
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
206
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
207 # to check the ability to pass None as defaults
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
208 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a')
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
209 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
210 self.checkequal(-1, 'rrarrrrrrrrra', 'rfind', 'a', 4, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
211 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4, None)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
212 self.checkequal( 2, 'rrarrrrrrrrra', 'rfind', 'a', None, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
213
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
214 self.checkraises(TypeError, 'hello', 'rfind')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
215 self.checkraises(TypeError, 'hello', 'rfind', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
216
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
217 def test_index(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
218 self.checkequal(0, 'abcdefghiabc', 'index', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
219 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
220 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
221 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
222
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
223 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
224 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
225 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
226 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
227
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
228 # to check the ability to pass None as defaults
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
229 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a')
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
230 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
231 self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
232 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
233 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
234
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
235 self.checkraises(TypeError, 'hello', 'index')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
236 self.checkraises(TypeError, 'hello', 'index', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
237
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
238 def test_rindex(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
239 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
240 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
241 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
242 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
243
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
244 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
245 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
246 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
247 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
248 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
249
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
250 # to check the ability to pass None as defaults
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
251 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a')
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
252 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
253 self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', 'a', 4, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
254 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4, None)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
255 self.checkequal( 2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
256
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
257 self.checkraises(TypeError, 'hello', 'rindex')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
258 self.checkraises(TypeError, 'hello', 'rindex', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
259
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
260 def test_lower(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
261 self.checkequal('hello', 'HeLLo', 'lower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
262 self.checkequal('hello', 'hello', 'lower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
263 self.checkraises(TypeError, 'hello', 'lower', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
264
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
265 def test_upper(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
266 self.checkequal('HELLO', 'HeLLo', 'upper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
267 self.checkequal('HELLO', 'HELLO', 'upper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
268 self.checkraises(TypeError, 'hello', 'upper', 42)
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
269
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
270 def test_expandtabs(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
271 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
272 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
273 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
274 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
275 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
276 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
277 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
42024
b94f284805be Fix a bug when there was a newline in the string expandtabs was called on.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41589
diff changeset
278 self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
279
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
280 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
42024
b94f284805be Fix a bug when there was a newline in the string expandtabs was called on.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41589
diff changeset
281 # This test is only valid when sizeof(int) == sizeof(void*) == 4.
b94f284805be Fix a bug when there was a newline in the string expandtabs was called on.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41589
diff changeset
282 if sys.maxint < (1 << 32) and struct.calcsize('P') == 4:
b94f284805be Fix a bug when there was a newline in the string expandtabs was called on.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41589
diff changeset
283 self.checkraises(OverflowError,
b94f284805be Fix a bug when there was a newline in the string expandtabs was called on.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41589
diff changeset
284 '\ta\n\tb', 'expandtabs', sys.maxint)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
285
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
286 def test_split(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
287 self.checkequal(['this', 'is', 'the', 'split', 'function'],
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
288 'this is the split function', 'split')
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
289
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
290 # by whitespace
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
291 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
292 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
293 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
294 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
295 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
296 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
297 sys.maxint-1)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
298 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
38373
4bfeab230e38 Test for more edge strip cases; leading and trailing separator gets removed
Andrew Dalke <dalke@dalkescientific.com>
parents: 38361
diff changeset
299 self.checkequal(['a b c d'], ' a b c d', 'split', None, 0)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
300 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
301
38340
8a99cdef0c94 Added a few more test cases for whitespace split. These strings have leading whitespace.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38335
diff changeset
302 self.checkequal([], ' ', 'split')
8a99cdef0c94 Added a few more test cases for whitespace split. These strings have leading whitespace.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38335
diff changeset
303 self.checkequal(['a'], ' a ', 'split')
8a99cdef0c94 Added a few more test cases for whitespace split. These strings have leading whitespace.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38335
diff changeset
304 self.checkequal(['a', 'b'], ' a b ', 'split')
8a99cdef0c94 Added a few more test cases for whitespace split. These strings have leading whitespace.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38335
diff changeset
305 self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
8a99cdef0c94 Added a few more test cases for whitespace split. These strings have leading whitespace.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38335
diff changeset
306 self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
8a99cdef0c94 Added a few more test cases for whitespace split. These strings have leading whitespace.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38335
diff changeset
307 self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
38342
5382aaf3a0da Added split whitespace checks for characters other than space.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38340
diff changeset
308 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
309 aaa = ' a '*20
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
310 self.checkequal(['a']*20, aaa, 'split')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
311 self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
312 self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
38340
8a99cdef0c94 Added a few more test cases for whitespace split. These strings have leading whitespace.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38335
diff changeset
313
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
314 # by a char
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
315 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
316 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
317 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
318 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
319 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
320 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
321 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
322 sys.maxint-2)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
323 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
324 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
325 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
326 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
327 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
328 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
329
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
330 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
331 self.checkequal(['a']*15 +['a|a|a|a|a'],
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
332 ('a|'*20)[:-1], 'split', '|', 15)
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
333
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
334 # by string
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
335 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
336 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
337 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
338 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
339 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
340 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
341 sys.maxint-10)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
342 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
343 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
344 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
345 self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
346 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
347 'split', 'test')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
348 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
349 self.checkequal(['', ''], 'aaa', 'split', 'aaa')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
350 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
351 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
352 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
353 self.checkequal([''], '', 'split', 'aaa')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
354 self.checkequal(['aa'], 'aa', 'split', 'aaa')
38357
be7415b0a318 Test cases for off-by-one errors in string split with multicharacter pattern.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38356
diff changeset
355 self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')
be7415b0a318 Test cases for off-by-one errors in string split with multicharacter pattern.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38356
diff changeset
356 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
357
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
358 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
359 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
360 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
361 'split', 'BLAH', 18)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
362
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
363 # mixed use of str and unicode
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
364 self.checkequal([u'a', u'b', u'c d'], 'a b c d', 'split', u' ', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
365
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
366 # argument type
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
367 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
368
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
369 # null case
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
370 self.checkraises(ValueError, 'hello', 'split', '')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
371 self.checkraises(ValueError, 'hello', 'split', '', 0)
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
372
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
373 def test_rsplit(self):
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
374 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
375 'this is the rsplit function', 'rsplit')
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
376
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
377 # by whitespace
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
378 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
379 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
380 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
381 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
382 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
383 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
384 sys.maxint-20)
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
385 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
38373
4bfeab230e38 Test for more edge strip cases; leading and trailing separator gets removed
Andrew Dalke <dalke@dalkescientific.com>
parents: 38361
diff changeset
386 self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0)
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
387 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
388
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
389 self.checkequal([], ' ', 'rsplit')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
390 self.checkequal(['a'], ' a ', 'rsplit')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
391 self.checkequal(['a', 'b'], ' a b ', 'rsplit')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
392 self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
393 self.checkequal([' a b','c'], ' a b c ', 'rsplit',
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
394 None, 1)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
395 self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
396 None, 2)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
397 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
398 aaa = ' a '*20
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
399 self.checkequal(['a']*20, aaa, 'rsplit')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
400 self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
401 self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
402
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
403
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
404 # by a char
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
405 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
406 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
407 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
408 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
409 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
410 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
411 sys.maxint-100)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
412 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
413 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
414 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
415 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
416 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
417
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
418 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
419
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
420 self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
421 self.checkequal(['a|a|a|a|a']+['a']*15,
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
422 ('a|'*20)[:-1], 'rsplit', '|', 15)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
423
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
424 # by string
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
425 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
426 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
427 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
428 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
429 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
430 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
431 sys.maxint-5)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
432 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
433 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
434 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
435 self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
436 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
437 'rsplit', 'test')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
438 self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
439 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
440 self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
441 self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
442 self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
443 self.checkequal([''], '', 'rsplit', 'aaa')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
444 self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
445 self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
446 self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
447
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
448 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
449 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
450 self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
451 'rsplit', 'BLAH', 18)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
452
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
453 # mixed use of str and unicode
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
454 self.checkequal([u'a b', u'c', u'd'], 'a b c d', 'rsplit', u' ', 2)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
455
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
456 # argument type
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
457 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
458
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
459 # null case
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
460 self.checkraises(ValueError, 'hello', 'rsplit', '')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
461 self.checkraises(ValueError, 'hello', 'rsplit', '', 0)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
462
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
463 def test_strip(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
464 self.checkequal('hello', ' hello ', 'strip')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
465 self.checkequal('hello ', ' hello ', 'lstrip')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
466 self.checkequal(' hello', ' hello ', 'rstrip')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
467 self.checkequal('hello', 'hello', 'strip')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
468
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
469 # strip/lstrip/rstrip with None arg
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
470 self.checkequal('hello', ' hello ', 'strip', None)
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
471 self.checkequal('hello ', ' hello ', 'lstrip', None)
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
472 self.checkequal(' hello', ' hello ', 'rstrip', None)
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
473 self.checkequal('hello', 'hello', 'strip', None)
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
474
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
475 # strip/lstrip/rstrip with str arg
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
476 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
477 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
478 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
479 self.checkequal('hello', 'hello', 'strip', 'xyz')
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
480
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
481 # strip/lstrip/rstrip with unicode arg
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
482 if test_support.have_unicode:
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
483 self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
484 'strip', unicode('xyz', 'ascii'))
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
485 self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
486 'lstrip', unicode('xyz', 'ascii'))
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
487 self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
488 'rstrip', unicode('xyz', 'ascii'))
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45500
diff changeset
489 # XXX
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45500
diff changeset
490 #self.checkequal(unicode('hello', 'ascii'), 'hello',
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45500
diff changeset
491 # 'strip', unicode('xyz', 'ascii'))
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
492
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
493 self.checkraises(TypeError, 'hello', 'strip', 42, 42)
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
494 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
495 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
496
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
497 def test_ljust(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
498 self.checkequal('abc ', 'abc', 'ljust', 10)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
499 self.checkequal('abc ', 'abc', 'ljust', 6)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
500 self.checkequal('abc', 'abc', 'ljust', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
501 self.checkequal('abc', 'abc', 'ljust', 2)
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30216
diff changeset
502 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
503 self.checkraises(TypeError, 'abc', 'ljust')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
504
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
505 def test_rjust(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
506 self.checkequal(' abc', 'abc', 'rjust', 10)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
507 self.checkequal(' abc', 'abc', 'rjust', 6)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
508 self.checkequal('abc', 'abc', 'rjust', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
509 self.checkequal('abc', 'abc', 'rjust', 2)
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30216
diff changeset
510 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
511 self.checkraises(TypeError, 'abc', 'rjust')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
512
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
513 def test_center(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
514 self.checkequal(' abc ', 'abc', 'center', 10)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
515 self.checkequal(' abc ', 'abc', 'center', 6)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
516 self.checkequal('abc', 'abc', 'center', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
517 self.checkequal('abc', 'abc', 'center', 2)
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30216
diff changeset
518 self.checkequal('***abc****', 'abc', 'center', 10, '*')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
519 self.checkraises(TypeError, 'abc', 'center')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
520
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
521 def test_swapcase(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
522 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
523
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
524 self.checkraises(TypeError, 'hello', 'swapcase', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
525
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
526 def test_replace(self):
38279
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
527 EQ = self.checkequal
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
528
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
529 # Operations on the empty string
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
530 EQ("", "", "replace", "", "")
38539
b9b036f02207 Re-enable a new empty-string test added during the NFS sprint,
Tim Peters <tim.peters@gmail.com>
parents: 38516
diff changeset
531 EQ("A", "", "replace", "", "A")
38279
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
532 EQ("", "", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
533 EQ("", "", "replace", "A", "A")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
534 EQ("", "", "replace", "", "", 100)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
535 EQ("", "", "replace", "", "", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
536
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
537 # interleave (from=="", 'to' gets inserted everywhere)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
538 EQ("A", "A", "replace", "", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
539 EQ("*A*", "A", "replace", "", "*")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
540 EQ("*1A*1", "A", "replace", "", "*1")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
541 EQ("*-#A*-#", "A", "replace", "", "*-#")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
542 EQ("*-A*-A*-", "AA", "replace", "", "*-")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
543 EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
544 EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
545 EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
546 EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
547 EQ("*-A*-A", "AA", "replace", "", "*-", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
548 EQ("*-AA", "AA", "replace", "", "*-", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
549 EQ("AA", "AA", "replace", "", "*-", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
550
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
551 # single character deletion (from=="A", to=="")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
552 EQ("", "A", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
553 EQ("", "AAA", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
554 EQ("", "AAA", "replace", "A", "", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
555 EQ("", "AAA", "replace", "A", "", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
556 EQ("", "AAA", "replace", "A", "", 4)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
557 EQ("", "AAA", "replace", "A", "", 3)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
558 EQ("A", "AAA", "replace", "A", "", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
559 EQ("AA", "AAA", "replace", "A", "", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
560 EQ("AAA", "AAA", "replace", "A", "", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
561 EQ("", "AAAAAAAAAA", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
562 EQ("BCD", "ABACADA", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
563 EQ("BCD", "ABACADA", "replace", "A", "", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
564 EQ("BCD", "ABACADA", "replace", "A", "", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
565 EQ("BCD", "ABACADA", "replace", "A", "", 5)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
566 EQ("BCD", "ABACADA", "replace", "A", "", 4)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
567 EQ("BCDA", "ABACADA", "replace", "A", "", 3)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
568 EQ("BCADA", "ABACADA", "replace", "A", "", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
569 EQ("BACADA", "ABACADA", "replace", "A", "", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
570 EQ("ABACADA", "ABACADA", "replace", "A", "", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
571 EQ("BCD", "ABCAD", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
572 EQ("BCD", "ABCADAA", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
573 EQ("BCD", "BCD", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
574 EQ("*************", "*************", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
575 EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
576
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
577 # substring deletion (from=="the", to=="")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
578 EQ("", "the", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
579 EQ("ater", "theater", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
580 EQ("", "thethe", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
581 EQ("", "thethethethe", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
582 EQ("aaaa", "theatheatheathea", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
583 EQ("that", "that", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
584 EQ("thaet", "thaet", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
585 EQ("here and re", "here and there", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
586 EQ("here and re and re", "here and there and there",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
587 "replace", "the", "", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
588 EQ("here and re and re", "here and there and there",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
589 "replace", "the", "", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
590 EQ("here and re and re", "here and there and there",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
591 "replace", "the", "", 3)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
592 EQ("here and re and re", "here and there and there",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
593 "replace", "the", "", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
594 EQ("here and re and there", "here and there and there",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
595 "replace", "the", "", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
596 EQ("here and there and there", "here and there and there",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
597 "replace", "the", "", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
598 EQ("here and re and re", "here and there and there", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
599
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
600 EQ("abc", "abc", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
601 EQ("abcdefg", "abcdefg", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
602
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
603 # substring deletion (from=="bob", to=="")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
604 EQ("bob", "bbobob", "replace", "bob", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
605 EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
606 EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
607 EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")
38280
32ba3a0c6040 We can't leave the checked-in tests broken.
Tim Peters <tim.peters@gmail.com>
parents: 38279
diff changeset
608
38279
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
609 # single character replace in place (len(from)==len(to)==1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
610 EQ("Who goes there?", "Who goes there?", "replace", "o", "o")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
611 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
612 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
613 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
614 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
615 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
616 EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
617 EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
618
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
619 EQ("Who goes there?", "Who goes there?", "replace", "a", "q")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
620 EQ("who goes there?", "Who goes there?", "replace", "W", "w")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
621 EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
622 EQ("Who goes there!", "Who goes there?", "replace", "?", "!")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
623 EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
624
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
625 EQ("Who goes there?", "Who goes there?", "replace", ".", "!")
38280
32ba3a0c6040 We can't leave the checked-in tests broken.
Tim Peters <tim.peters@gmail.com>
parents: 38279
diff changeset
626
38279
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
627 # substring replace in place (len(from)==len(to) > 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
628 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
629 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
630 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
631 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
632 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
633 EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
634 EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
635 EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
636 EQ("cobob", "bobob", "replace", "bob", "cob")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
637 EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
638 EQ("bobob", "bobob", "replace", "bot", "bot")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
639
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
640 # replace single character (len(from)==1, len(to)>1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
641 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
642 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
643 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
644 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
645 EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
646 EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
647 EQ("A----B----C----", "A.B.C.", "replace", ".", "----")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
648
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
649 EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
650
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
651 # replace substring (len(from)>1, len(to)!=len(from))
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
652 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
653 "replace", "spam", "ham")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
654 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
655 "replace", "spam", "ham", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
656 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
657 "replace", "spam", "ham", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
658 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
659 "replace", "spam", "ham", 4)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
660 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
661 "replace", "spam", "ham", 3)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
662 EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
663 "replace", "spam", "ham", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
664 EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
665 "replace", "spam", "ham", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
666 EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
667 "replace", "spam", "ham", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
668
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
669 EQ("bobob", "bobobob", "replace", "bobob", "bob")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
670 EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
671 EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
38280
32ba3a0c6040 We can't leave the checked-in tests broken.
Tim Peters <tim.peters@gmail.com>
parents: 38279
diff changeset
672
39302
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
673 ba = buffer('a')
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
674 bb = buffer('b')
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
675 EQ("bbc", "abc", "replace", ba, bb)
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
676 EQ("aac", "abc", "replace", bb, ba)
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
677
38280
32ba3a0c6040 We can't leave the checked-in tests broken.
Tim Peters <tim.peters@gmail.com>
parents: 38279
diff changeset
678 #
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
679 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
680 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
681 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
682 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
683 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
684 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
685 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
686 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
687 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
688 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
689 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
690 self.checkequal('abc', 'abc', 'replace', '', '-', 0)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
691 self.checkequal('', '', 'replace', '', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
692 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
693 self.checkequal('abc', 'abc', 'replace', 'xy', '--')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
694 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
695 # MemoryError due to empty result (platform malloc issue when requesting
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
696 # 0 bytes).
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
697 self.checkequal('', '123', 'replace', '123', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
698 self.checkequal('', '123123', 'replace', '123', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
699 self.checkequal('x', '123x123', 'replace', '123', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
700
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
701 self.checkraises(TypeError, 'hello', 'replace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
702 self.checkraises(TypeError, 'hello', 'replace', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
703 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
704 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
705
38299
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
706 def test_replace_overflow(self):
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
707 # Check for overflow checking on 32 bit machines
41589
37d82bee7a16 Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41532
diff changeset
708 if sys.maxint != 2147483647 or struct.calcsize("P") > 4:
38299
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
709 return
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
710 A2_16 = "A" * (2**16)
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
711 self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
712 self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
713 self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
38279
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
714
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
715 def test_zfill(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
716 self.checkequal('123', '123', 'zfill', 2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
717 self.checkequal('123', '123', 'zfill', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
718 self.checkequal('0123', '123', 'zfill', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
719 self.checkequal('+123', '+123', 'zfill', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
720 self.checkequal('+123', '+123', 'zfill', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
721 self.checkequal('+0123', '+123', 'zfill', 5)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
722 self.checkequal('-123', '-123', 'zfill', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
723 self.checkequal('-123', '-123', 'zfill', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
724 self.checkequal('-0123', '-123', 'zfill', 5)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
725 self.checkequal('000', '', 'zfill', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
726 self.checkequal('34', '34', 'zfill', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
727 self.checkequal('0034', '34', 'zfill', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
728
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
729 self.checkraises(TypeError, '123', 'zfill')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
730
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45500
diff changeset
731 # XXX alias for py3k forward compatibility
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45500
diff changeset
732 BaseTest = CommonTest
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45500
diff changeset
733
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
734 class MixinStrUnicodeUserStringTest:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
735 # additional tests that only work for
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
736 # stringlike objects, i.e. str, unicode, UserString
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
737 # (but not the string module)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
738
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
739 def test_islower(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
740 self.checkequal(False, '', 'islower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
741 self.checkequal(True, 'a', 'islower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
742 self.checkequal(False, 'A', 'islower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
743 self.checkequal(False, '\n', 'islower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
744 self.checkequal(True, 'abc', 'islower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
745 self.checkequal(False, 'aBc', 'islower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
746 self.checkequal(True, 'abc\n', 'islower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
747 self.checkraises(TypeError, 'abc', 'islower', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
748
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
749 def test_isupper(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
750 self.checkequal(False, '', 'isupper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
751 self.checkequal(False, 'a', 'isupper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
752 self.checkequal(True, 'A', 'isupper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
753 self.checkequal(False, '\n', 'isupper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
754 self.checkequal(True, 'ABC', 'isupper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
755 self.checkequal(False, 'AbC', 'isupper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
756 self.checkequal(True, 'ABC\n', 'isupper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
757 self.checkraises(TypeError, 'abc', 'isupper', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
758
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
759 def test_istitle(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
760 self.checkequal(False, '', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
761 self.checkequal(False, 'a', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
762 self.checkequal(True, 'A', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
763 self.checkequal(False, '\n', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
764 self.checkequal(True, 'A Titlecased Line', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
765 self.checkequal(True, 'A\nTitlecased Line', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
766 self.checkequal(True, 'A Titlecased, Line', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
767 self.checkequal(False, 'Not a capitalized String', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
768 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
769 self.checkequal(False, 'Not--a Titlecase String', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
770 self.checkequal(False, 'NOT', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
771 self.checkraises(TypeError, 'abc', 'istitle', 42)
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
772
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
773 def test_isspace(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
774 self.checkequal(False, '', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
775 self.checkequal(False, 'a', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
776 self.checkequal(True, ' ', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
777 self.checkequal(True, '\t', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
778 self.checkequal(True, '\r', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
779 self.checkequal(True, '\n', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
780 self.checkequal(True, ' \t\r\n', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
781 self.checkequal(False, ' \t\r\na', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
782 self.checkraises(TypeError, 'abc', 'isspace', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
783
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
784 def test_isalpha(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
785 self.checkequal(False, '', 'isalpha')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
786 self.checkequal(True, 'a', 'isalpha')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
787 self.checkequal(True, 'A', 'isalpha')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
788 self.checkequal(False, '\n', 'isalpha')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
789 self.checkequal(True, 'abc', 'isalpha')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
790 self.checkequal(False, 'aBc123', 'isalpha')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
791 self.checkequal(False, 'abc\n', 'isalpha')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
792 self.checkraises(TypeError, 'abc', 'isalpha', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
793
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
794 def test_isalnum(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
795 self.checkequal(False, '', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
796 self.checkequal(True, 'a', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
797 self.checkequal(True, 'A', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
798 self.checkequal(False, '\n', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
799 self.checkequal(True, '123abc456', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
800 self.checkequal(True, 'a1b3c', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
801 self.checkequal(False, 'aBc000 ', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
802 self.checkequal(False, 'abc\n', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
803 self.checkraises(TypeError, 'abc', 'isalnum', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
804
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
805 def test_isdigit(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
806 self.checkequal(False, '', 'isdigit')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
807 self.checkequal(False, 'a', 'isdigit')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
808 self.checkequal(True, '0', 'isdigit')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
809 self.checkequal(True, '0123456789', 'isdigit')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
810 self.checkequal(False, '0123456789a', 'isdigit')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
811
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
812 self.checkraises(TypeError, 'abc', 'isdigit', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
813
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
814 def test_title(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
815 self.checkequal(' Hello ', ' hello ', 'title')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
816 self.checkequal('Hello ', 'hello ', 'title')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
817 self.checkequal('Hello ', 'Hello ', 'title')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
818 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
819 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
820 self.checkequal('Getint', "getInt", 'title')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
821 self.checkraises(TypeError, 'hello', 'title', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
822
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
823 def test_splitlines(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
824 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
825 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
826 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
827 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
828 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
829 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
830 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
831
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
832 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
833
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
834 def test_startswith(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
835 self.checkequal(True, 'hello', 'startswith', 'he')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
836 self.checkequal(True, 'hello', 'startswith', 'hello')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
837 self.checkequal(False, 'hello', 'startswith', 'hello world')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
838 self.checkequal(True, 'hello', 'startswith', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
839 self.checkequal(False, 'hello', 'startswith', 'ello')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
840 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
841 self.checkequal(True, 'hello', 'startswith', 'o', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
842 self.checkequal(False, 'hello', 'startswith', 'o', 5)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
843 self.checkequal(True, 'hello', 'startswith', '', 5)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
844 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
845 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
846 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
847 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
848
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
849 # test negative indices
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
850 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
851 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
852 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
853 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
854 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
855 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
856 self.checkequal(False, 'hello', 'startswith', 'o', -2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
857 self.checkequal(True, 'hello', 'startswith', 'o', -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
858 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
859 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
860
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
861 self.checkraises(TypeError, 'hello', 'startswith')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
862 self.checkraises(TypeError, 'hello', 'startswith', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
863
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
864 # test tuple arguments
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
865 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
866 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
867 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
868 self.checkequal(False, 'hello', 'startswith', ())
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
869 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
870 'rld', 'lowo'), 3)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
871 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
872 'rld'), 3)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
873 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
874 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
875 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
876
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
877 self.checkraises(TypeError, 'hello', 'startswith', (42,))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
878
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
879 def test_endswith(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
880 self.checkequal(True, 'hello', 'endswith', 'lo')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
881 self.checkequal(False, 'hello', 'endswith', 'he')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
882 self.checkequal(True, 'hello', 'endswith', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
883 self.checkequal(False, 'hello', 'endswith', 'hello world')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
884 self.checkequal(False, 'helloworld', 'endswith', 'worl')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
885 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
886 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
887 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
888 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
889 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
890 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
891 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
892 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
893 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
894
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
895 # test negative indices
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
896 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
897 self.checkequal(False, 'hello', 'endswith', 'he', -2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
898 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
899 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
900 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
901 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
902 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
903 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
904 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
905 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
906 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
907 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
908 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
909
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
910 self.checkraises(TypeError, 'hello', 'endswith')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
911 self.checkraises(TypeError, 'hello', 'endswith', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
912
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
913 # test tuple arguments
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
914 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
915 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
916 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
917 self.checkequal(False, 'hello', 'endswith', ())
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
918 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
919 'rld', 'lowo'), 3)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
920 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
921 'rld'), 3, -1)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
922 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
923 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
924 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
925
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
926 self.checkraises(TypeError, 'hello', 'endswith', (42,))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
927
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
928 def test___contains__(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
929 self.checkequal(True, '', '__contains__', '') # vereq('' in '', True)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
930 self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
931 self.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
932 self.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
933 self.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
934 self.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
935 self.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
936 self.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
937 self.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
938
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
939 def test_subscript(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
940 self.checkequal(u'a', 'abc', '__getitem__', 0)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
941 self.checkequal(u'c', 'abc', '__getitem__', -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
942 self.checkequal(u'a', 'abc', '__getitem__', 0L)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
943 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 3))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
944 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
945 self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
946 self.checkequal(u'', 'abc', '__getitem__', slice(0, 0))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
947
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
948 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
949
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
950 def test_slice(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
951 self.checkequal('abc', 'abc', '__getslice__', 0, 1000)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
952 self.checkequal('abc', 'abc', '__getslice__', 0, 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
953 self.checkequal('ab', 'abc', '__getslice__', 0, 2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
954 self.checkequal('bc', 'abc', '__getslice__', 1, 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
955 self.checkequal('b', 'abc', '__getslice__', 1, 2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
956 self.checkequal('', 'abc', '__getslice__', 2, 2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
957 self.checkequal('', 'abc', '__getslice__', 1000, 1000)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
958 self.checkequal('', 'abc', '__getslice__', 2000, 1000)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
959 self.checkequal('', 'abc', '__getslice__', 2, 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
960
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
961 self.checkraises(TypeError, 'abc', '__getslice__', 'def')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
962
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
963 def test_extended_getslice(self):
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
964 # Test extended slicing by comparing with list slicing.
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
965 s = string.ascii_letters + string.digits
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
966 indices = (0, None, 1, 3, 41, -1, -2, -37)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
967 for start in indices:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
968 for stop in indices:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
969 # Skip step 0 (invalid)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
970 for step in indices[1:]:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
971 L = list(s)[start:stop:step]
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
972 self.checkequal(u"".join(L), s, '__getitem__',
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
973 slice(start, stop, step))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
974
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
975 def test_mul(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
976 self.checkequal('', 'abc', '__mul__', -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
977 self.checkequal('', 'abc', '__mul__', 0)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
978 self.checkequal('abc', 'abc', '__mul__', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
979 self.checkequal('abcabcabc', 'abc', '__mul__', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
980 self.checkraises(TypeError, 'abc', '__mul__')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
981 self.checkraises(TypeError, 'abc', '__mul__', '')
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35758
diff changeset
982 # XXX: on a 64-bit system, this doesn't raise an overflow error,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35758
diff changeset
983 # but either raises a MemoryError, or succeeds (if you have 54TiB)
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35758
diff changeset
984 #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
985
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
986 def test_join(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
987 # join now works with any sequence type
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
988 # moved here, because the argument order is
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
989 # different in string.join (see the test in
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
990 # test.test_string.StringTest.test_join)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
991 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
992 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
38709
67450db26f81 Apply perky's fix for #1503157: "/".join([u"", u""]) raising OverflowError.
Georg Brandl <georg@python.org>
parents: 38697
diff changeset
993 self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))
67450db26f81 Apply perky's fix for #1503157: "/".join([u"", u""]) raising OverflowError.
Georg Brandl <georg@python.org>
parents: 38697
diff changeset
994 self.checkequal('ac', '', 'join', ('a', '', 'c', ''))
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
995 self.checkequal('w x y z', ' ', 'join', Sequence())
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
996 self.checkequal('abc', 'a', 'join', ('abc',))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
997 self.checkequal('z', 'a', 'join', UserList(['z']))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
998 if test_support.have_unicode:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
999 self.checkequal(unicode('a.b.c'), unicode('.'), 'join', ['a', 'b', 'c'])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1000 self.checkequal(unicode('a.b.c'), '.', 'join', [unicode('a'), 'b', 'c'])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1001 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', unicode('b'), 'c'])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1002 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', 'b', unicode('c')])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1003 self.checkraises(TypeError, '.', 'join', ['a', unicode('b'), 3])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1004 for i in [5, 25, 125]:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1005 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1006 ['a' * i] * i)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1007 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1008 ('a' * i,) * i)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1010 self.checkraises(TypeError, ' ', 'join', BadSeq1())
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1011 self.checkequal('a b c', ' ', 'join', BadSeq2())
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1012
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1013 self.checkraises(TypeError, ' ', 'join')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1014 self.checkraises(TypeError, ' ', 'join', 7)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1015 self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123L]))
35758
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1016 try:
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1017 def f():
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1018 yield 4 + ""
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1019 self.fixtype(' ').join(f())
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1020 except TypeError, e:
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1021 if '+' not in str(e):
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1022 self.fail('join() ate exception message')
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1023 else:
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1024 self.fail('exception not raised')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1025
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1026 def test_formatting(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1027 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1028 self.checkequal('+10+', '+%d+', '__mod__', 10)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1029 self.checkequal('a', "%c", '__mod__', "a")
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1030 self.checkequal('a', "%c", '__mod__', "a")
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1031 self.checkequal('"', "%c", '__mod__', 34)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1032 self.checkequal('$', "%c", '__mod__', 36)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1033 self.checkequal('10', "%d", '__mod__', 10)
28478
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 28407
diff changeset
1034 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1035
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1036 for ordinal in (-100, 0x200000):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1037 # unicode raises ValueError, str raises OverflowError
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1038 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1039
45500
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1040 longvalue = sys.maxint + 10L
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1041 slongvalue = str(longvalue)
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1042 if slongvalue[-1] in ("L","l"): slongvalue = slongvalue[:-1]
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1043 self.checkequal(' 42', '%3ld', '__mod__', 42)
45500
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1044 self.checkequal('42', '%d', '__mod__', 42L)
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1045 self.checkequal('42', '%d', '__mod__', 42.0)
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1046 self.checkequal(slongvalue, '%d', '__mod__', longvalue)
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1047 self.checkcall('%d', '__mod__', float(longvalue))
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1048 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
30216
7ab1a41f16da SF bug #795506: Wrong handling of string format code for float values.
Raymond Hettinger <python@rcn.com>
parents: 28774
diff changeset
1049 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1050
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1051 self.checkraises(TypeError, 'abc', '__mod__')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1052 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1053 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1054 self.checkraises(TypeError, '%c', '__mod__', (None,))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1055 self.checkraises(ValueError, '%(foo', '__mod__', {})
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1056 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
45500
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1057 self.checkraises(TypeError, '%d', '__mod__', "42") # not numeric
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1058 self.checkraises(TypeError, '%d', '__mod__', (42+0j)) # no int/long conversion provided
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1059
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1060 # argument names with properly nested brackets are supported
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1061 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1062
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1063 # 100 is a magic number in PyUnicode_Format, this forces a resize
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1064 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1065
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1066 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1067 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1068 self.checkraises(ValueError, '%10', '__mod__', (42,))
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1069
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1070 def test_floatformatting(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1071 # float formatting
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1072 for prec in xrange(100):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1073 format = '%%.%if' % prec
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1074 value = 0.01
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1075 for x in xrange(60):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1076 value = value * 3.141592655 / 3.0 * 10.0
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1077 # The formatfloat() code in stringobject.c and
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1078 # unicodeobject.c uses a 120 byte buffer and switches from
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1079 # 'f' formatting to 'g' at precision 50, so we expect
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1080 # OverflowErrors for the ranges x < 50 and prec >= 67.
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1081 if x < 50 and prec >= 67:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1082 self.checkraises(OverflowError, format, "__mod__", value)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1083 else:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1084 self.checkcall(format, "__mod__", value)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1085
38296
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1086 def test_inplace_rewrites(self):
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1087 # Check that strings don't copy and modify cached single-character strings
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1088 self.checkequal('a', 'A', 'lower')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1089 self.checkequal(True, 'A', 'isupper')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1090 self.checkequal('A', 'a', 'upper')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1091 self.checkequal(True, 'a', 'islower')
38327
e9eb526b7111 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38299
diff changeset
1092
38296
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1093 self.checkequal('a', 'A', 'replace', 'A', 'a')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1094 self.checkequal(True, 'A', 'isupper')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1095
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1096 self.checkequal('A', 'a', 'capitalize')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1097 self.checkequal(True, 'a', 'islower')
38327
e9eb526b7111 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38299
diff changeset
1098
38296
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1099 self.checkequal('A', 'a', 'swapcase')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1100 self.checkequal(True, 'a', 'islower')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1101
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1102 self.checkequal('A', 'a', 'title')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1103 self.checkequal(True, 'a', 'islower')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1104
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1105 def test_partition(self):
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1106
38391
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1107 self.checkequal(('this is the par', 'ti', 'tion method'),
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1108 'this is the partition method', 'partition', 'ti')
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1109
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1110 # from raymond's original specification
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1111 S = 'http://www.python.org'
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1112 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1113 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1114 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1115 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1116
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1117 self.checkraises(ValueError, S, 'partition', '')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1118 self.checkraises(TypeError, S, 'partition', None)
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1119
48935
b5365f34640f Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46107
diff changeset
1120 # mixed use of str and unicode
b5365f34640f Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46107
diff changeset
1121 self.assertEqual('a/b/c'.partition(u'/'), ('a', '/', 'b/c'))
b5365f34640f Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46107
diff changeset
1122
38391
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1123 def test_rpartition(self):
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1124
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1125 self.checkequal(('this is the rparti', 'ti', 'on method'),
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1126 'this is the rpartition method', 'rpartition', 'ti')
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1127
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1128 # from raymond's original specification
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1129 S = 'http://www.python.org'
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1130 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
39734
cfb77fff0240 Fix endcase for str.rpartition()
Raymond Hettinger <python@rcn.com>
parents: 39302
diff changeset
1131 self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')
38391
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1132 self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1133 self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1134
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1135 self.checkraises(ValueError, S, 'rpartition', '')
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1136 self.checkraises(TypeError, S, 'rpartition', None)
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1137
48935
b5365f34640f Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46107
diff changeset
1138 # mixed use of str and unicode
b5365f34640f Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46107
diff changeset
1139 self.assertEqual('a/b/c'.rpartition(u'/'), ('a/b', '/', 'c'))
33298
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1140
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1141 class MixinStrStringUserStringTest:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1142 # Additional tests for 8bit strings, i.e. str, UserString and
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1143 # the string module
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1144
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1145 def test_maketrans(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1146 self.assertEqual(
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1147 ''.join(map(chr, xrange(256))).replace('abc', 'xyz'),
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1148 string.maketrans('abc', 'xyz')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1149 )
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1150 self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzw')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1151
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1152 def test_translate(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1153 table = string.maketrans('abc', 'xyz')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1154 self.checkequal('xyzxyz', 'xyzabcdef', 'translate', table, 'def')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1155
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1156 table = string.maketrans('a', 'A')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1157 self.checkequal('Abc', 'abc', 'translate', table)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1158 self.checkequal('xyz', 'xyz', 'translate', table)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1159 self.checkequal('yz', 'xyz', 'translate', table, 'x')
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41440
diff changeset
1160 self.checkequal('yx', 'zyzzx', 'translate', None, 'z')
41440
cedc8d7d1c7f SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents: 39734
diff changeset
1161 self.checkequal('zyzzx', 'zyzzx', 'translate', None, '')
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41440
diff changeset
1162 self.checkequal('zyzzx', 'zyzzx', 'translate', None)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1163 self.checkraises(ValueError, 'xyz', 'translate', 'too short', 'strip')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1164 self.checkraises(ValueError, 'xyz', 'translate', 'too short')
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1165
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1166
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1167 class MixinStrUserStringTest:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1168 # Additional tests that only work with
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1169 # 8bit compatible object, i.e. str and UserString
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1170
35287
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1171 if test_support.have_unicode:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1172 def test_encoding_decoding(self):
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1173 codecs = [('rot13', 'uryyb jbeyq'),
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1174 ('base64', 'aGVsbG8gd29ybGQ=\n'),
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1175 ('hex', '68656c6c6f20776f726c64'),
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1176 ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')]
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1177 for encoding, data in codecs:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1178 self.checkequal(data, 'hello world', 'encode', encoding)
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1179 self.checkequal('hello world', data, 'decode', encoding)
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1180 # zlib is optional, so we make the test optional too...
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1181 try:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1182 import zlib
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1183 except ImportError:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1184 pass
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1185 else:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1186 data = 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]'
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1187 self.checkequal(data, 'hello world', 'encode', 'zlib')
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1188 self.checkequal('hello world', data, 'decode', 'zlib')
28407
632a6cae0be6 Add two tests for simple error cases.
Walter Dörwald <walter@livinglogic.de>
parents: 28033
diff changeset
1189
35287
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1190 self.checkraises(TypeError, 'xyz', 'decode', 42)
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1191 self.checkraises(TypeError, 'xyz', 'encode', 42)
33298
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1192
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1193
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1194 class MixinStrUnicodeTest:
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1195 # Additional tests that only work with str and unicode.
33298
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1196
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1197 def test_bug1001011(self):
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1198 # Make sure join returns a NEW object for single item sequences
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1199 # involving a subclass.
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1200 # Make sure that it is of the appropriate type.
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1201 # Check the optimisation still occurs for standard objects.
33298
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1202 t = self.type2test
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1203 class subclass(t):
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1204 pass
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1205 s1 = subclass("abcd")
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1206 s2 = t().join([s1])
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1207 self.assert_(s1 is not s2)
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1208 self.assert_(type(s2) is t)
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1209
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1210 s1 = t("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1211 s2 = t().join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1212 self.assert_(s1 is s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1213
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1214 # Should also test mixed-type join.
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1215 if t is unicode:
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1216 s1 = subclass("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1217 s2 = "".join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1218 self.assert_(s1 is not s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1219 self.assert_(type(s2) is t)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1220
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1221 s1 = t("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1222 s2 = "".join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1223 self.assert_(s1 is s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1224
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1225 elif t is str:
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1226 s1 = subclass("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1227 s2 = u"".join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1228 self.assert_(s1 is not s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1229 self.assert_(type(s2) is unicode) # promotes!
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1230
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1231 s1 = t("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1232 s2 = u"".join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1233 self.assert_(s1 is not s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1234 self.assert_(type(s2) is unicode) # promotes!
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1235
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1236 else:
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1237 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)