Add a test for utils.chunk
This commit is contained in:
parent
bd87b4eb10
commit
898f5c50c4
@ -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]
|
||||
|
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user