Merge pull request #1529 from davep/blackify-tests

Run black over some more tests
This commit is contained in:
Dave Pearson
2023-01-09 15:04:00 +00:00
committed by GitHub
8 changed files with 65 additions and 86 deletions

View File

@@ -94,8 +94,9 @@ def test_arrange_dock_bottom():
assert widgets == {child, header}
assert spacing == Spacing(0, 0, 1, 0)
def test_arrange_dock_badly():
child = Widget(id="child")
child.styles.dock = "nowhere"
with pytest.raises(AssertionError):
_ = arrange( Widget(), [child], Size(80, 24), Size(80, 24))
_ = arrange(Widget(), [child], Size(80, 24), Size(80, 24))

View File

@@ -10,6 +10,7 @@ class Focusable(Widget, can_focus=True):
class NonFocusable(Widget, can_focus=False, can_focus_children=False):
pass
class ChildrenFocusableOnly(Widget, can_focus=False, can_focus_children=True):
pass

View File

@@ -3,15 +3,18 @@ import pytest
from textual.widget import Widget
from textual._node_list import NodeList
def test_empty_list():
"""Does an empty node list report as being empty?"""
assert len(NodeList())==0
assert len(NodeList()) == 0
def test_add_one():
"""Does adding a node to the node list report as having one item?"""
nodes = NodeList()
nodes._append(Widget())
assert len(nodes)==1
assert len(nodes) == 1
def test_repeat_add_one():
"""Does adding the same item to the node list ignore the additional adds?"""
@@ -19,7 +22,8 @@ def test_repeat_add_one():
widget = Widget()
for _ in range(1000):
nodes._append(widget)
assert len(nodes)==1
assert len(nodes) == 1
def test_insert():
nodes = NodeList()
@@ -28,8 +32,9 @@ def test_insert():
widget3 = Widget()
nodes._append(widget1)
nodes._append(widget3)
nodes._insert(1,widget2)
assert list(nodes) == [widget1,widget2,widget3]
nodes._insert(1, widget2)
assert list(nodes) == [widget1, widget2, widget3]
def test_truthy():
"""Does a node list act as a truthy object?"""
@@ -38,6 +43,7 @@ def test_truthy():
nodes._append(Widget())
assert bool(nodes)
def test_contains():
"""Can we check if a widget is (not) within the list?"""
widget = Widget()
@@ -47,6 +53,7 @@ def test_contains():
assert widget in nodes
assert Widget() not in nodes
def test_index():
"""Can we get the index of a widget in the list?"""
widget = Widget()
@@ -56,6 +63,7 @@ def test_index():
nodes._append(widget)
assert nodes.index(widget) == 0
def test_remove():
"""Can we remove a widget we've added?"""
widget = Widget()
@@ -65,29 +73,31 @@ def test_remove():
nodes._remove(widget)
assert widget not in nodes
def test_clear():
"""Can we clear the list?"""
nodes = NodeList()
assert len(nodes)==0
assert len(nodes) == 0
widgets = [Widget() for _ in range(1000)]
for widget in widgets:
nodes._append(widget)
assert len(nodes)==1000
assert len(nodes) == 1000
for widget in widgets:
assert widget in nodes
nodes._clear()
assert len(nodes)==0
assert len(nodes) == 0
for widget in widgets:
assert widget not in nodes
def test_listy():
nodes = NodeList()
widget1 = Widget()
widget2 = Widget()
nodes._append(widget1)
nodes._append(widget2)
assert list(nodes)==[widget1, widget2]
assert list(reversed(nodes))==[widget2, widget1]
assert nodes[0]==widget1
assert nodes[1]==widget2
assert nodes[0:2]==[widget1, widget2]
assert list(nodes) == [widget1, widget2]
assert list(reversed(nodes)) == [widget2, widget1]
assert nodes[0] == widget1
assert nodes[1] == widget2
assert nodes[0:2] == [widget1, widget2]

View File

@@ -27,12 +27,15 @@ class ListPathApp(App[None]):
CSS_PATH = ["test.css", Path("/another/path.css")]
@pytest.mark.parametrize("app,expected_css_path_attribute", [
(RelativePathObjectApp(), [APP_DIR / "test.css"]),
(RelativePathStrApp(), [APP_DIR / "test.css"]),
(AbsolutePathObjectApp(), [Path("/tmp/test.css")]),
(AbsolutePathStrApp(), [Path("/tmp/test.css")]),
(ListPathApp(), [APP_DIR / "test.css", Path("/another/path.css")]),
])
@pytest.mark.parametrize(
"app,expected_css_path_attribute",
[
(RelativePathObjectApp(), [APP_DIR / "test.css"]),
(RelativePathStrApp(), [APP_DIR / "test.css"]),
(AbsolutePathObjectApp(), [Path("/tmp/test.css")]),
(AbsolutePathStrApp(), [Path("/tmp/test.css")]),
(ListPathApp(), [APP_DIR / "test.css", Path("/another/path.css")]),
],
)
def test_css_paths_of_various_types(app, expected_css_path_attribute):
assert app.css_path == [path.absolute() for path in expected_css_path_attribute]

View File

@@ -12,7 +12,9 @@ async def test_unmount():
class UnmountWidget(Container):
def on_unmount(self, event: events.Unmount):
unmount_ids.append(f"{self.__class__.__name__}#{self.id}-{self.parent is not None}-{len(self.children)}")
unmount_ids.append(
f"{self.__class__.__name__}#{self.id}-{self.parent is not None}-{len(self.children)}"
)
class MyScreen(Screen):
def compose(self) -> ComposeResult:

View File

@@ -3,6 +3,7 @@ import pytest
from textual.app import App
from textual.widget import Widget, WidgetError
async def test_widget_move_child() -> None:
"""Test moving a widget in a child list."""
@@ -35,29 +36,24 @@ async def test_widget_move_child() -> None:
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 )]
widgets = [Widget(id=f"widget-{n}") for n in range(10)]
# Test attempting to move past the end of the child list.
async with App().run_test() as pilot:
container = Widget(*widgets)
await pilot.app.mount(container)
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 with App().run_test() as pilot:
container = Widget(*widgets)
await pilot.app.mount(container)
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.
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:
async with App().run_test() as pilot:
container = Widget(*widgets)
@@ -68,12 +64,7 @@ async def test_widget_move_child() -> None:
assert container.children[2].id == "widget-2"
# Test the different permutations of moving one widget after another.
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:
async with App().run_test() as pilot:
container = Widget(*widgets)

View File

@@ -5,16 +5,19 @@ from textual.widget import Widget, WidgetError, MountError
from textual.widgets import Static
from textual.css.query import TooManyMatches
class SelfOwn(Widget):
"""Test a widget that tries to own itself."""
def __init__(self) -> None:
super().__init__(self)
async def test_mount_via_app() -> None:
"""Perform mount tests via the app."""
# Make a background set of widgets.
widgets = [Static(id=f"starter-{n}") for n in range( 10 )]
widgets = [Static(id=f"starter-{n}") for n in range(10)]
async with App().run_test() as pilot:
with pytest.raises(WidgetError):

View File

@@ -4,6 +4,7 @@ from textual.widget import Widget
from textual.widgets import Static, Button
from textual.containers import Container
async def test_remove_single_widget():
"""It should be possible to the only widget on a screen."""
async with App().run_test() as pilot:
@@ -12,6 +13,7 @@ async def test_remove_single_widget():
await pilot.app.query_one(Static).remove()
assert len(pilot.app.screen.children) == 0
async def test_many_remove_all_widgets():
"""It should be possible to remove all widgets on a multi-widget screen."""
async with App().run_test() as pilot:
@@ -20,6 +22,7 @@ async def test_many_remove_all_widgets():
await pilot.app.query(Static).remove()
assert len(pilot.app.screen.children) == 0
async def test_many_remove_some_widgets():
"""It should be possible to remove some widgets on a multi-widget screen."""
async with App().run_test() as pilot:
@@ -28,79 +31,42 @@ async def test_many_remove_some_widgets():
await pilot.app.query(".is-0").remove()
assert len(pilot.app.screen.children) == 5
async def test_remove_branch():
"""It should be possible to remove a whole branch in the DOM."""
async with App().run_test() as pilot:
await pilot.app.mount(
Container(
Container(
Container(
Container(
Container(
Static()
)
)
)
)
),
Container(Container(Container(Container(Container(Static()))))),
Static(),
Container(
Container(
Container(
Container(
Container(
Static()
)
)
)
)
),
Container(Container(Container(Container(Container(Static()))))),
)
assert len(pilot.app.screen.walk_children(with_self=False)) == 13
await pilot.app.screen.children[0].remove()
assert len(pilot.app.screen.walk_children(with_self=False)) == 7
async def test_remove_overlap():
"""It should be possible to remove an overlapping collection of widgets."""
async with App().run_test() as pilot:
await pilot.app.mount(
Container(
Container(
Container(
Container(
Container(
Static()
)
)
)
)
),
Container(Container(Container(Container(Container(Static()))))),
Static(),
Container(
Container(
Container(
Container(
Container(
Static()
)
)
)
)
),
Container(Container(Container(Container(Container(Static()))))),
)
assert len(pilot.app.screen.walk_children(with_self=False)) == 13
await pilot.app.query(Container).remove()
assert len(pilot.app.screen.walk_children(with_self=False)) == 1
async def test_remove_move_focus():
"""Removing a focused widget should settle focus elsewhere."""
async with App().run_test() as pilot:
buttons = [ Button(str(n)) for n in range(10)]
buttons = [Button(str(n)) for n in range(10)]
await pilot.app.mount(Container(*buttons[:5]), Container(*buttons[5:]))
assert len(pilot.app.screen.children) == 2
assert len(pilot.app.screen.walk_children(with_self=False)) == 12
assert pilot.app.focused is None
await pilot.press( "tab" )
await pilot.press("tab")
assert pilot.app.focused is not None
assert pilot.app.focused == buttons[0]
await pilot.app.screen.children[0].remove()
@@ -109,13 +75,14 @@ async def test_remove_move_focus():
assert pilot.app.focused is not None
assert pilot.app.focused == buttons[9]
async def test_widget_remove_order():
"""A Widget.remove of a top-level widget should cause bottom-first removal."""
removals: list[str] = []
class Removable(Container):
def on_unmount( self, _ ):
def on_unmount(self, _):
removals.append(self.id if self.id is not None else "unknown")
async with App().run_test() as pilot:
@@ -127,13 +94,14 @@ async def test_widget_remove_order():
assert len(pilot.app.screen.walk_children(with_self=False)) == 0
assert removals == ["grandchild", "child", "parent"]
async def test_query_remove_order():
"""A DOMQuery.remove of a top-level widget should cause bottom-first removal."""
removals: list[str] = []
class Removable(Container):
def on_unmount( self, _ ):
def on_unmount(self, _):
removals.append(self.id if self.id is not None else "unknown")
async with App().run_test() as pilot: