~divmod-dev/divmod.org/trunk

« back to all changes in this revision

Viewing changes to Epsilon/epsilon/test/test_caseless.py

  • Committer: Jean-Paul Calderone
  • Date: 2014-06-29 20:33:04 UTC
  • mfrom: (2749.1.1 remove-epsilon-1325289)
  • Revision ID: exarkun@twistedmatrix.com-20140629203304-gdkmbwl1suei4m97
mergeĀ lp:~exarkun/divmod.org/remove-epsilon-1325289

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Tests for L{epsilon.caseless}.
3
 
"""
4
 
 
5
 
import sys
6
 
from twisted.trial.unittest import TestCase
7
 
from epsilon.caseless import Caseless
8
 
 
9
 
class CaselessTestCase(TestCase):
10
 
    """
11
 
    Tests for L{Caseless}.
12
 
    """
13
 
 
14
 
    def _casings(s):
15
 
        """
16
 
        Generate variously cased versions of the given string.
17
 
        """
18
 
        yield s.lower()
19
 
        yield s.upper()
20
 
        yield s.title()
21
 
        yield s.title().swapcase()
22
 
    _casings = staticmethod(_casings)
23
 
 
24
 
    def _strings(self):
25
 
        """
26
 
        Generate a variety of C{str} and C{unicode} test samples.
27
 
        """
28
 
        for t in [str, unicode]:
29
 
            yield t()
30
 
            for s in self._casings('foo'):
31
 
                yield t(s)
32
 
 
33
 
 
34
 
    def test_cased(self):
35
 
        """
36
 
        L{Caseless} should expose the wrapped string as C{cased}.
37
 
        """
38
 
        for s in self._strings():
39
 
            self.assertIdentical(Caseless(s).cased, s)
40
 
 
41
 
 
42
 
    def test_idempotence(self):
43
 
        """
44
 
        L{Caseless} should be idempotent.
45
 
        """
46
 
        for s in self._strings():
47
 
            self.assertIdentical(Caseless(Caseless(s)).cased, s)
48
 
 
49
 
 
50
 
    def test_repr(self):
51
 
        """
52
 
        L{Caseless} should implement L{repr}.
53
 
        """
54
 
        for s in self._strings():
55
 
            self.assertEquals(repr(Caseless(s)), 'Caseless(%r)' % s)
56
 
 
57
 
 
58
 
    def test_str(self):
59
 
        """
60
 
        L{Caseless} should delegate L{str}.
61
 
        """
62
 
        for s in self._strings():
63
 
            self.assertEquals(str(Caseless(s)), str(s))
64
 
 
65
 
 
66
 
    def test_unicode(self):
67
 
        """
68
 
        L{Caseless} should delegate L{unicode}.
69
 
        """
70
 
        for s in self._strings():
71
 
            self.assertEquals(unicode(Caseless(s)), unicode(s))
72
 
 
73
 
 
74
 
    def test_len(self):
75
 
        """
76
 
        L{Caseless} should delegate L{len}.
77
 
        """
78
 
        for s in self._strings():
79
 
            self.assertEquals(len(Caseless(s)), len(s))
80
 
 
81
 
 
82
 
    def test_getitem(self):
83
 
        """
84
 
        L{Caseless} should delegate indexing/slicing.
85
 
        """
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])
92
 
 
93
 
 
94
 
    def test_iter(self):
95
 
        """
96
 
        L{Caseless} should delegate L{iter}.
97
 
        """
98
 
        for s in self._strings():
99
 
            self.assertEquals(list(iter(Caseless(s))), list(iter(s)))
100
 
 
101
 
 
102
 
    def test_lower(self):
103
 
        """
104
 
        L{Caseless} should delegate C{lower}.
105
 
        """
106
 
        for s in self._strings():
107
 
            self.assertEquals(Caseless(s).lower(), s.lower())
108
 
 
109
 
 
110
 
    def test_upper(self):
111
 
        """
112
 
        L{Caseless} should delegate C{upper}.
113
 
        """
114
 
        for s in self._strings():
115
 
            self.assertEquals(Caseless(s).upper(), s.upper())
116
 
 
117
 
 
118
 
    def test_title(self):
119
 
        """
120
 
        L{Caseless} should delegate C{title}.
121
 
        """
122
 
        for s in self._strings():
123
 
            self.assertEquals(Caseless(s).title(), s.title())
124
 
 
125
 
 
126
 
    def test_swapcase(self):
127
 
        """
128
 
        L{Caseless} should delegate C{swapcase}.
129
 
        """
130
 
        for s in self._strings():
131
 
            self.assertEquals(Caseless(s).swapcase(), s.swapcase())
132
 
 
133
 
 
134
 
    def test_comparison(self):
135
 
        """
136
 
        L{Caseless} should implement comparison and hashing case-insensitively.
137
 
        """
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)
148
 
 
149
 
 
150
 
    def test_contains(self):
151
 
        """
152
 
        L{Caseless} should search for substrings case-insensitively.
153
 
        """
154
 
        for a in map(Caseless, self._casings(u'abc')):
155
 
            for b in map(Caseless, self._casings(u'{{{abc}}}')):
156
 
                self.assertIn(a, b)
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)
160
 
 
161
 
 
162
 
    def test_startswith(self):
163
 
        """
164
 
        L{Caseless} should implement C{startswith} case-insensitively.
165
 
        """
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))
178
 
 
179
 
 
180
 
    def test_endswith(self):
181
 
        """
182
 
        L{Caseless} should implement C{endswith} case-insensitively.
183
 
        """
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))
196
 
 
197
 
 
198
 
    def test_startswithTuple(self):
199
 
        """
200
 
        L{test_startswith} with tuple arguments.
201
 
        """
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))
214
 
 
215
 
 
216
 
    def test_endswithTuple(self):
217
 
        """
218
 
        L{test_endswith} with tuple arguments.
219
 
        """
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))
232
 
 
233
 
    if sys.version_info < (2, 5):
234
 
        test_startswithTuple.skip = test_endswithTuple.skip = (
235
 
            'Tuple arguments implemented in Python 2.5')
236
 
 
237
 
 
238
 
    def test_count(self):
239
 
        """
240
 
        L{Caseless} should implement C{count} case-insensitively.
241
 
        """
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)
249
 
 
250
 
 
251
 
    def test_findindex(self):
252
 
        """
253
 
        L{Caseless} should implement C{find}/C{index} case-insensitively.
254
 
        """
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')
262
 
 
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))
269
 
 
270
 
 
271
 
    def test_rfindindex(self):
272
 
        """
273
 
        L{Caseless} should implement C{rfind}/C{rindex} case-insensitively.
274
 
        """
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')
282
 
 
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,))
289
 
 
290
 
 
291
 
__doctests__ = ['epsilon.caseless']