From 102d7f6fbbb98bc343506e00d101abc80744e21f Mon Sep 17 00:00:00 2001 From: Rafael Garcia Date: Wed, 27 Aug 2025 18:06:13 +0200 Subject: [PATCH] test: cover choices out-of-bounds --- bink/choices.py | 8 +++++++- tests/test_choices.py | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/test_choices.py diff --git a/bink/choices.py b/bink/choices.py index cf1d13b..044bca6 100644 --- a/bink/choices.py +++ b/bink/choices.py @@ -40,7 +40,13 @@ def __getitem__(self, idx: int) -> str: if not isinstance(idx, int): raise TypeError - if idx < 0 or idx > self._len: + # The index is 0-based; valid values range from 0 to ``self._len - 1``. + # The previous check allowed ``idx == self._len`` which would defer the + # bounds validation to the underlying C library, resulting in a + # ``RuntimeError`` instead of the expected ``IndexError``. Ensure the + # upper bound is exclusive so out-of-range access raises ``IndexError`` + # consistently. + if idx < 0 or idx >= self._len: raise IndexError return self.get_text(idx) diff --git a/tests/test_choices.py b/tests/test_choices.py new file mode 100644 index 0000000..f325255 --- /dev/null +++ b/tests/test_choices.py @@ -0,0 +1,12 @@ +import pytest +from bink.story import story_from_file + + +def test_choices_getitem_out_of_bounds(): + story = story_from_file("inkfiles/TheIntercept.ink.json") + # Advance the story until at least one choice is available + while story.can_continue() and len(story.choices) == 0: + story.cont() + choices = story.choices + with pytest.raises(IndexError): + _ = choices[len(choices)]