Improve tests.

This commit is contained in:
Rodrigo Girão Serrão
2023-01-30 11:50:02 +00:00
parent c4dbde1994
commit b023d4e02e
4 changed files with 264 additions and 37 deletions

View File

@@ -6,57 +6,90 @@ from textual.screen import Screen
from textual.widgets import Label from textual.widgets import Label
updates = 0
class _Label(Label):
"""Label widget that keeps track of its own updates."""
def refresh(self, *args, **kwargs):
global updates
updates += 1
return super().refresh(*args, **kwargs)
@pytest.mark.parametrize( @pytest.mark.parametrize(
"style, value", "style, value",
[ [
("grid_size_rows", 2), ("grid_size_rows", 3),
("grid_size_columns", 2), ("grid_size_columns", 3),
("grid_gutter_vertical", 2), ("grid_gutter_vertical", 4),
("grid_gutter_horizontal", 1), ("grid_gutter_horizontal", 4),
("grid_rows", "1fr 3fr"), ("grid_rows", "1fr 3fr"),
("grid_columns", "1fr 3fr"), ("grid_columns", "1fr 3fr"),
("scrollbar_gutter", "stable"),
("align_horizontal", "right"),
("align_vertical", "bottom"),
("align", ("right", "bottom")),
], ],
) )
def test_programmatic_style_change_refreshes_children_layout(style: str, value): async def test_programmatic_style_change_updates_children(style: str, value: object):
"""Regression test for #1607 https://github.com/Textualize/textual/issues/1607 """Regression test for #1607 https://github.com/Textualize/textual/issues/1607
Some programmatic style changes to a widget were not updating the layout of the Some programmatic style changes to a widget were not updating the layout of the
children widgets, which seemed to be happening when the style change did not affect children widgets, which seemed to be happening when the style change did not affect
the size of the widget but did affect the layout of the children. the size of the widget but did affect the layout of the children.
This test, in particular, checks the attributes that _should_ affect the size of the
children widgets.
""" """
global updates class MyApp(App[None]):
CSS = """
Grid { grid-size: 2 2; }
Label { width: 100%; height: 100%; }
"""
app = App() def compose(self):
app.DEFAULT_CSS = "Grid { grid-size: 1 1; }" yield Grid(
app._set_active() Label("one"),
app.push_screen(Screen()) Label("two"),
Label("three"),
Label("four"),
)
grid = Grid( app = MyApp()
_Label("one"),
_Label("two"),
_Label("three"),
_Label("four"),
)
app.screen._add_children(grid)
update_count = updates async with app.run_test() as pilot:
setattr(grid.styles, style, value) sizes = [(lbl.size.width, lbl.size.height) for lbl in app.screen.query(Label)]
print(updates, update_count)
assert updates > update_count setattr(app.query_one(Grid).styles, style, value)
await pilot.pause()
assert sizes != [
(lbl.size.width, lbl.size.height) for lbl in app.screen.query(Label)
]
@pytest.mark.parametrize(
"style, value",
[
("align_horizontal", "right"),
("align_vertical", "bottom"),
("align", ("right", "bottom")),
],
)
async def test_programmatic_align_change_updates_children_position(
style: str, value: str
):
"""Regression test for #1607 for the align(_xxx) styles.
See https://github.com/Textualize/textual/issues/1607.
"""
class MyApp(App[None]):
CSS = "Grid { grid-size: 2 2; }"
def compose(self):
yield Grid(
Label("one"),
Label("two"),
Label("three"),
Label("four"),
)
app = MyApp()
async with app.run_test() as pilot:
offsets = [(lbl.region.x, lbl.region.y) for lbl in app.screen.query(Label)]
setattr(app.query_one(Grid).styles, style, value)
await pilot.pause()
assert offsets != [
(lbl.region.x, lbl.region.y) for lbl in app.screen.query(Label)
]

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,29 @@
from textual.app import App
from textual.containers import Grid
from textual.widgets import Label
class ProgrammaticScrollbarGutterChange(App[None]):
CSS = """
Grid { grid-size: 2 2; scrollbar-size: 5 5; }
Label { width: 100%; height: 100%; background: red; }
"""
def compose(self):
yield Grid(
Label("one"),
Label("two"),
Label("three"),
Label("four"),
)
def on_key(self, event):
if event.key == "s":
self.query_one(Grid).styles.scrollbar_gutter = "stable"
app = ProgrammaticScrollbarGutterChange()
if __name__ == "__main__":
app().run()

View File

@@ -179,6 +179,16 @@ def test_nested_auto_heights(snap_compare):
assert snap_compare("snapshot_apps/nested_auto_heights.py", press=["1", "2", "_"]) assert snap_compare("snapshot_apps/nested_auto_heights.py", press=["1", "2", "_"])
def test_programmatic_scrollbar_gutter_change(snap_compare):
"""Regression test for #1607 https://github.com/Textualize/textual/issues/1607
See also tests/css/test_programmatic_style_changes.py for other related regression tests.
"""
assert snap_compare(
"snapshot_apps/programmatic_scrollbar_gutter_change.py", press=["s"]
)
# --- Other --- # --- Other ---