annotate Lib/test/string_tests.py @ 38664:7619216bbc19 2.4

Bug #1502728: Correctly link against librt library on HP-UX. (backport from rev. 46743)
author Georg Brandl <georg@python.org>
date Thu, 08 Jun 2006 12:54:16 +0000
parents 10512ceba30c
children e15fafa4107d 556f6f1056a8
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
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
5 import unittest, string, sys
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
6 from test import test_support
13527
07c31182f237 add more tests of string.join variants to run_method_tests
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13517
diff changeset
7 from UserList import UserList
07c31182f237 add more tests of string.join variants to run_method_tests
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13517
diff changeset
8
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
9 class Sequence:
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
10 def __init__(self, seq='wxyz'): self.seq = seq
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
11 def __len__(self): return len(self.seq)
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
12 def __getitem__(self, i): return self.seq[i]
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
13
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
14 class BadSeq1(Sequence):
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
15 def __init__(self): self.seq = [7, 'hello', 123L]
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
16
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
17 class BadSeq2(Sequence):
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
18 def __init__(self): self.seq = ['a', 'b', 'c']
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
19 def __len__(self): return 8
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
20
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
21 class CommonTest(unittest.TestCase):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
22 # This testcase contains test that can be used in all
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
23 # stringlike classes. Currently this is str, unicode
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
24 # UserString and the string module.
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
25
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
26 # The type to be tested
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
27 # Change in subclasses to change the behaviour of fixtesttype()
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
28 type2test = None
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
29
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
30 # All tests pass their arguments to the testing methods
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
31 # as str objects. fixtesttype() can be used to propagate
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
32 # these arguments to the appropriate type
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
33 def fixtype(self, obj):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
34 if isinstance(obj, str):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
35 return self.__class__.type2test(obj)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
36 elif isinstance(obj, list):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
37 return [self.fixtype(x) for x in obj]
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
38 elif isinstance(obj, tuple):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
39 return tuple([self.fixtype(x) for x in obj])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
40 elif isinstance(obj, dict):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
41 return dict([
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
42 (self.fixtype(key), self.fixtype(value))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
43 for (key, value) in obj.iteritems()
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
44 ])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
45 else:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
46 return obj
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
47
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
48 # check that object.method(*args) returns result
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
49 def checkequal(self, result, object, methodname, *args):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
50 result = self.fixtype(result)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
51 object = self.fixtype(object)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
52 args = self.fixtype(args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
53 realresult = getattr(object, methodname)(*args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
54 self.assertEqual(
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
55 result,
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
56 realresult
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
57 )
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
58 # if the original is returned make sure that
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
59 # this doesn't happen with subclasses
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
60 if object == realresult:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
61 class subtype(self.__class__.type2test):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
62 pass
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
63 object = subtype(object)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
64 realresult = getattr(object, methodname)(*args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
65 self.assert_(object is not realresult)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
66
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
67 # check that object.method(*args) raises exc
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
68 def checkraises(self, exc, object, methodname, *args):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
69 object = self.fixtype(object)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
70 args = self.fixtype(args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
71 self.assertRaises(
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
72 exc,
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
73 getattr(object, methodname),
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
74 *args
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
75 )
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
76
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
77 # call object.method(*args) without any checks
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
78 def checkcall(self, object, methodname, *args):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
79 object = self.fixtype(object)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
80 args = self.fixtype(args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
81 getattr(object, methodname)(*args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
82
33833
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
83 def test_hash(self):
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
84 # SF bug 1054139: += optimization was not invalidating cached hash value
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
85 a = self.type2test('DNSSEC')
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
86 b = self.type2test('')
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
87 for c in a:
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
88 b += c
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
89 hash(b)
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
90 self.assertEqual(hash(a), hash(b))
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
91
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
92 def test_capitalize(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
93 self.checkequal(' hello ', ' hello ', 'capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
94 self.checkequal('Hello ', 'Hello ','capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
95 self.checkequal('Hello ', 'hello ','capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
96 self.checkequal('Aaaa', 'aaaa', 'capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
97 self.checkequal('Aaaa', 'AaAa', 'capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
98
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
99 self.checkraises(TypeError, 'hello', 'capitalize', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
100
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
101 def test_count(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
102 self.checkequal(3, 'aaa', 'count', 'a')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
103 self.checkequal(0, 'aaa', 'count', 'b')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
104 self.checkequal(3, 'aaa', 'count', 'a')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
105 self.checkequal(0, 'aaa', 'count', 'b')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
106 self.checkequal(3, 'aaa', 'count', 'a')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
107 self.checkequal(0, 'aaa', 'count', 'b')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
108 self.checkequal(0, 'aaa', 'count', 'b')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
109 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
110 self.checkequal(3, 'aaa', 'count', 'a', -10)
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(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
112 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
113
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
114 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
115 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
116
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
117 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
118 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
119 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
120 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
121
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
122 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
123 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
124
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
125 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
126 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
127 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
128 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
129 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
130
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', 'rfind')
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', 'rfind', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
133
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
134 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
135 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
136 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
137 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
138 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
139
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
140 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
141 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
142 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
143 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
144
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
145 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
146 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
147
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
148 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
149 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
150 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
151 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
152 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
153
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
154 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
155 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
156 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
157 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
158 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
159
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
160 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
161 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
162
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
163 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
164 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
165 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
166 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
167
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
168 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
169 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
170 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
171 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
172
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
173 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
174 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
175 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
176 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
177 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
178 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
179 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
180 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
181
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
182 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
183
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
184 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
185 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
186 '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
187
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
188 # by whitespace
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
189 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
190 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
191 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
192 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
193 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
194 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
195 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
196
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
197 # by a char
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
198 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
199 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
200 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
201 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
202 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
203 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
204 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
205 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
206 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
207
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
208 # 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
209 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
210 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
211 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
212 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
213 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
214 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
215 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
216 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
217
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
218 # 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
219 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
220
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
221 # 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
222 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
223
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
224 def test_rsplit(self):
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
225 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
226 '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
227
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
228 # by whitespace
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
229 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
230 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
231 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
232 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
233 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
234 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
235 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
236
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
237 # by a char
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
238 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
239 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
240 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
241 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
242 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
243 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
244 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
245 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
246 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
247
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
248 # by string
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
249 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
250 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
251 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
252 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
253 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
254 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
255 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
256 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
257
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
258 # 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
259 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
260
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
261 # argument type
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
262 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
263
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
264 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
265 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
266 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
267 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
268 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
269
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
270 # 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
271 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
272 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
273 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
274 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
275
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
276 # 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
277 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
278 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
279 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
280 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
281
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
282 # 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
283 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
284 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
285 '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
286 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
287 '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
288 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
289 'rstrip', unicode('xyz', 'ascii'))
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
290 self.checkequal(unicode('hello', 'ascii'), 'hello',
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
291 '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
292
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
293 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
294 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
295 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
296
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
297 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
298 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
299 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
300 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
301 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
302 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
303 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
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_rjust(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(' 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
307 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
308 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
309 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
310 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
311 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
312
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
313 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
314 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
315 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
316 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
317 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
318 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
319 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
320
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
321 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
322 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
323
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
324 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
325
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
326 def test_replace(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
327 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
328 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
329 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
330 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
331 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
332 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
333 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
334 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
335 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
336 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
337 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
338 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
339 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
340 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
341 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
342 # 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
343 # 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
344 # 0 bytes).
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('', '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
346 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
347 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
348
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
349 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
350 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
351 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
352 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
353
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
354 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
355 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
356 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
357 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
358 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
359 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
360 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
361 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
362 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
363 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
364 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
365 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
366 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
367
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
368 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
369
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
370 class MixinStrUnicodeUserStringTest:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
371 # 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
372 # 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
373 # (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
374
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
375 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
376 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
377 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
378 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
379 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
380 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
381 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
382 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
383 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
384
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
385 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
386 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
387 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
388 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
389 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
390 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
391 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
392 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
393 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
394
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
395 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
396 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
397 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
398 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
399 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
400 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
401 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
402 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
403 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
404 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
405 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
406 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
407 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
408
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
409 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
410 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
411 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
412 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
413 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
414 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
415 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
416 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
417 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
418 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
419
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
420 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
421 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
422 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
423 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
424 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
425 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
426 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
427 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
428 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
429
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
430 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
431 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
432 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
433 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
434 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
435 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
436 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
437 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
438 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
439 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
440
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
441 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
442 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
443 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
444 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
445 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
446 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
447
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
448 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
449
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
450 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
451 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
452 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
453 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
454 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
455 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
456 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
457 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
458
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
459 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
460 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
461 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
462 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
463 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
464 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
465 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
466 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
467
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
468 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
469
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
470 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
471 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
472 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
473 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
474 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
475 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
476 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
477 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
478 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
479 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
480 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
481 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
482 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
483 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
484
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
485 # 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
486 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
487 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
488 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
489 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
490 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
491 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
492 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
493 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
494 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
495 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
496
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
497 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
498 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
499
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
500 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
501 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
502 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
503 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
504 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
505 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
506 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
507 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
508 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
509 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
510 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
511 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
512 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
513 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
514 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
515
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
516 # 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
517 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
518 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
519 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
520 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
521 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
522 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
523 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
524 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
525 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
526 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
527 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
528 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
529 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
530
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
531 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
532 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
533
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
534 def test___contains__(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
535 self.checkequal(True, '', '__contains__', '') # vereq('' in '', True)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
536 self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
537 self.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
538 self.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
539 self.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
540 self.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
541 self.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
542 self.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
543 self.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
544
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
545 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
546 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
547 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
548 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
549 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
550 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
551 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
552 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
553 # FIXME What about negative indizes? This is handled differently by [] and __getitem__(slice)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
554
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
555 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
556
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
557 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
558 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
559 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
560 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
561 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
562 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
563 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
564 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
565 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
566 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
567 # FIXME What about negative indizes? This is handled differently by [] and __getslice__
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, '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
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_mul(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
572 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
573 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
574 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
575 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
576 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
577 self.checkraises(TypeError, 'abc', '__mul__', '')
28033
1c2c97d34563 Get test to work on alpha
Neal Norwitz <nnorwitz@gmail.com>
parents: 28009
diff changeset
578 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
579
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
580 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
581 # 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
582 # 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
583 # 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
584 # 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
585 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
586 self.checkequal('abcd', '', '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
587 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
588 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
589 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
590 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
591 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
592 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
593 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
594 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
595 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
596 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
597 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
598 ['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
599 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
600 ('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
601
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
602 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
603 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
604
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
605 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
606 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
607 self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123L]))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
608
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
609 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
610 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
611 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
612 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
613 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
614 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
615 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
616 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
617 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
618
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
619 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
620 # 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
621 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
622
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
623 self.checkequal(' 42', '%3ld', '__mod__', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
624 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
625 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
626
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
627 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
628 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
629 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
630 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
631 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
632 self.checkraises(TypeError, '%(foo)s %(bar)s', '__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
633
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
634 # 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
635 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
636
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
637 # 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
638 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
639
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
640 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
641 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
642 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
643
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
644 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
645 # float formatting
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
646 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
647 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
648 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
649 for x in xrange(60):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
650 value = value * 3.141592655 / 3.0 * 10.0
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
651 # The formatfloat() code in stringobject.c and
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
652 # unicodeobject.c uses a 120 byte buffer and switches from
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
653 # 'f' formatting to 'g' at precision 50, so we expect
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
654 # OverflowErrors for the ranges x < 50 and prec >= 67.
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
655 if x < 50 and prec >= 67:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
656 self.checkraises(OverflowError, format, "__mod__", value)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
657 else:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
658 self.checkcall(format, "__mod__", value)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
659
33298
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
660
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
661 class MixinStrStringUserStringTest:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
662 # 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
663 # 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
664
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
665 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
666 self.assertEqual(
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
667 ''.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
668 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
669 )
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
670 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
671
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
672 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
673 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
674 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
675
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
676 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
677 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
678 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
679 self.checkequal('yz', 'xyz', 'translate', table, 'x')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
680 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
681 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
682
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
683
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
684 class MixinStrUserStringTest:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
685 # 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
686 # 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
687
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
688 def test_encoding_decoding(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
689 codecs = [('rot13', 'uryyb jbeyq'),
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
690 ('base64', 'aGVsbG8gd29ybGQ=\n'),
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
691 ('hex', '68656c6c6f20776f726c64'),
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
692 ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')]
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
693 for encoding, data in codecs:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
694 self.checkequal(data, 'hello world', 'encode', encoding)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
695 self.checkequal('hello world', data, 'decode', encoding)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
696 # zlib is optional, so we make the test optional too...
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
697 try:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
698 import zlib
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
699 except ImportError:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
700 pass
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
701 else:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
702 data = 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]'
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
703 self.checkequal(data, 'hello world', 'encode', 'zlib')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
704 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
705
632a6cae0be6 Add two tests for simple error cases.
Walter Dörwald <walter@livinglogic.de>
parents: 28033
diff changeset
706 self.checkraises(TypeError, 'xyz', 'decode', 42)
632a6cae0be6 Add two tests for simple error cases.
Walter Dörwald <walter@livinglogic.de>
parents: 28033
diff changeset
707 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
708
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
709
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
710 class MixinStrUnicodeTest:
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
711 # 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
712
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
713 def test_bug1001011(self):
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
714 # 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
715 # involving a subclass.
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
716 # Make sure that it is of the appropriate type.
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
717 # 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
718 t = self.type2test
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
719 class subclass(t):
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
720 pass
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
721 s1 = subclass("abcd")
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
722 s2 = t().join([s1])
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
723 self.assert_(s1 is not s2)
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
724 self.assert_(type(s2) is t)
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
725
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
726 s1 = t("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
727 s2 = t().join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
728 self.assert_(s1 is s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
729
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
730 # Should also test mixed-type join.
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
731 if t is unicode:
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
732 s1 = subclass("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
733 s2 = "".join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
734 self.assert_(s1 is not s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
735 self.assert_(type(s2) is t)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
736
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
737 s1 = t("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
738 s2 = "".join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
739 self.assert_(s1 is s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
740
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
741 elif t is str:
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
742 s1 = subclass("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
743 s2 = u"".join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
744 self.assert_(s1 is not s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
745 self.assert_(type(s2) is unicode) # promotes!
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
746
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
747 s1 = t("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
748 s2 = u"".join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
749 self.assert_(s1 is not s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
750 self.assert_(type(s2) is unicode) # promotes!
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
751
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
752 else:
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
753 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)