Make the widget moving tests more granular and add more tests

Adds two (currently xfail) tests to illustrate #1743
This commit is contained in:
Dave Pearson
2023-05-09 11:19:00 +01:00
parent dd7e768887
commit d3de6d1587

View File

@@ -1,58 +1,92 @@
from __future__ import annotations
import pytest import pytest
from textual.app import App from textual.app import App
from textual.widget import Widget, WidgetError from textual.widget import Widget, WidgetError
async def test_widget_move_child() -> None: async def test_move_child_no_direction() -> None:
"""Test moving a widget in a child list.""" """Test moving a widget in a child list."""
# Test calling move_child with no direction.
async with App().run_test() as pilot: async with App().run_test() as pilot:
child = Widget(Widget()) child = Widget(Widget())
await pilot.app.mount(child) await pilot.app.mount(child)
with pytest.raises(WidgetError): with pytest.raises(WidgetError):
pilot.app.screen.move_child(child) pilot.app.screen.move_child(child)
# Test calling move_child with more than one direction.
async def test_move_child_both_directions() -> None:
"""Test calling move_child with more than one direction."""
async with App().run_test() as pilot: async with App().run_test() as pilot:
child = Widget(Widget()) child = Widget(Widget())
await pilot.app.mount(child) await pilot.app.mount(child)
with pytest.raises(WidgetError): with pytest.raises(WidgetError):
pilot.app.screen.move_child(child, before=1, after=2) pilot.app.screen.move_child(child, before=1, after=2)
# Test attempting to move a child that isn't ours.
async def test_move_child_not_our_child() -> None:
"""Test attempting to move a child that isn't ours."""
async with App().run_test() as pilot: async with App().run_test() as pilot:
child = Widget(Widget()) child = Widget(Widget())
await pilot.app.mount(child) await pilot.app.mount(child)
with pytest.raises(WidgetError): with pytest.raises(WidgetError):
pilot.app.screen.move_child(Widget(), before=child) pilot.app.screen.move_child(Widget(), before=child)
# Test attempting to move relative to a widget that isn't a child.
async def test_move_child_to_outside() -> None:
"""Test attempting to move relative to a widget that isn't a child."""
async with App().run_test() as pilot: async with App().run_test() as pilot:
child = Widget(Widget()) child = Widget(Widget())
await pilot.app.mount(child) await pilot.app.mount(child)
with pytest.raises(WidgetError): with pytest.raises(WidgetError):
pilot.app.screen.move_child(child, before=Widget()) pilot.app.screen.move_child(child, before=Widget())
# Make a background set of widgets.
widgets = [Widget(id=f"widget-{n}") for n in range(10)]
# Test attempting to move past the end of the child list. @pytest.mark.xfail(
strict=True, reason="https://github.com/Textualize/textual/issues/1743"
)
async def test_move_child_before_itself() -> None:
"""Test moving a widget before itself."""
async with App().run_test() as pilot: async with App().run_test() as pilot:
child = Widget(Widget())
await pilot.app.mount(child)
pilot.app.screen.move_child(child, before=child)
@pytest.mark.xfail(
strict=True, reason="https://github.com/Textualize/textual/issues/1743"
)
async def test_move_child_after_itself() -> None:
"""Test moving a widget after itself."""
async with App().run_test() as pilot:
child = Widget(Widget())
await pilot.app.mount(child)
pilot.app.screen.move_child(child, after=child)
async def test_move_past_end_of_child_list() -> None:
"""Test attempting to move past the end of the child list."""
async with App().run_test() as pilot:
widgets = [Widget(id=f"widget-{n}") for n in range(10)]
container = Widget(*widgets) container = Widget(*widgets)
await pilot.app.mount(container) await pilot.app.mount(container)
with pytest.raises(WidgetError): with pytest.raises(WidgetError):
container.move_child(widgets[0], before=len(widgets) + 10) container.move_child(widgets[0], before=len(widgets) + 10)
# Test attempting to move before the end of the child list.
async def test_move_before_end_of_child_list() -> None:
"""Test attempting to move before the end of the child list."""
async with App().run_test() as pilot: async with App().run_test() as pilot:
widgets = [Widget(id=f"widget-{n}") for n in range(10)]
container = Widget(*widgets) container = Widget(*widgets)
await pilot.app.mount(container) await pilot.app.mount(container)
with pytest.raises(WidgetError): with pytest.raises(WidgetError):
container.move_child(widgets[0], before=-(len(widgets) + 10)) container.move_child(widgets[0], before=-(len(widgets) + 10))
# Test the different permutations of moving one widget before another.
async def test_move_before_permutations() -> None:
"""Test the different permutations of moving one widget before another."""
widgets = [Widget(id=f"widget-{n}") for n in range(10)]
perms = ((1, 0), (widgets[1], 0), (1, widgets[0]), (widgets[1], widgets[0])) perms = ((1, 0), (widgets[1], 0), (1, widgets[0]), (widgets[1], widgets[0]))
for child, target in perms: for child, target in perms:
async with App().run_test() as pilot: async with App().run_test() as pilot:
@@ -63,7 +97,10 @@ async def test_widget_move_child() -> None:
assert container._nodes[1].id == "widget-0" assert container._nodes[1].id == "widget-0"
assert container._nodes[2].id == "widget-2" assert container._nodes[2].id == "widget-2"
# Test the different permutations of moving one widget after another.
async def test_move_after_permutations() -> None:
"""Test the different permutations of moving one widget after another."""
widgets = [Widget(id=f"widget-{n}") for n in range(10)]
perms = ((0, 1), (widgets[0], 1), (0, widgets[1]), (widgets[0], widgets[1])) perms = ((0, 1), (widgets[0], 1), (0, widgets[1]), (widgets[0], widgets[1]))
for child, target in perms: for child, target in perms:
async with App().run_test() as pilot: async with App().run_test() as pilot:
@@ -74,16 +111,22 @@ async def test_widget_move_child() -> None:
assert container._nodes[1].id == "widget-0" assert container._nodes[1].id == "widget-0"
assert container._nodes[2].id == "widget-2" assert container._nodes[2].id == "widget-2"
# Test moving after a child after the last child.
async def test_move_child_after_last_child() -> None:
"""Test moving after a child after the last child."""
async with App().run_test() as pilot: async with App().run_test() as pilot:
widgets = [Widget(id=f"widget-{n}") for n in range(10)]
container = Widget(*widgets) container = Widget(*widgets)
await pilot.app.mount(container) await pilot.app.mount(container)
container.move_child(widgets[0], after=widgets[-1]) container.move_child(widgets[0], after=widgets[-1])
assert container._nodes[0].id == "widget-1" assert container._nodes[0].id == "widget-1"
assert container._nodes[-1].id == "widget-0" assert container._nodes[-1].id == "widget-0"
# Test moving after a child after the last child's numeric position.
async def test_move_child_after_last_numeric_location() -> None:
"""Test moving after a child after the last child's numeric position."""
async with App().run_test() as pilot: async with App().run_test() as pilot:
widgets = [Widget(id=f"widget-{n}") for n in range(10)]
container = Widget(*widgets) container = Widget(*widgets)
await pilot.app.mount(container) await pilot.app.mount(container)
container.move_child(widgets[0], after=widgets[9]) container.move_child(widgets[0], after=widgets[9])