summaryrefslogtreecommitdiff
path: root/tests/unit/utils/usertypes/test_neighborlist.py
blob: 587dffbd2d72489b7a797d81fc47a19ca386152c (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2014-2019 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser.  If not, see <http://www.gnu.org/licenses/>.

"""Tests for the NeighborList class."""

import pytest

from qutebrowser.utils import usertypes


class TestInit:

    """Just try to init some neighborlists."""

    def test_empty(self):
        """Test constructing an empty NeighborList."""
        nl = usertypes.NeighborList()
        assert nl.items == []

    def test_items(self):
        """Test constructing a NeighborList with items."""
        nl = usertypes.NeighborList([1, 2, 3])
        assert nl.items == [1, 2, 3]

    def test_len(self):
        """Test len() on NeighborList."""
        nl = usertypes.NeighborList([1, 2, 3])
        assert len(nl) == 3

    def test_contains(self):
        """Test 'in' on NeighborList."""
        nl = usertypes.NeighborList([1, 2, 3])
        assert 2 in nl
        assert 4 not in nl

    def test_invalid_mode(self):
        """Test with an invalid mode."""
        with pytest.raises(TypeError):
            usertypes.NeighborList(mode='blah')


class TestDefaultArg:

    """Test the default argument."""

    def test_simple(self):
        """Test default with a numeric argument."""
        nl = usertypes.NeighborList([1, 2, 3], default=2)
        assert nl._idx == 1

    def test_none(self):
        """Test default 'None'."""
        nl = usertypes.NeighborList([1, 2, None], default=None)
        assert nl._idx == 2

    def test_unset(self):
        """Test unset default value."""
        nl = usertypes.NeighborList([1, 2, 3])
        assert nl._idx is None

    def test_invalid_reset(self):
        """Test reset without default."""
        nl = usertypes.NeighborList([1, 2, 3, 4, 5])
        with pytest.raises(ValueError):
            nl.reset()


class TestEmpty:

    """Tests with no items."""

    @pytest.fixture
    def neighborlist(self):
        return usertypes.NeighborList()

    def test_curitem(self, neighborlist):
        """Test curitem with no item."""
        with pytest.raises(IndexError):
            neighborlist.curitem()

    def test_firstitem(self, neighborlist):
        """Test firstitem with no item."""
        with pytest.raises(IndexError):
            neighborlist.firstitem()

    def test_lastitem(self, neighborlist):
        """Test lastitem with no item."""
        with pytest.raises(IndexError):
            neighborlist.lastitem()

    def test_getitem(self, neighborlist):
        """Test getitem with no item."""
        with pytest.raises(IndexError):
            neighborlist.getitem(1)


class TestItems:

    """Tests with items."""

    @pytest.fixture
    def neighborlist(self):
        return usertypes.NeighborList([1, 2, 3, 4, 5], default=3)

    def test_curitem(self, neighborlist):
        """Test curitem()."""
        assert neighborlist._idx == 2
        assert neighborlist.curitem() == 3
        assert neighborlist._idx == 2

    def test_nextitem(self, neighborlist):
        """Test nextitem()."""
        assert neighborlist.nextitem() == 4
        assert neighborlist._idx == 3
        assert neighborlist.nextitem() == 5
        assert neighborlist._idx == 4

    def test_previtem(self, neighborlist):
        """Test previtem()."""
        assert neighborlist.previtem() == 2
        assert neighborlist._idx == 1
        assert neighborlist.previtem() == 1
        assert neighborlist._idx == 0

    def test_firstitem(self, neighborlist):
        """Test firstitem()."""
        assert neighborlist.firstitem() == 1
        assert neighborlist._idx == 0

    def test_lastitem(self, neighborlist):
        """Test lastitem()."""
        assert neighborlist.lastitem() == 5
        assert neighborlist._idx == 4

    def test_reset(self, neighborlist):
        """Test reset()."""
        neighborlist.nextitem()
        assert neighborlist._idx == 3
        neighborlist.reset()
        assert neighborlist._idx == 2

    def test_getitem(self, neighborlist):
        """Test getitem()."""
        assert neighborlist.getitem(2) == 5
        assert neighborlist._idx == 4
        neighborlist.reset()
        assert neighborlist.getitem(-2) == 1
        assert neighborlist._idx == 0


class TestSingleItem:

    """Tests with a list with only one item."""

    @pytest.fixture
    def neighborlist(self):
        return usertypes.NeighborList([1], default=1)

    def test_first_edge(self, neighborlist):
        """Test out of bounds previtem() with mode=edge."""
        neighborlist._mode = usertypes.NeighborList.Modes.edge
        neighborlist.firstitem()
        assert neighborlist._idx == 0
        assert neighborlist.previtem() == 1
        assert neighborlist._idx == 0

    def test_first_raise(self, neighborlist):
        """Test out of bounds previtem() with mode=raise."""
        neighborlist._mode = usertypes.NeighborList.Modes.exception
        neighborlist.firstitem()
        assert neighborlist._idx == 0
        with pytest.raises(IndexError):
            neighborlist.previtem()
        assert neighborlist._idx == 0

    def test_last_edge(self, neighborlist):
        """Test out of bounds nextitem() with mode=edge."""
        neighborlist._mode = usertypes.NeighborList.Modes.edge
        neighborlist.lastitem()
        assert neighborlist._idx == 0
        assert neighborlist.nextitem() == 1
        assert neighborlist._idx == 0

    def test_last_raise(self, neighborlist):
        """Test out of bounds nextitem() with mode=raise."""
        neighborlist._mode = usertypes.NeighborList.Modes.exception
        neighborlist.lastitem()
        assert neighborlist._idx == 0
        with pytest.raises(IndexError):
            neighborlist.nextitem()
        assert neighborlist._idx == 0


class TestEdgeMode:

    """Tests with mode=edge."""

    @pytest.fixture
    def neighborlist(self):
        return usertypes.NeighborList(
            [1, 2, 3, 4, 5], default=3,
            mode=usertypes.NeighborList.Modes.edge)

    def test_first(self, neighborlist):
        """Test out of bounds previtem()."""
        neighborlist.firstitem()
        assert neighborlist._idx == 0
        assert neighborlist.previtem() == 1
        assert neighborlist._idx == 0

    def test_last(self, neighborlist):
        """Test out of bounds nextitem()."""
        neighborlist.lastitem()
        assert neighborlist._idx == 4
        assert neighborlist.nextitem() == 5
        assert neighborlist._idx == 4


class TestExceptionMode:

    """Tests with mode=exception."""

    @pytest.fixture
    def neighborlist(self):
        return usertypes.NeighborList(
            [1, 2, 3, 4, 5], default=3,
            mode=usertypes.NeighborList.Modes.exception)

    def test_first(self, neighborlist):
        """Test out of bounds previtem()."""
        neighborlist.firstitem()
        assert neighborlist._idx == 0
        with pytest.raises(IndexError):
            neighborlist.previtem()
        assert neighborlist._idx == 0

    def test_last(self, neighborlist):
        """Test out of bounds nextitem()."""
        neighborlist.lastitem()
        assert neighborlist._idx == 4
        with pytest.raises(IndexError):
            neighborlist.nextitem()
        assert neighborlist._idx == 4


class TestSnapIn:

    """Tests for the fuzzyval/_snap_in features."""

    @pytest.fixture
    def neighborlist(self):
        return usertypes.NeighborList([20, 9, 1, 5])

    def test_bigger(self, neighborlist):
        """Test fuzzyval with snapping to a bigger value."""
        neighborlist.fuzzyval = 7
        assert neighborlist.nextitem() == 9
        assert neighborlist._idx == 1
        assert neighborlist.nextitem() == 1
        assert neighborlist._idx == 2

    def test_smaller(self, neighborlist):
        """Test fuzzyval with snapping to a smaller value."""
        neighborlist.fuzzyval = 7
        assert neighborlist.previtem() == 5
        assert neighborlist._idx == 3
        assert neighborlist.previtem() == 1
        assert neighborlist._idx == 2

    def test_equal_bigger(self, neighborlist):
        """Test fuzzyval with matching value, snapping to a bigger value."""
        neighborlist.fuzzyval = 20
        assert neighborlist.nextitem() == 9
        assert neighborlist._idx == 1

    def test_equal_smaller(self, neighborlist):
        """Test fuzzyval with matching value, snapping to a smaller value."""
        neighborlist.fuzzyval = 5
        assert neighborlist.previtem() == 1
        assert neighborlist._idx == 2

    def test_too_big_next(self, neighborlist):
        """Test fuzzyval/next with a value bigger than any in the list."""
        neighborlist.fuzzyval = 30
        assert neighborlist.nextitem() == 20
        assert neighborlist._idx == 0

    def test_too_big_prev(self, neighborlist):
        """Test fuzzyval/prev with a value bigger than any in the list."""
        neighborlist.fuzzyval = 30
        assert neighborlist.previtem() == 20
        assert neighborlist._idx == 0

    def test_too_small_next(self, neighborlist):
        """Test fuzzyval/next with a value smaller than any in the list."""
        neighborlist.fuzzyval = 0
        assert neighborlist.nextitem() == 1
        assert neighborlist._idx == 2

    def test_too_small_prev(self, neighborlist):
        """Test fuzzyval/prev with a value smaller than any in the list."""
        neighborlist.fuzzyval = 0
        assert neighborlist.previtem() == 1
        assert neighborlist._idx == 2