Add a test for utils.chunk

This commit is contained in:
Florian Bruhin 2018-02-27 11:20:19 +01:00
parent bd87b4eb10
commit 898f5c50c4
2 changed files with 18 additions and 1 deletions

View File

@ -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]

View File

@ -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))