annotate Lib/test/string_tests.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 2801bf875a24
children d544873d62e9
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
41633
5d0969167f6d Merging change 55102 from the trunk:
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 39734
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)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64708
diff changeset
65 self.assertTrue(object is not realresult)
28009
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
49309
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
123 self.checkequal(1, '', 'count', '')
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
124 self.checkequal(0, '', 'count', '', 1, 1)
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
125 self.checkequal(0, '', 'count', '', sys.maxint, 0)
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
126
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
127 self.checkequal(0, '', 'count', 'xx')
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
128 self.checkequal(0, '', 'count', 'xx', 1, 1)
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
129 self.checkequal(0, '', 'count', 'xx', sys.maxint, 0)
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
130
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
131 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
132 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
133
34621
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
134 # For a variety of combinations,
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
135 # verify that str.count() matches an equivalent function
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
136 # 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
137 charset = ['', 'a', 'b']
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
138 digits = 7
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
139 base = len(charset)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
140 teststrings = set()
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
141 for i in xrange(base ** digits):
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
142 entry = []
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
143 for j in xrange(digits):
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
144 i, m = divmod(i, base)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
145 entry.append(charset[m])
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
146 teststrings.add(''.join(entry))
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
147 teststrings = list(teststrings)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
148 for i in teststrings:
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
149 i = self.fixtype(i)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
150 n = len(i)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
151 for j in teststrings:
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
152 r1 = i.count(j)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
153 if j:
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
154 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
155 else:
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
156 r2, rem = len(i)+1, 0
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
157 if rem or r1 != r2:
39302
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
158 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
159 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
160
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
161 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
162 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
163 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
164 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
165
38514
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
166 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
167 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
168 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
169
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
170 # 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
171 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
172 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
173 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
174 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
175 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
176
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
177 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
178 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
179
49309
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
180 self.checkequal(0, '', 'find', '')
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
181 self.checkequal(-1, '', 'find', '', 1, 1)
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
182 self.checkequal(-1, '', 'find', '', sys.maxint, 0)
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
183
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
184 self.checkequal(-1, '', 'find', 'xx')
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
185 self.checkequal(-1, '', 'find', 'xx', 1, 1)
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
186 self.checkequal(-1, '', 'find', 'xx', sys.maxint, 0)
e0c06caefbff #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48937
diff changeset
187
58096
f5e496a8417f Add tests for issue #7458: str.rfind() would crash when called with an invalid
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
188 # issue 7458
f5e496a8417f Add tests for issue #7458: str.rfind() would crash when called with an invalid
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
189 self.checkequal(-1, 'ab', 'find', 'xxx', sys.maxsize + 1, 0)
f5e496a8417f Add tests for issue #7458: str.rfind() would crash when called with an invalid
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
190
34620
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
191 # For a variety of combinations,
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
192 # 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
193 # 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
194 charset = ['', 'a', 'b', 'c']
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
195 digits = 5
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
196 base = len(charset)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
197 teststrings = set()
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
198 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
199 entry = []
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
200 for j in xrange(digits):
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
201 i, m = divmod(i, base)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
202 entry.append(charset[m])
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
203 teststrings.add(''.join(entry))
34621
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
204 teststrings = list(teststrings)
34620
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
205 for i in teststrings:
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
206 i = self.fixtype(i)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
207 for j in teststrings:
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
208 loc = i.find(j)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
209 r1 = (loc != -1)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
210 r2 = j in i
58098
4acd8faea2d9 Remove silly conditional.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58096
diff changeset
211 self.assertEqual(r1, r2)
34620
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
212 if loc != -1:
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
213 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
214
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
215 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
216 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
217 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
218 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
219 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
220
38514
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
221 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
222 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
223 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
224
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
225 # 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
226 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
227 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
228 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
229 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
230 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
231
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
232 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
233 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
234
58090
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
235 # For a variety of combinations,
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
236 # verify that str.rfind() matches __contains__
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
237 # and that the found substring is really at that location
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
238 charset = ['', 'a', 'b', 'c']
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
239 digits = 5
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
240 base = len(charset)
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
241 teststrings = set()
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
242 for i in xrange(base ** digits):
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
243 entry = []
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
244 for j in xrange(digits):
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
245 i, m = divmod(i, base)
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
246 entry.append(charset[m])
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
247 teststrings.add(''.join(entry))
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
248 teststrings = list(teststrings)
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
249 for i in teststrings:
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
250 i = self.fixtype(i)
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
251 for j in teststrings:
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
252 loc = i.rfind(j)
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
253 r1 = (loc != -1)
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
254 r2 = j in i
58098
4acd8faea2d9 Remove silly conditional.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58096
diff changeset
255 self.assertEqual(r1, r2)
58090
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
256 if loc != -1:
64708
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64701
diff changeset
257 self.assertEqual(i[loc:loc+len(j)], self.fixtype(j))
58090
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
258
58096
f5e496a8417f Add tests for issue #7458: str.rfind() would crash when called with an invalid
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
259 # issue 7458
f5e496a8417f Add tests for issue #7458: str.rfind() would crash when called with an invalid
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
260 self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0)
f5e496a8417f Add tests for issue #7458: str.rfind() would crash when called with an invalid
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
261
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
262 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
263 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
264 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
265 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
266 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
267
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(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
269 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
270 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
271 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
272
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
273 # 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
274 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
275 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
276 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
277 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
278 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
279
28009
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', 'index')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
281 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
282
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
283 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
284 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
285 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
286 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
287 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
288
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
289 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
290 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
291 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
292 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
293 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
294
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
295 # 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
296 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
297 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
298 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
299 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
300 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
301
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
302 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
303 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
304
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
305 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
306 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
307 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
308 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
309
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
310 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
311 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
312 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
313 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
314
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
315 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
316 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
317 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
318 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
319 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
320 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
321 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
322 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
42027
71f3192915e1 Backport 55874:
Neal Norwitz <nnorwitz@gmail.com>
parents: 41633
diff changeset
323 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
324
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
325 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
42027
71f3192915e1 Backport 55874:
Neal Norwitz <nnorwitz@gmail.com>
parents: 41633
diff changeset
326 # This test is only valid when sizeof(int) == sizeof(void*) == 4.
71f3192915e1 Backport 55874:
Neal Norwitz <nnorwitz@gmail.com>
parents: 41633
diff changeset
327 if sys.maxint < (1 << 32) and struct.calcsize('P') == 4:
71f3192915e1 Backport 55874:
Neal Norwitz <nnorwitz@gmail.com>
parents: 41633
diff changeset
328 self.checkraises(OverflowError,
71f3192915e1 Backport 55874:
Neal Norwitz <nnorwitz@gmail.com>
parents: 41633
diff changeset
329 '\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
330
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
331 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
332 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
333 '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
334
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
335 # by whitespace
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')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
337 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
338 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
339 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
340 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
341 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
342 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
343 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
344 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
345 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
346
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
347 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
348 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
349 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
350 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
351 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
352 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
353 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
354 aaa = ' a '*20
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
355 self.checkequal(['a']*20, aaa, 'split')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
356 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
357 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
358
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
359 # by a char
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
360 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
361 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
362 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
363 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
364 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
365 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
366 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
367 sys.maxint-2)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
368 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
369 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
370 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
371 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
372 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
373 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
374
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
375 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
376 self.checkequal(['a']*15 +['a|a|a|a|a'],
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
377 ('a|'*20)[:-1], 'split', '|', 15)
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
378
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
379 # 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
380 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
381 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
382 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
383 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
384 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
385 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
386 sys.maxint-10)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
387 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
388 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
389 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
390 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
391 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
392 'split', 'test')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
393 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
394 self.checkequal(['', ''], 'aaa', 'split', 'aaa')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
395 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
396 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
397 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
398 self.checkequal([''], '', 'split', 'aaa')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
399 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
400 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
401 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
402
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
403 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
404 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
405 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
406 '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
407
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
408 # 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
409 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
410
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
411 # 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
412 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
413
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
414 # null case
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
415 self.checkraises(ValueError, 'hello', 'split', '')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
416 self.checkraises(ValueError, 'hello', 'split', '', 0)
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
417
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
418 def test_rsplit(self):
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
419 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
420 '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
421
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
422 # by whitespace
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
423 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
424 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
425 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
426 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
427 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
428 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
429 sys.maxint-20)
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
430 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
431 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
432 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
433
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
434 self.checkequal([], ' ', 'rsplit')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
435 self.checkequal(['a'], ' a ', 'rsplit')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
436 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
437 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
438 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
439 None, 1)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
440 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
441 None, 2)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
442 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
443 aaa = ' a '*20
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
444 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
445 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
446 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
447
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
448
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
449 # by a char
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
450 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
451 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
452 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
453 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
454 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
455 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
456 sys.maxint-100)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
457 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
458 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
459 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
460 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
461 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
462
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
463 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
464
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
465 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
466 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
467 ('a|'*20)[:-1], 'rsplit', '|', 15)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
468
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
469 # by string
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
470 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
471 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
472 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
473 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
474 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
475 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
476 sys.maxint-5)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
477 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
478 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
479 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
480 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
481 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
482 'rsplit', 'test')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
483 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
484 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
485 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
486 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
487 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
488 self.checkequal([''], '', 'rsplit', 'aaa')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
489 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
490 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
491 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
492
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
493 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
494 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
495 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
496 'rsplit', 'BLAH', 18)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
497
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
498 # 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
499 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
500
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
501 # argument type
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
502 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
503
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
504 # null case
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
505 self.checkraises(ValueError, 'hello', 'rsplit', '')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
506 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
507
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
508 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
509 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
510 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
511 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
512 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
513
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
514 # 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
515 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
516 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
517 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
518 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
519
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
520 # 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
521 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
522 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
523 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
524 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
525
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
526 # 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
527 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
528 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
529 '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
530 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
531 '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
532 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
533 '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
534 # 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
535 #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
536 # '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
537
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
538 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
539 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
540 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
541
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
542 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
543 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
544 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
545 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
546 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
547 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
548 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
549
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
550 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
551 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
552 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
553 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
554 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
555 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
556 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
557
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
558 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
559 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
560 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
561 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
562 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
563 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
564 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
565
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
566 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
567 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
568
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
569 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
570
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
571 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
572 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
573
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
574 # 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
575 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
576 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
577 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
578 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
579 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
580 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
581
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
582 # 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
583 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
584 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
585 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
586 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
587 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
588 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
589 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
590 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
591 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
592 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
593 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
594 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
595
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
596 # 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
597 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
598 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
599 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
600 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
601 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
602 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
603 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
604 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
605 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
606 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
607 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
608 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
609 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
610 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
611 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
612 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
613 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
614 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
615 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
616 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
617 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
618 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
619 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
620 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
621
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
622 # 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
623 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
624 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
625 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
626 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
627 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
628 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
629 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
630 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
631 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
632 "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
633 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
634 "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
635 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
636 "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
637 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
638 "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
639 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
640 "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
641 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
642 "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
643 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
644
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("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
646 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
647
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
648 # 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
649 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
650 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
651 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
652 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
653
38279
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
654 # 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
655 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
656 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
657 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
658 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
659 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
660 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
661 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
662 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
663
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("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
665 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
666 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
667 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
668 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
669
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("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
671
38279
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
672 # 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
673 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
674 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
675 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
676 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
677 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
678 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
679 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
680 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
681 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
682 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
683 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
684
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
685 # 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
686 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
687 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
688 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
689 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
690 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
691 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
692 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
693
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
694 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
695
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
696 # 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
697 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
698 "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
699 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
700 "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
701 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
702 "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
703 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
704 "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
705 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
706 "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
707 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
708 "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
709 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
710 "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
711 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
712 "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
713
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
714 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
715 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
716 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
717
59432
e23cba03cc4b Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58474
diff changeset
718 with test_support.check_py3k_warnings():
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
719 ba = buffer('a')
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
720 bb = buffer('b')
39302
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
721 EQ("bbc", "abc", "replace", ba, bb)
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
722 EQ("aac", "abc", "replace", bb, ba)
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
723
38280
32ba3a0c6040 We can't leave the checked-in tests broken.
Tim Peters <tim.peters@gmail.com>
parents: 38279
diff changeset
724 #
28009
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('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
726 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
727 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
728 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
729 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
730 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
731 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
732 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
733 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
734 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
735 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
736 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
737 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
738 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
739 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
740 # 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
741 # 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
742 # 0 bytes).
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('', '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
744 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
745 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
746
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, 'hello', 'replace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
748 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
749 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
750 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
751
38299
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
752 def test_replace_overflow(self):
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
753 # Check for overflow checking on 32 bit machines
41633
5d0969167f6d Merging change 55102 from the trunk:
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 39734
diff changeset
754 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
755 return
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
756 A2_16 = "A" * (2**16)
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
757 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
758 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
759 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
760
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
761 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
762 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
763 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
764 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
765 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
766 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
767 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
768 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
769 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
770 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
771 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
772 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
773 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
774
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
775 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
776
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
777 # 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
778 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
779
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
780 class MixinStrUnicodeUserStringTest:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
781 # 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
782 # 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
783 # (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
784
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
785 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
786 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
787 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
788 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
789 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
790 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
791 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
792 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
793 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
794
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
795 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
796 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
797 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
798 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
799 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
800 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
801 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
802 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
803 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
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_istitle(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, '', 'istitle')
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', 'istitle')
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, 'A', 'istitle')
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(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
810 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
811 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
812 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
813 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
814 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
815 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
816 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
817 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
818
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
819 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
820 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
821 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
822 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
823 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
824 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
825 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
826 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
827 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
828 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
829
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
830 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
831 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
832 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
833 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
834 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
835 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
836 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
837 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
838 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
839
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
840 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
841 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
842 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
843 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
844 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
845 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
846 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
847 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
848 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
849 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
850
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
851 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
852 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
853 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
854 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
855 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
856 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
857
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
858 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
859
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
860 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
861 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
862 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
863 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
864 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
865 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
866 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
867 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
868
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
869 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
870 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
871 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
872 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
873 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
874 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
875 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
876 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
877
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
878 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
879
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
880 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
881 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
882 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
883 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
884 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
885 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
886 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
887 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
888 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
889 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
890 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
891 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
892 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
893 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
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', '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
897 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
898 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
899 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
900 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
901 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
902 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
903 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
904 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
905 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
906
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
907 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
908 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
909
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
910 # 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
911 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
912 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
913 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
914 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
915 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
916 '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
917 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
918 '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
919 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
920 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
921 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
922
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.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
924
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
925 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
926 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
927 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
928 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
929 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
930 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
931 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
932 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
933 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
934 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
935 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
936 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
937 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
938 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
939 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
940
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
941 # 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
942 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
943 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
944 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
945 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
946 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
947 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
948 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
949 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
950 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
951 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
952 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
953 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
954 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
955
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
956 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
957 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
958
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
959 # 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
960 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
961 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
962 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
963 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
964 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
965 '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
966 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
967 '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
968 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
969 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
970 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
971
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
972 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
973
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
974 def test___contains__(self):
58475
e98ee945e262 Merged revisions 77729 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 49308
diff changeset
975 self.checkequal(True, '', '__contains__', '')
e98ee945e262 Merged revisions 77729 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 49308
diff changeset
976 self.checkequal(True, 'abc', '__contains__', '')
e98ee945e262 Merged revisions 77729 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 49308
diff changeset
977 self.checkequal(False, 'abc', '__contains__', '\0')
e98ee945e262 Merged revisions 77729 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 49308
diff changeset
978 self.checkequal(True, '\0abc', '__contains__', '\0')
e98ee945e262 Merged revisions 77729 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 49308
diff changeset
979 self.checkequal(True, 'abc\0', '__contains__', '\0')
e98ee945e262 Merged revisions 77729 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 49308
diff changeset
980 self.checkequal(True, '\0abc', '__contains__', 'a')
e98ee945e262 Merged revisions 77729 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 49308
diff changeset
981 self.checkequal(True, 'asdf', '__contains__', 'asdf')
e98ee945e262 Merged revisions 77729 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 49308
diff changeset
982 self.checkequal(False, 'asd', '__contains__', 'asdf')
e98ee945e262 Merged revisions 77729 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 49308
diff changeset
983 self.checkequal(False, '', '__contains__', 'asdf')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
984
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
985 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
986 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
987 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
988 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
989 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
990 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
991 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
992 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
993
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
994 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
995
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
996 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
997 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
998 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
999 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
1000 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
1001 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
1002 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
1003 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
1004 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
1005 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
1006
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1007 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
1008
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1009 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
1010 # 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
1011 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
1012 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
1013 for start in indices:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1014 for stop in indices:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1015 # Skip step 0 (invalid)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1016 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
1017 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
1018 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
1019 slice(start, stop, step))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1020
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1021 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
1022 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
1023 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
1024 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
1025 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
1026 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
1027 self.checkraises(TypeError, 'abc', '__mul__', '')
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35758
diff changeset
1028 # 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
1029 # 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
1030 #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
1031
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1032 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
1033 # 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
1034 # 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
1035 # 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
1036 # 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
1037 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
1038 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
1039 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
1040 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
1041 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
1042 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
1043 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
1044 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
1045 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
1046 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
1047 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
1048 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
1049 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
1050 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
1051 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
1052 ['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
1053 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
1054 ('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
1055
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, ' ', 'join', BadSeq1())
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1057 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
1058
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1059 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
1060 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
1061 self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123L]))
35758
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1062 try:
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1063 def f():
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1064 yield 4 + ""
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1065 self.fixtype(' ').join(f())
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1066 except TypeError, e:
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1067 if '+' not in str(e):
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1068 self.fail('join() ate exception message')
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1069 else:
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1070 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
1071
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1072 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
1073 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
1074 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
1075 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
1076 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
1077 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
1078 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
1079 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
1080 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
1081
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1082 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
1083 # 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
1084 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
1085
45500
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1086 longvalue = sys.maxint + 10L
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1087 slongvalue = str(longvalue)
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1088 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
1089 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
1090 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
1091 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
1092 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
1093 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
1094 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
1095 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
1096
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1097 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
1098 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
1099 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
1100 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
1101 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
1102 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
1103 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
1104 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
1105
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1106 # 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
1107 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
1108
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1109 # 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
1110 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
1111
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1112 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
1113 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
1114 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
1115
78792
2801bf875a24 use the stricter PyMapping_Check (closes #15801)
Benjamin Peterson <benjamin@python.org>
parents: 71869
diff changeset
1116 class X(object): pass
2801bf875a24 use the stricter PyMapping_Check (closes #15801)
Benjamin Peterson <benjamin@python.org>
parents: 71869
diff changeset
1117 self.checkraises(TypeError, 'abc', '__mod__', X())
2801bf875a24 use the stricter PyMapping_Check (closes #15801)
Benjamin Peterson <benjamin@python.org>
parents: 71869
diff changeset
1118
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1119 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
1120 # float formatting
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1121 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
1122 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
1123 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
1124 for x in xrange(60):
64701
28e6a41c5ba0 Merged revisions 84470-84471,84566-84567,84759 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59432
diff changeset
1125 value = value * 3.14159265359 / 3.0 * 10.0
57412
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 49308
diff changeset
1126 self.checkcall(format, "__mod__", value)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1127
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
1128 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
1129 # 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
1130 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
1131 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
1132 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
1133 self.checkequal(True, 'a', 'islower')
38327
e9eb526b7111 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38299
diff changeset
1134
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
1135 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
1136 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
1137
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
1138 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
1139 self.checkequal(True, 'a', 'islower')
38327
e9eb526b7111 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38299
diff changeset
1140
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
1141 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
1142 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
1143
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
1144 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
1145 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
1146
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1147 def test_partition(self):
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1148
38391
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1149 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
1150 'this is the partition method', 'partition', 'ti')
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1151
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1152 # from raymond's original specification
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1153 S = 'http://www.python.org'
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1154 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1155 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1156 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1157 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1158
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1159 self.checkraises(ValueError, S, 'partition', '')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1160 self.checkraises(TypeError, S, 'partition', None)
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1161
48937
16c03f7cbc22 Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 42027
diff changeset
1162 # mixed use of str and unicode
16c03f7cbc22 Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 42027
diff changeset
1163 self.assertEqual('a/b/c'.partition(u'/'), ('a', '/', 'b/c'))
16c03f7cbc22 Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 42027
diff changeset
1164
38391
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1165 def test_rpartition(self):
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1166
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1167 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
1168 '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
1169
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1170 # from raymond's original specification
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1171 S = 'http://www.python.org'
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1172 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
39734
cfb77fff0240 Fix endcase for str.rpartition()
Raymond Hettinger <python@rcn.com>
parents: 39302
diff changeset
1173 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
1174 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
1175 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
1176
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1177 self.checkraises(ValueError, S, 'rpartition', '')
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1178 self.checkraises(TypeError, S, 'rpartition', None)
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1179
48937
16c03f7cbc22 Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 42027
diff changeset
1180 # mixed use of str and unicode
16c03f7cbc22 Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 42027
diff changeset
1181 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
1182
69471
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1183 def test_none_arguments(self):
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1184 # issue 11828
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1185 s = 'hello'
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1186 self.checkequal(2, s, 'find', 'l', None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1187 self.checkequal(3, s, 'find', 'l', -2, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1188 self.checkequal(2, s, 'find', 'l', None, -2)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1189 self.checkequal(0, s, 'find', 'h', None, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1190
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1191 self.checkequal(3, s, 'rfind', 'l', None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1192 self.checkequal(3, s, 'rfind', 'l', -2, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1193 self.checkequal(2, s, 'rfind', 'l', None, -2)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1194 self.checkequal(0, s, 'rfind', 'h', None, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1195
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1196 self.checkequal(2, s, 'index', 'l', None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1197 self.checkequal(3, s, 'index', 'l', -2, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1198 self.checkequal(2, s, 'index', 'l', None, -2)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1199 self.checkequal(0, s, 'index', 'h', None, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1200
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1201 self.checkequal(3, s, 'rindex', 'l', None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1202 self.checkequal(3, s, 'rindex', 'l', -2, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1203 self.checkequal(2, s, 'rindex', 'l', None, -2)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1204 self.checkequal(0, s, 'rindex', 'h', None, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1205
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1206 self.checkequal(2, s, 'count', 'l', None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1207 self.checkequal(1, s, 'count', 'l', -2, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1208 self.checkequal(1, s, 'count', 'l', None, -2)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1209 self.checkequal(0, s, 'count', 'x', None, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1210
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1211 self.checkequal(True, s, 'endswith', 'o', None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1212 self.checkequal(True, s, 'endswith', 'lo', -2, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1213 self.checkequal(True, s, 'endswith', 'l', None, -2)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1214 self.checkequal(False, s, 'endswith', 'x', None, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1215
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1216 self.checkequal(True, s, 'startswith', 'h', None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1217 self.checkequal(True, s, 'startswith', 'l', -2, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1218 self.checkequal(True, s, 'startswith', 'h', None, -2)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1219 self.checkequal(False, s, 'startswith', 'x', None, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1220
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1221 def test_find_etc_raise_correct_error_messages(self):
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1222 # issue 11828
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1223 s = 'hello'
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1224 x = 'x'
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1225 self.assertRaisesRegexp(TypeError, r'\bfind\b', s.find,
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1226 x, None, None, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1227 self.assertRaisesRegexp(TypeError, r'\brfind\b', s.rfind,
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1228 x, None, None, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1229 self.assertRaisesRegexp(TypeError, r'\bindex\b', s.index,
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1230 x, None, None, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1231 self.assertRaisesRegexp(TypeError, r'\brindex\b', s.rindex,
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1232 x, None, None, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1233 self.assertRaisesRegexp(TypeError, r'^count\(', s.count,
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1234 x, None, None, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1235 self.assertRaisesRegexp(TypeError, r'^startswith\(', s.startswith,
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1236 x, None, None, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1237 self.assertRaisesRegexp(TypeError, r'^endswith\(', s.endswith,
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1238 x, None, None, None)
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
1239
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1240 class MixinStrStringUserStringTest:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1241 # 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
1242 # 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
1243
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1244 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
1245 self.assertEqual(
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1246 ''.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
1247 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
1248 )
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1249 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
1250
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1251 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
1252 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
1253 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
1254
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1255 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
1256 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
1257 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
1258 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
1259 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
1260 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
1261 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
1262 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
1263 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
1264
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1265
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1266 class MixinStrUserStringTest:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1267 # 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
1268 # 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
1269
35287
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1270 if test_support.have_unicode:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1271 def test_encoding_decoding(self):
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1272 codecs = [('rot13', 'uryyb jbeyq'),
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1273 ('base64', 'aGVsbG8gd29ybGQ=\n'),
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1274 ('hex', '68656c6c6f20776f726c64'),
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1275 ('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
1276 for encoding, data in codecs:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1277 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
1278 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
1279 # 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
1280 try:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1281 import zlib
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1282 except ImportError:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1283 pass
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1284 else:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1285 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
1286 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
1287 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
1288
35287
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1289 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
1290 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
1291
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1292
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1293 class MixinStrUnicodeTest:
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1294 # 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
1295
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1296 def test_bug1001011(self):
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1297 # 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
1298 # involving a subclass.
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1299 # Make sure that it is of the appropriate type.
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1300 # 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
1301 t = self.type2test
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1302 class subclass(t):
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1303 pass
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1304 s1 = subclass("abcd")
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1305 s2 = t().join([s1])
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64708
diff changeset
1306 self.assertTrue(s1 is not s2)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64708
diff changeset
1307 self.assertTrue(type(s2) is t)
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1308
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1309 s1 = t("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1310 s2 = t().join([s1])
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64708
diff changeset
1311 self.assertTrue(s1 is s2)
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1312
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1313 # Should also test mixed-type join.
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1314 if t is unicode:
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1315 s1 = subclass("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1316 s2 = "".join([s1])
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64708
diff changeset
1317 self.assertTrue(s1 is not s2)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64708
diff changeset
1318 self.assertTrue(type(s2) is t)
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1319
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1320 s1 = t("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1321 s2 = "".join([s1])
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64708
diff changeset
1322 self.assertTrue(s1 is s2)
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1323
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1324 elif t is str:
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1325 s1 = subclass("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1326 s2 = u"".join([s1])
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64708
diff changeset
1327 self.assertTrue(s1 is not s2)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64708
diff changeset
1328 self.assertTrue(type(s2) is unicode) # promotes!
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1329
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1330 s1 = t("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1331 s2 = u"".join([s1])
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64708
diff changeset
1332 self.assertTrue(s1 is not s2)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64708
diff changeset
1333 self.assertTrue(type(s2) is unicode) # promotes!
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1334
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1335 else:
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1336 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)