diff --git a/qutebrowser/test/utils/test_utils.py b/qutebrowser/test/utils/test_utils.py index fec327097..b3176059f 100644 --- a/qutebrowser/test/utils/test_utils.py +++ b/qutebrowser/test/utils/test_utils.py @@ -404,5 +404,57 @@ class ForceEncodingTests(unittest.TestCase): self.assertEqual(utils.force_encoding(text, 'ascii'), 'hell? w?rld') +class NewestSliceTests(unittest.TestCase): + + """Test newest_slice.""" + + def test_count_minus_two(self): + """Test with a count of -2.""" + with self.assertRaises(ValueError): + utils.newest_slice([], -2) + + def test_count_minus_one(self): + """Test with a count of -1 (all elements).""" + items = range(20) + sliced = utils.newest_slice(items, -1) + self.assertEqual(list(sliced), list(items)) + + def test_count_zero(self): + """Test with a count of 0 (no elements).""" + items = range(20) + sliced = utils.newest_slice(items, 0) + self.assertEqual(list(sliced), []) + + def test_count_much_smaller(self): + """Test with a count which is much smaller than the iterable.""" + items = range(20) + sliced = utils.newest_slice(items, 5) + self.assertEqual(list(sliced), [15, 16, 17, 18, 19]) + + def test_count_smaller(self): + """Test with a count which is exactly one smaller.""" + items = range(5) + sliced = utils.newest_slice(items, 4) + self.assertEqual(list(sliced), [1, 2, 3, 4]) + + def test_count_equal(self): + """Test with a count which is just as large as the iterable.""" + items = range(5) + sliced = utils.newest_slice(items, 5) + self.assertEqual(list(sliced), list(items)) + + def test_count_bigger(self): + """Test with a count which is one bigger than the iterable.""" + items = range(5) + sliced = utils.newest_slice(items, 6) + self.assertEqual(list(sliced), list(items)) + + def test_count_much_bigger(self): + """Test with a count which is much bigger than the iterable.""" + items = range(5) + sliced = utils.newest_slice(items, 50) + self.assertEqual(list(sliced), list(items)) + + if __name__ == '__main__': unittest.main() diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index 630f11c13..11016b788 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -27,6 +27,7 @@ import os.path import collections import functools import contextlib +import itertools from PyQt5.QtCore import Qt from PyQt5.QtGui import QKeySequence, QColor @@ -552,3 +553,22 @@ def force_encoding(text, encoding): This replaces all chars not encodable with question marks. """ return text.encode(encoding, errors='replace').decode(encoding) + + +def newest_slice(iterable, count): + """Get an iterable for the n newest items of the given iterable. + + Args: + count: How many elements to get. + 0: get no items: + n: get the n newest items + -1: get all items + """ + if count < -1: + raise ValueError("count can't be smaller than -1!") + elif count == 0: + return [] + elif count == -1 or len(iterable) < count: + return iterable + else: + return itertools.islice(iterable, len(iterable) - count, len(iterable))