|
| 1 | +import pytest |
| 2 | + |
| 3 | +from textual.app import App |
| 4 | +from textual.widget import Widget, WidgetError |
| 5 | + |
| 6 | +async def test_widget_move_child() -> None: |
| 7 | + """Test moving a widget in a child list.""" |
| 8 | + |
| 9 | + # Test calling move_child with no direction. |
| 10 | + async with App().run_test() as pilot: |
| 11 | + child = Widget(Widget()) |
| 12 | + await pilot.app.mount(child) |
| 13 | + with pytest.raises(WidgetError): |
| 14 | + pilot.app.screen.move_child(child) |
| 15 | + |
| 16 | + # Test calling move_child with more than one direction. |
| 17 | + async with App().run_test() as pilot: |
| 18 | + child = Widget(Widget()) |
| 19 | + await pilot.app.mount(child) |
| 20 | + with pytest.raises(WidgetError): |
| 21 | + pilot.app.screen.move_child(child, before=1, after=2) |
| 22 | + |
| 23 | + # Test attempting to move a child that isn't ours. |
| 24 | + async with App().run_test() as pilot: |
| 25 | + child = Widget(Widget()) |
| 26 | + await pilot.app.mount(child) |
| 27 | + with pytest.raises(WidgetError): |
| 28 | + pilot.app.screen.move_child(Widget(), before=child) |
| 29 | + |
| 30 | + # Test attempting to move relative to a widget that isn't a child. |
| 31 | + async with App().run_test() as pilot: |
| 32 | + child = Widget(Widget()) |
| 33 | + await pilot.app.mount(child) |
| 34 | + with pytest.raises(WidgetError): |
| 35 | + pilot.app.screen.move_child(child, before=Widget()) |
| 36 | + |
| 37 | + # Make a background set of widgets. |
| 38 | + widgets = [Widget(id=f"widget-{n}") for n in range( 10 )] |
| 39 | + |
| 40 | + # Test attempting to move past the end of the child list. |
| 41 | + async with App().run_test() as pilot: |
| 42 | + container = Widget(*widgets) |
| 43 | + await pilot.app.mount(container) |
| 44 | + with pytest.raises(WidgetError): |
| 45 | + container.move_child(widgets[0], before=len(widgets)+10) |
| 46 | + |
| 47 | + # Test attempting to move before the end of the child list. |
| 48 | + async with App().run_test() as pilot: |
| 49 | + container = Widget(*widgets) |
| 50 | + await pilot.app.mount(container) |
| 51 | + with pytest.raises(WidgetError): |
| 52 | + container.move_child(widgets[0], before=-(len(widgets)+10)) |
| 53 | + |
| 54 | + # Test the different permutations of moving one widget before another. |
| 55 | + perms = ( |
| 56 | + ( 1, 0 ), |
| 57 | + ( widgets[1], 0 ), |
| 58 | + ( 1, widgets[ 0 ] ), |
| 59 | + ( widgets[ 1 ], widgets[ 0 ]) |
| 60 | + ) |
| 61 | + for child, target in perms: |
| 62 | + async with App().run_test() as pilot: |
| 63 | + container = Widget(*widgets) |
| 64 | + await pilot.app.mount(container) |
| 65 | + container.move_child(child, before=target) |
| 66 | + assert container.children[0].id == "widget-1" |
| 67 | + assert container.children[1].id == "widget-0" |
| 68 | + assert container.children[2].id == "widget-2" |
| 69 | + |
| 70 | + # Test the different permutations of moving one widget after another. |
| 71 | + perms = ( |
| 72 | + ( 0, 1 ), |
| 73 | + ( widgets[0], 1 ), |
| 74 | + ( 0, widgets[ 1 ] ), |
| 75 | + ( widgets[ 0 ], widgets[ 1 ]) |
| 76 | + ) |
| 77 | + for child, target in perms: |
| 78 | + async with App().run_test() as pilot: |
| 79 | + container = Widget(*widgets) |
| 80 | + await pilot.app.mount(container) |
| 81 | + container.move_child(child, after=target) |
| 82 | + assert container.children[0].id == "widget-1" |
| 83 | + assert container.children[1].id == "widget-0" |
| 84 | + assert container.children[2].id == "widget-2" |
| 85 | + |
| 86 | + # Test moving after a child after the last child. |
| 87 | + async with App().run_test() as pilot: |
| 88 | + container = Widget(*widgets) |
| 89 | + await pilot.app.mount(container) |
| 90 | + container.move_child(widgets[0], after=widgets[-1]) |
| 91 | + assert container.children[0].id == "widget-1" |
| 92 | + assert container.children[-1].id == "widget-0" |
| 93 | + |
| 94 | + # Test moving after a child after the last child's numeric position. |
| 95 | + async with App().run_test() as pilot: |
| 96 | + container = Widget(*widgets) |
| 97 | + await pilot.app.mount(container) |
| 98 | + container.move_child(widgets[0], after=widgets[9]) |
| 99 | + assert container.children[0].id == "widget-1" |
| 100 | + assert container.children[-1].id == "widget-0" |
0 commit comments