Skip to content

Commit

Permalink
Added a test case
Browse files Browse the repository at this point in the history
  • Loading branch information
hirak99 committed Nov 5, 2023
1 parent 12bc3b3 commit 9dd7753
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/code/snap_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def find_target(config: configs.Config, suffix: str) -> Optional[snap_holder.Sna


def _all_but_last_k(array: list[_GenericT], k: int) -> Iterator[_GenericT]:
"""All but at most k last elements."""
if k < 0:
raise ValueError(f"k = {k} < 0")
yield from array[: len(array) - k]
Expand Down
3 changes: 3 additions & 0 deletions src/code/snap_operator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ def test_all_but_k(self):
self.assertEqual(
list(snap_operator._all_but_last_k([1, 2, 3, 4], 0)), [1, 2, 3, 4]
)
# Leave -2 out of 4 elements - causes an error.
with self.assertRaisesRegex(ValueError, r"k = .+ < 0"):
list(snap_operator._all_but_last_k([1, 2, 3, 4], -2))
# Leave 5 out of 2 elements. Succeeds.
self.assertEqual(list(snap_operator._all_but_last_k([1, 2], 5)), [])

def setUp(self) -> None:
super().setUp()
Expand Down

0 comments on commit 9dd7753

Please sign in to comment.