-
Notifications
You must be signed in to change notification settings - Fork 826
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2985 from nekeal/feature/extend-optionlist
Adds an interface for replacing prompt of an individual option in an `OptionList`
- Loading branch information
Showing
6 changed files
with
673 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"""Test replacing options prompt from an option list.""" | ||
import pytest | ||
|
||
from textual.app import App, ComposeResult | ||
from textual.widgets import OptionList | ||
from textual.widgets.option_list import Option, OptionDoesNotExist | ||
|
||
|
||
class OptionListApp(App[None]): | ||
"""Test option list application.""" | ||
|
||
def compose(self) -> ComposeResult: | ||
yield OptionList( | ||
Option("0", id="0"), | ||
Option("line1\nline2"), | ||
) | ||
|
||
|
||
async def test_replace_option_prompt_with_invalid_id() -> None: | ||
"""Attempting to replace the prompt of an option ID that doesn't exist should raise an exception.""" | ||
async with OptionListApp().run_test() as pilot: | ||
with pytest.raises(OptionDoesNotExist): | ||
pilot.app.query_one(OptionList).replace_option_prompt("does-not-exist", "new-prompt") | ||
|
||
|
||
async def test_replace_option_prompt_with_invalid_index() -> None: | ||
"""Attempting to replace the prompt of an option index that doesn't exist should raise an exception.""" | ||
async with OptionListApp().run_test() as pilot: | ||
with pytest.raises(OptionDoesNotExist): | ||
pilot.app.query_one(OptionList).replace_option_prompt_at_index(23, "new-prompt") | ||
|
||
|
||
async def test_replace_option_prompt_with_valid_id() -> None: | ||
"""It should be possible to replace the prompt of an option ID that does exist.""" | ||
async with OptionListApp().run_test() as pilot: | ||
option_list = pilot.app.query_one(OptionList) | ||
option_list.replace_option_prompt("0", "new-prompt") | ||
assert option_list.get_option("0").prompt == "new-prompt" | ||
|
||
|
||
async def test_replace_option_prompt_with_valid_index() -> None: | ||
"""It should be possible to replace the prompt of an option index that does exist.""" | ||
async with OptionListApp().run_test() as pilot: | ||
option_list = pilot.app.query_one(OptionList).replace_option_prompt_at_index(1, "new-prompt") | ||
assert option_list.get_option_at_index(1).prompt == "new-prompt" | ||
|
||
|
||
async def test_replace_single_line_option_prompt_with_multiple() -> None: | ||
"""It should be possible to replace single line prompt with multiple lines """ | ||
new_prompt = "new-prompt\nsecond line" | ||
async with OptionListApp().run_test() as pilot: | ||
option_list = pilot.app.query_one(OptionList) | ||
option_list.replace_option_prompt("0", new_prompt) | ||
assert option_list.get_option("0").prompt == new_prompt | ||
|
||
|
||
async def test_replace_multiple_line_option_prompt_with_single() -> None: | ||
"""It should be possible to replace multiple line prompt with a single line""" | ||
new_prompt = "new-prompt" | ||
async with OptionListApp().run_test() as pilot: | ||
option_list = pilot.app.query_one(OptionList) | ||
option_list.replace_option_prompt("0", new_prompt) | ||
assert option_list.get_option("0").prompt == new_prompt | ||
|
||
|
||
async def test_replace_multiple_line_option_prompt_with_multiple() -> None: | ||
"""It should be possible to replace multiple line prompt with multiple lines""" | ||
new_prompt = "new-prompt\nsecond line" | ||
async with OptionListApp().run_test() as pilot: | ||
option_list = pilot.app.query_one(OptionList) | ||
option_list.replace_option_prompt_at_index(1, new_prompt) | ||
assert option_list.get_option_at_index(1).prompt == new_prompt |
Oops, something went wrong.