From 29ce0a51578df1fa6d0aa08c7128d0bec1d51255 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 18 Dec 2014 23:04:43 +0100 Subject: [PATCH] Fix handling of small/big fuzzyval's in NeighborList. This fixes an exception when having a really big or small zoom (e.g. 0) and then using +/-. Fixes #361. --- .../test/utils/usertypes/test_neighborlist.py | 24 +++++++++++++++++++ qutebrowser/utils/usertypes.py | 10 ++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/qutebrowser/test/utils/usertypes/test_neighborlist.py b/qutebrowser/test/utils/usertypes/test_neighborlist.py index d74b765bf..51a9d5889 100644 --- a/qutebrowser/test/utils/usertypes/test_neighborlist.py +++ b/qutebrowser/test/utils/usertypes/test_neighborlist.py @@ -355,6 +355,30 @@ class SnapInTests(unittest.TestCase): self.assertEqual(self.nl.previtem(), 1) self.assertEqual(self.nl._idx, 2) + def test_too_big_next(self): + """Test fuzzyval/next with a value bigger than any in the list.""" + self.nl.fuzzyval = 30 + self.assertEqual(self.nl.nextitem(), 20) + self.assertEqual(self.nl._idx, 0) + + def test_too_big_prev(self): + """Test fuzzyval/prev with a value bigger than any in the list.""" + self.nl.fuzzyval = 30 + self.assertEqual(self.nl.previtem(), 20) + self.assertEqual(self.nl._idx, 0) + + def test_too_small_next(self): + """Test fuzzyval/next with a value smaller than any in the list.""" + self.nl.fuzzyval = 0 + self.assertEqual(self.nl.nextitem(), 1) + self.assertEqual(self.nl._idx, 2) + + def test_too_small_prev(self): + """Test fuzzyval/prev with a value smaller than any in the list.""" + self.nl.fuzzyval = 0 + self.assertEqual(self.nl.previtem(), 1) + self.assertEqual(self.nl._idx, 2) + if __name__ == '__main__': unittest.main() diff --git a/qutebrowser/utils/usertypes.py b/qutebrowser/utils/usertypes.py index c21b7febf..a802e7244 100644 --- a/qutebrowser/utils/usertypes.py +++ b/qutebrowser/utils/usertypes.py @@ -116,8 +116,14 @@ class NeighborList(collections.abc.Sequence): op = operator.le if offset < 0 else operator.ge items = [(idx, e) for (idx, e) in enumerate(self._items) if op(e, self.fuzzyval)] - close_item = min(items, key=lambda tpl: abs(self.fuzzyval - tpl[1])) - self._idx = close_item[0] + if items: + item = min(items, key=lambda tpl: abs(self.fuzzyval - tpl[1])) + else: + sorted_items = sorted([(idx, e) for (idx, e) in + enumerate(self.items)]) + idx = 0 if offset < 0 else -1 + item = sorted_items[idx] + self._idx = item[0] return self.fuzzyval not in self._items def _get_new_item(self, offset):