2
Tests for L{epsilon.caseless}.
6
from twisted.trial.unittest import TestCase
7
from epsilon.caseless import Caseless
9
class CaselessTestCase(TestCase):
11
Tests for L{Caseless}.
16
Generate variously cased versions of the given string.
21
yield s.title().swapcase()
22
_casings = staticmethod(_casings)
26
Generate a variety of C{str} and C{unicode} test samples.
28
for t in [str, unicode]:
30
for s in self._casings('foo'):
36
L{Caseless} should expose the wrapped string as C{cased}.
38
for s in self._strings():
39
self.assertIdentical(Caseless(s).cased, s)
42
def test_idempotence(self):
44
L{Caseless} should be idempotent.
46
for s in self._strings():
47
self.assertIdentical(Caseless(Caseless(s)).cased, s)
52
L{Caseless} should implement L{repr}.
54
for s in self._strings():
55
self.assertEquals(repr(Caseless(s)), 'Caseless(%r)' % s)
60
L{Caseless} should delegate L{str}.
62
for s in self._strings():
63
self.assertEquals(str(Caseless(s)), str(s))
66
def test_unicode(self):
68
L{Caseless} should delegate L{unicode}.
70
for s in self._strings():
71
self.assertEquals(unicode(Caseless(s)), unicode(s))
76
L{Caseless} should delegate L{len}.
78
for s in self._strings():
79
self.assertEquals(len(Caseless(s)), len(s))
82
def test_getitem(self):
84
L{Caseless} should delegate indexing/slicing.
86
for s in self._strings():
87
for i in xrange(len(s)):
88
self.assertEquals(Caseless(s)[i], s[i])
89
self.assertEquals(Caseless(s)[:i], s[:i])
90
self.assertEquals(Caseless(s)[i:], s[i:])
91
self.assertEquals(Caseless(s)[::-1], s[::-1])
96
L{Caseless} should delegate L{iter}.
98
for s in self._strings():
99
self.assertEquals(list(iter(Caseless(s))), list(iter(s)))
102
def test_lower(self):
104
L{Caseless} should delegate C{lower}.
106
for s in self._strings():
107
self.assertEquals(Caseless(s).lower(), s.lower())
110
def test_upper(self):
112
L{Caseless} should delegate C{upper}.
114
for s in self._strings():
115
self.assertEquals(Caseless(s).upper(), s.upper())
118
def test_title(self):
120
L{Caseless} should delegate C{title}.
122
for s in self._strings():
123
self.assertEquals(Caseless(s).title(), s.title())
126
def test_swapcase(self):
128
L{Caseless} should delegate C{swapcase}.
130
for s in self._strings():
131
self.assertEquals(Caseless(s).swapcase(), s.swapcase())
134
def test_comparison(self):
136
L{Caseless} should implement comparison and hashing case-insensitively.
138
for a in map(Caseless, self._casings(u'abc')):
139
for b in map(Caseless, self._casings(u'abc')):
140
self.assertEquals(a, b)
141
self.assertEquals(hash(a), hash(b))
142
self.assertEquals(cmp(a, b), 0)
143
for a in map(Caseless, self._casings(u'abc')):
144
for b in map(Caseless, self._casings(u'abd')):
145
self.assertNotEquals(a, b)
146
self.assertNotEquals(hash(a), hash(b))
147
self.assertEquals(cmp(a, b), -1)
150
def test_contains(self):
152
L{Caseless} should search for substrings case-insensitively.
154
for a in map(Caseless, self._casings(u'abc')):
155
for b in map(Caseless, self._casings(u'{{{abc}}}')):
157
for a in map(Caseless, self._casings(u'abc')):
158
for b in map(Caseless, self._casings(u'{{{abd}}}')):
159
self.assertNotIn(a, b)
162
def test_startswith(self):
164
L{Caseless} should implement C{startswith} case-insensitively.
166
for a in map(Caseless, self._casings(u'abcbabcba')):
167
for b in self._casings(u'abc'):
168
self.assertTrue(a.startswith(b))
169
self.assertTrue(a.startswith(b, 4))
170
self.assertFalse(a.startswith(b, 2))
171
self.assertFalse(a.startswith(b, 4, 6))
172
for a in map(Caseless, self._casings(u'abcbabcba')):
173
for b in self._casings(u'cba'):
174
self.assertFalse(a.startswith(b))
175
self.assertFalse(a.startswith(b, 4))
176
self.assertTrue(a.startswith(b, 2))
177
self.assertFalse(a.startswith(b, 4, 6))
180
def test_endswith(self):
182
L{Caseless} should implement C{endswith} case-insensitively.
184
for a in map(Caseless, self._casings(u'abcbabcba')):
185
for b in self._casings(u'cba'):
186
self.assertTrue(a.endswith(b))
187
self.assertTrue(a.endswith(b, 0, 5))
188
self.assertFalse(a.endswith(b, 0, 3))
189
self.assertFalse(a.endswith(b, 7))
190
for a in map(Caseless, self._casings(u'abcbabcba')):
191
for b in self._casings(u'abc'):
192
self.assertFalse(a.endswith(b))
193
self.assertFalse(a.endswith(b, 0, 5))
194
self.assertTrue(a.endswith(b, 0, 3))
195
self.assertFalse(a.endswith(b, 7))
198
def test_startswithTuple(self):
200
L{test_startswith} with tuple arguments.
202
for a in map(Caseless, self._casings(u'abcbabcba')):
203
for b in self._casings(u'abc'):
204
self.assertTrue(a.startswith((u'foo', b, u'bar')))
205
self.assertTrue(a.startswith((u'foo', b, u'bar'), 4))
206
self.assertFalse(a.startswith((u'foo', b, u'bar'), 2))
207
self.assertFalse(a.startswith((u'foo', b, u'bar'), 4, 6))
208
for a in map(Caseless, self._casings(u'abcbabcba')):
209
for b in self._casings(u'cba'):
210
self.assertFalse(a.startswith((u'foo', b, u'bar')))
211
self.assertFalse(a.startswith((u'foo', b, u'bar'), 4))
212
self.assertTrue(a.startswith((u'foo', b, u'bar'), 2))
213
self.assertFalse(a.startswith((u'foo', b, u'bar'), 4, 6))
216
def test_endswithTuple(self):
218
L{test_endswith} with tuple arguments.
220
for a in map(Caseless, self._casings(u'abcbabcba')):
221
for b in self._casings(u'cba'):
222
self.assertTrue(a.endswith((u'foo', b, u'bar')))
223
self.assertTrue(a.endswith((u'foo', b, u'bar'), 0, 5))
224
self.assertFalse(a.endswith((u'foo', b, u'bar'), 0, 3))
225
self.assertFalse(a.endswith((u'foo', b, u'bar'), 7))
226
for a in map(Caseless, self._casings(u'abcbabcba')):
227
for b in self._casings(u'abc'):
228
self.assertFalse(a.endswith((u'foo', b, u'bar')))
229
self.assertFalse(a.endswith((u'foo', b, u'bar'), 0, 5))
230
self.assertTrue(a.endswith((u'foo', b, u'bar'), 0, 3))
231
self.assertFalse(a.endswith((u'foo', b, u'bar'), 7))
233
if sys.version_info < (2, 5):
234
test_startswithTuple.skip = test_endswithTuple.skip = (
235
'Tuple arguments implemented in Python 2.5')
238
def test_count(self):
240
L{Caseless} should implement C{count} case-insensitively.
242
for a in map(Caseless, self._casings(u'abcbabcba')):
243
self.assertEquals(a.count(u'foo'), 0)
244
for b in self._casings(u'cba'):
245
self.assertEquals(a.count(b), 2)
246
self.assertEquals(a.count(b, 2), 2)
247
self.assertEquals(a.count(b, 3), 1)
248
self.assertEquals(a.count(b, 0, 4), 0)
251
def test_findindex(self):
253
L{Caseless} should implement C{find}/C{index} case-insensitively.
255
def assertFound(a, b, result, rest=()):
256
self.assertEquals(a.find(b, *rest), result)
257
self.assertEquals(a.index(b, *rest), result)
258
def assertNotFound(a, b, rest=()):
259
self.assertEquals(a.find(b, *rest), -1)
260
err = self.assertRaises(ValueError, lambda: a.index(b, *rest))
261
self.assertEquals(str(err), 'substring not found')
263
for a in map(Caseless, self._casings(u'abcbabcba')):
264
assertNotFound(a, u'foo')
265
for b in self._casings(u'abc'):
266
assertFound(a, b, result=0)
267
assertFound(a, b, rest=(1,), result=4)
268
assertNotFound(a, b, rest=(1, 6))
271
def test_rfindindex(self):
273
L{Caseless} should implement C{rfind}/C{rindex} case-insensitively.
275
def assertFound(a, b, result, rest=()):
276
self.assertEquals(a.rfind(b, *rest), result)
277
self.assertEquals(a.rindex(b, *rest), result)
278
def assertNotFound(a, b, rest=()):
279
self.assertEquals(a.rfind(b, *rest), -1)
280
err = self.assertRaises(ValueError, lambda: a.rindex(b, *rest))
281
self.assertEquals(str(err), 'substring not found')
283
for a in map(Caseless, self._casings(u'abcbabcba')):
284
assertNotFound(a, u'foo')
285
for b in self._casings(u'cba'):
286
assertFound(a, b, result=6)
287
assertFound(a, b, rest=(0, 8), result=2)
288
assertNotFound(a, b, rest=(7,))
291
__doctests__ = ['epsilon.caseless']