Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/pick/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ def __post_init__(self) -> None:
if len(self.options) == 0:
raise ValueError("options should not be an empty list")

if self.default_index >= len(self.options):
raise ValueError("default_index should be less than the length of options")
if self.default_index < -len(self.options) or self.default_index >= len(self.options):
raise ValueError("default_index is out of range of options")

if self.default_index < 0:
self.default_index += len(self.options)

if self.multiselect and self.min_selection_count > len(self.options):
raise ValueError(
Expand Down
19 changes: 18 additions & 1 deletion tests/test_pick.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from pick import Picker, Option
import pytest

from pick import Option, Picker


def test_move_up_down():
Expand Down Expand Up @@ -78,6 +80,21 @@ def test_disabled_option():
picker.move_down()
assert picker.get_selected() == (Option("option3"), 2)

def test_negative_default_index():
options = ["option1", "option2", "option3"]
picker = Picker(options, default_index=-1)
assert picker.get_selected() == ("option3", 2)
lines, _ = picker.get_lines()
assert "* option3" in lines

picker = Picker(options, default_index=-3)
assert picker.get_selected() == ("option1", 0)

with pytest.raises(ValueError):
Picker(options, default_index=-4)
with pytest.raises(ValueError):
Picker(options, default_index=3)


def test_mark_index_disabled_option():
options = [Option("option1"), Option("option2", enabled=False), Option("option3")]
Expand Down
Loading