Skip to content

Commit 102d7f6

Browse files
committed
test: cover choices out-of-bounds
1 parent 1ef409c commit 102d7f6

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

bink/choices.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ def __getitem__(self, idx: int) -> str:
4040
if not isinstance(idx, int):
4141
raise TypeError
4242

43-
if idx < 0 or idx > self._len:
43+
# The index is 0-based; valid values range from 0 to ``self._len - 1``.
44+
# The previous check allowed ``idx == self._len`` which would defer the
45+
# bounds validation to the underlying C library, resulting in a
46+
# ``RuntimeError`` instead of the expected ``IndexError``. Ensure the
47+
# upper bound is exclusive so out-of-range access raises ``IndexError``
48+
# consistently.
49+
if idx < 0 or idx >= self._len:
4450
raise IndexError
4551

4652
return self.get_text(idx)

tests/test_choices.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
from bink.story import story_from_file
3+
4+
5+
def test_choices_getitem_out_of_bounds():
6+
story = story_from_file("inkfiles/TheIntercept.ink.json")
7+
# Advance the story until at least one choice is available
8+
while story.can_continue() and len(story.choices) == 0:
9+
story.cont()
10+
choices = story.choices
11+
with pytest.raises(IndexError):
12+
_ = choices[len(choices)]

0 commit comments

Comments
 (0)