aboutsummaryrefslogtreecommitdiff
blob: edbaa5a002c9613f48312ddde6e0a54544139774 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
from collections import OrderedDict
from itertools import chain
from operator import itemgetter

import pytest
from snakeoil import sequences
from snakeoil.sequences import split_elements, split_negations


class UnhashableComplex(complex):

    def __hash__(self):
        raise TypeError


class TestStableUnique:

    def common_check(self, func):
        # silly
        assert func(()) == []
        # hashable
        assert sorted(func([1, 1, 2, 3, 2])) == [1, 2, 3]
        # neither

    def test_stable_unique(self, func=sequences.stable_unique):
        assert list(set([1, 2, 3])) == [1, 2, 3], \
            "this test is reliant on the interpreter hasing 1,2,3 into a specific ordering- " \
            "for whatever reason, ordering differs, thus this test can't verify it"
        assert func([3, 2, 1]) == [3, 2, 1]

    def test_iter_stable_unique(self):
        self.test_stable_unique(lambda x: list(sequences.iter_stable_unique(x)))
        o = UnhashableComplex()
        l = [1, 2, 3, o, UnhashableComplex(), 4, 3, UnhashableComplex()]
        assert list(sequences.iter_stable_unique(l)) == [1, 2, 3, o, 4]

    def _generator(self):
        for x in range(5, -1, -1):
            yield x

    def test_unstable_unique(self):
        self.common_check(sequences.unstable_unique)
        uc = UnhashableComplex
        res = sequences.unstable_unique([uc(1, 0), uc(0, 1), uc(1, 0)])
        # sortable
        assert sorted(sequences.unstable_unique(
            [[1, 2], [1, 3], [1, 2], [1, 3]])) == [[1, 2], [1, 3]]
        assert res == [uc(1, 0), uc(0, 1)] or res == [uc(0, 1), uc(1, 0)]
        assert sorted(sequences.unstable_unique(self._generator())) == sorted(range(6))


class TestChainedLists:

    @staticmethod
    def gen_cl():
        return sequences.ChainedLists(
            list(range(3)),
            list(range(3, 6)),
            list(range(6, 100))
        )

    def test_contains(self):
        cl = self.gen_cl()
        for x in (1, 2, 4, 99):
            assert x in cl

    def test_iter(self):
        assert list(self.gen_cl()) == list(range(100))

    def test_len(self):
        assert len(self.gen_cl()) == 100

    def test_str(self):
        l = sequences.ChainedLists(list(range(3)), list(range(3, 5)))
        assert str(l) == '[ [0, 1, 2], [3, 4] ]'

    def test_getitem(self):
        cl = self.gen_cl()
        for x in (1, 2, 4, 98, -1, -99, 0):
            # "Statement seems to have no effect"
            # pylint: disable=W0104
            cl[x]
        with pytest.raises(IndexError):
            cl.__getitem__(100)
        with pytest.raises(IndexError):
            cl.__getitem__(-101)

    def test_mutable(self):
        with pytest.raises(TypeError):
            self.gen_cl().__delitem__(1)
        with pytest.raises(TypeError):
            self.gen_cl().__setitem__(1, 2)

    def test_append(self):
        cl = self.gen_cl()
        cl.append(list(range(10)))
        assert len(cl) == 110

    def test_extend(self):
        cl = self.gen_cl()
        cl.extend(list(range(10)) for i in range(5))
        assert len(cl) == 150


class Test_iflatten_instance:
    func = staticmethod(sequences.iflatten_instance)

    def test_it(self):
        o = OrderedDict((k, None) for k in range(10))
        for l, correct, skip in (
                (["asdf", ["asdf", "asdf"], 1, None],
                 ["asdf", "asdf", "asdf", 1, None], str),
                ([o, 1, "fds"], [o, 1, "fds"], (str, OrderedDict)),
                ([o, 1, "fds"], list(range(10)) + [1, "fds"], str),
                ("fds", ["fds"], str),
                ("fds", ["f", "d", "s"], int),
                ('', [''], str),
                (1, [1], int),
                ):
            iterator = self.func(l, skip)
            assert list(iterator) == correct
            assert list(iterator) == []

        # There is a small difference between the cpython and native
        # version: the cpython one raises immediately, for native we
        # have to iterate.
        def fail():
            return list(self.func(None))
        with pytest.raises(TypeError):
            fail()

        # Yes, no sane code does this, but even insane code shouldn't
        # kill the cpython version.
        iters = []
        iterator = self.func(iters)
        iters.append(iterator)
        with pytest.raises(ValueError):
            next(iterator)

        # Regression test: this was triggered through demandload.
        # **{} is there to explicitly force a dict.
        assert self.func((), **{})


class Test_iflatten_func:
    func = staticmethod(sequences.iflatten_func)

    def test_it(self):
        o = OrderedDict((k, None) for k in range(10))
        for l, correct, skip in (
                (["asdf", ["asdf", "asdf"], 1, None],
                 ["asdf", "asdf", "asdf", 1, None], str),
                ([o, 1, "fds"], [o, 1, "fds"], (str, OrderedDict)),
                ([o, 1, "fds"], list(range(10)) + [1, "fds"], str),
                ("fds", ["fds"], str),
                (1, [1], int),
                ):
            iterator = self.func(l, lambda x: isinstance(x, skip))
            assert list(iterator) == correct
            assert list(iterator) == []

        # There is a small difference between the cpython and native
        # version: the cpython one raises immediately, for native we
        # have to iterate.
        def fail():
            return list(self.func(None, lambda x: False))
        with pytest.raises(TypeError):
            fail()

        # Yes, no sane code does this, but even insane code shouldn't
        # kill the cpython version.
        iters = []
        iterator = self.func(iters, lambda x: False)
        iters.append(iterator)
        with pytest.raises(ValueError):
            next(iterator)

        # Regression test: this was triggered through demandload.
        # **{} is there to explicitly force a dict to the underlay cpy
        assert self.func((), lambda x: True, **{})


class Test_predicate_split:
    kls = staticmethod(sequences.predicate_split)

    def test_simple(self):
        false_l, true_l = self.kls(lambda x: x % 2 == 0, range(100))
        assert false_l == list(range(1, 100, 2))
        assert true_l == list(range(0, 100, 2))

    def test_key(self):
        false_l, true_l = self.kls(lambda x: x % 2 == 0,
                                   ([0, x] for x in range(100)),
                                   key=itemgetter(1))
        assert false_l == [[0, x] for x in range(1, 100, 2)]
        assert true_l == [[0, x] for x in range(0, 100, 2)]


class TestSplitNegations:

    def test_empty(self):
        # empty input
        seq = ''
        assert split_negations(seq) == ((), ())

    def test_bad_value(self):
        # no-value negation should raise a ValueError
        bad_values = (
            '-',
            'a b c - d f e',
        )

        for s in bad_values:
            with pytest.raises(ValueError):
                split_negations(s.split())

    def test_negs(self):
        # all negs
        seq = ('-' + str(x) for x in range(100))
        assert split_negations(seq) == (tuple(map(str, range(100))), ())

    def test_pos(self):
        # all pos
        seq = (str(x) for x in range(100))
        assert split_negations(seq) == ((), tuple(map(str, range(100))))

    def test_neg_pos(self):
        # both
        seq = (('-' + str(x), str(x)) for x in range(100))
        seq = chain.from_iterable(seq)
        assert split_negations(seq) == (tuple(map(str, range(100))), tuple(map(str, range(100))))

    def test_converter(self):
        # converter method
        seq = (('-' + str(x), str(x)) for x in range(100))
        seq = chain.from_iterable(seq)
        assert split_negations(seq, int) == (tuple(range(100)), tuple(range(100)))


class TestSplitElements:

    def test_empty(self):
        # empty input
        seq = ''
        assert split_elements(seq) == ((), (), ())

    def test_bad_value(self):
        # no-value neg/pos should raise ValueErrors
        bad_values = (
            '-',
            '+',
            'a b c - d f e',
            'a b c + d f e',
        )

        for s in bad_values:
            with pytest.raises(ValueError):
                split_elements(s.split())

    def test_negs(self):
        # all negs
        seq = ('-' + str(x) for x in range(100))
        assert split_elements(seq) == (tuple(map(str, range(100))), (), ())

    def test_neutral(self):
        # all neutral
        seq = (str(x) for x in range(100))
        assert split_elements(seq) == ((), tuple(map(str, range(100))), ())

    def test_pos(self):
        # all pos
        seq = ('+' + str(x) for x in range(100))
        assert split_elements(seq) == ((), (), tuple(map(str, range(100))))

    def test_neg_pos(self):
        # both negative and positive values
        seq = (('-' + str(x), '+' + str(x)) for x in range(100))
        seq = chain.from_iterable(seq)
        assert split_elements(seq) == (
            tuple(map(str, range(100))),
            (),
            tuple(map(str, range(100))),
        )

    def test_neg_neu_pos(self):
        # all three value types
        seq = (('-' + str(x), str(x), '+' + str(x)) for x in range(100))
        seq = chain.from_iterable(seq)
        assert split_elements(seq) == (
            tuple(map(str, range(100))),
            tuple(map(str, range(100))),
            tuple(map(str, range(100))),
        )

    def test_converter(self):
        # converter method
        seq = (('-' + str(x), str(x), '+' + str(x)) for x in range(100))
        seq = chain.from_iterable(seq)
        assert split_elements(seq, int) == (
            tuple(range(100)), tuple(range(100)), tuple(range(100)))