textual/tests/select/test_empty_select.py
Rodrigo Girão Serrão 8997615430
Improve Select.
Use a special value to flag a blank selection, add methods is_blank and clear, slightly refactor the way the widget is setup to avoid having attributes _initial_options and _options.
Implement exceptions to be raised when the widget is entering bad state.
2023-11-01 14:08:44 +00:00

53 lines
1.5 KiB
Python

import pytest
from textual.app import App
from textual.widgets import Select
from textual.widgets.select import EmptySelectError
async def test_empty_select_is_ok_with_blanks():
class SelectApp(App[None]):
def compose(self):
yield Select([])
app = SelectApp()
async with app.run_test():
# Sanity check:
assert app.query_one(Select).is_blank()
async def test_empty_set_options_is_ok_with_blanks():
class SelectApp(App[None]):
def compose(self):
yield Select([(str(n), n) for n in range(3)], value=0)
app = SelectApp()
async with app.run_test():
select = app.query_one(Select)
assert not select.is_blank() # Sanity check.
select.set_options([])
assert select.is_blank() # Sanity check.
async def test_empty_select_raises_exception_if_allow_blank_is_false():
class SelectApp(App[None]):
def compose(self):
yield Select([], allow_blank=False)
app = SelectApp()
with pytest.raises(EmptySelectError):
async with app.run_test():
pass
async def test_empty_set_options_raises_exception_if_allow_blank_is_false():
class SelectApp(App[None]):
def compose(self):
yield Select([(str(n), n) for n in range(3)], allow_blank=False)
app = SelectApp()
async with app.run_test():
select = app.query_one(Select)
assert not select.is_blank() # Sanity check.
with pytest.raises(EmptySelectError):
select.set_options([])