Skip to content

Commit 0a9b960

Browse files
authored
Extend OptionList testing (#2166)
* Add tests for errors when removing things that don't exist * Add tests for errors when toggling enabled/disabled on invalid options
1 parent 87f96ef commit 0a9b960

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

tests/option_list/test_option_list_disabled.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
from __future__ import annotations
44

5+
import pytest
6+
57
from textual.app import App, ComposeResult
68
from textual.widgets import OptionList
7-
from textual.widgets.option_list import Option
9+
from textual.widgets.option_list import Option, OptionDoesNotExist
810

911

1012
class OptionListApp(App[None]):
@@ -78,3 +80,31 @@ async def test_disabled_to_enabled_via_id() -> None:
7880
assert option_list.get_option(str(n)).disabled is True
7981
option_list.enable_option(str(n))
8082
assert option_list.get_option(str(n)).disabled is False
83+
84+
85+
async def test_disable_invalid_id() -> None:
86+
"""Disabling an option via an ID that does not exist should throw an error."""
87+
async with OptionListApp(True).run_test() as pilot:
88+
with pytest.raises(OptionDoesNotExist):
89+
pilot.app.query_one(OptionList).disable_option("does-not-exist")
90+
91+
92+
async def test_disable_invalid_index() -> None:
93+
"""Disabling an option via an index that does not exist should throw an error."""
94+
async with OptionListApp(True).run_test() as pilot:
95+
with pytest.raises(OptionDoesNotExist):
96+
pilot.app.query_one(OptionList).disable_option_at_index(4242)
97+
98+
99+
async def test_enable_invalid_id() -> None:
100+
"""Disabling an option via an ID that does not exist should throw an error."""
101+
async with OptionListApp(False).run_test() as pilot:
102+
with pytest.raises(OptionDoesNotExist):
103+
pilot.app.query_one(OptionList).enable_option("does-not-exist")
104+
105+
106+
async def test_enable_invalid_index() -> None:
107+
"""Disabling an option via an index that does not exist should throw an error."""
108+
async with OptionListApp(False).run_test() as pilot:
109+
with pytest.raises(OptionDoesNotExist):
110+
pilot.app.query_one(OptionList).enable_option_at_index(4242)

tests/option_list/test_option_removal.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
from __future__ import annotations
44

5+
import pytest
6+
57
from textual.app import App, ComposeResult
68
from textual.widgets import OptionList
7-
from textual.widgets.option_list import Option
9+
from textual.widgets.option_list import Option, OptionDoesNotExist
810

911

1012
class OptionListApp(App[None]):
@@ -83,3 +85,17 @@ async def test_remove_all_options_via_id() -> None:
8385
option_list.remove_option("1")
8486
assert option_list.option_count == 0
8587
assert option_list.highlighted is None
88+
89+
90+
async def test_remove_invalid_id() -> None:
91+
"""Attempting to remove an option ID that doesn't exist should raise an exception."""
92+
async with OptionListApp().run_test() as pilot:
93+
with pytest.raises(OptionDoesNotExist):
94+
pilot.app.query_one(OptionList).remove_option("does-not-exist")
95+
96+
97+
async def test_remove_invalid_index() -> None:
98+
"""Attempting to remove an option index that doesn't exist should raise an exception."""
99+
async with OptionListApp().run_test() as pilot:
100+
with pytest.raises(OptionDoesNotExist):
101+
pilot.app.query_one(OptionList).remove_option_at_index(23)

0 commit comments

Comments
 (0)