diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index f7c1c90b0..f03d42844 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -663,6 +663,7 @@ def chunk(elems, n): If elems % n != 0, the last chunk will be smaller. """ - # FIXME test this + if n < 1: + raise ValueError("n needs to be at least 1!") for i in range(0, len(elems), n): yield elems[i:i + n] diff --git a/tests/unit/utils/test_utils.py b/tests/unit/utils/test_utils.py index e8391a74f..b2eef0237 100644 --- a/tests/unit/utils/test_utils.py +++ b/tests/unit/utils/test_utils.py @@ -787,3 +787,19 @@ class TestYaml: with tmpfile.open('w', encoding='utf-8') as f: utils.yaml_dump([1, 2], f) assert tmpfile.read() == '- 1\n- 2\n' + + +@pytest.mark.parametrize('elems, n, expected', [ + ([], 1, []), + ([1], 1, [[1]]), + ([1, 2], 2, [[1, 2]]), + ([1, 2, 3, 4], 2, [[1, 2], [3, 4]]), +]) +def test_chunk(elems, n, expected): + assert list(utils.chunk(elems, n)) == expected + + +@pytest.mark.parametrize('n', [-1, 0]) +def test_chunk_invalid(n): + with pytest.raises(ValueError): + list(utils.chunk([], n))