mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Run black over the tests
This commit is contained in:
@@ -31,6 +31,7 @@ MOVEMENT_KEYS = ["up", "down", "left", "right", "home", "end", "pageup", "pagedo
|
||||
# bindings set anywhere, and uses a default screen, should only have the one
|
||||
# binding in place: ctrl+c; it's hard-coded in the app class for now.
|
||||
|
||||
|
||||
class NoBindings(App[None]):
|
||||
"""An app with zero bindings."""
|
||||
|
||||
@@ -49,6 +50,7 @@ async def test_just_app_no_bindings() -> None:
|
||||
# words avoiding anything to do with movement keys. The result should be
|
||||
# that we see the letter a, ctrl+c, and nothing else.
|
||||
|
||||
|
||||
class AlphaBinding(App[None]):
|
||||
"""An app with a simple alpha key binding."""
|
||||
|
||||
@@ -71,6 +73,7 @@ async def test_just_app_alpha_binding() -> None:
|
||||
# To start with the screen has no bindings -- it's just a direct subclass
|
||||
# with no other changes.
|
||||
|
||||
|
||||
class ScreenNoBindings(Screen):
|
||||
"""A screen with no added bindings."""
|
||||
|
||||
@@ -90,7 +93,9 @@ class AppWithScreenNoBindings(App[None]):
|
||||
async def test_app_screen_has_no_movement_bindings() -> None:
|
||||
"""A screen with no bindings should not have movement key bindings."""
|
||||
async with AppWithScreenNoBindings().run_test() as pilot:
|
||||
assert sorted(list(pilot.app.screen._bindings.keys.keys())) != sorted(MOVEMENT_KEYS)
|
||||
assert sorted(list(pilot.app.screen._bindings.keys.keys())) != sorted(
|
||||
MOVEMENT_KEYS
|
||||
)
|
||||
|
||||
|
||||
##############################################################################
|
||||
@@ -100,6 +105,7 @@ async def test_app_screen_has_no_movement_bindings() -> None:
|
||||
# now do the same thing but with a binding added that isn't for a movement
|
||||
# key.
|
||||
|
||||
|
||||
class ScreenWithBindings(Screen):
|
||||
"""A screen with a simple alpha key binding."""
|
||||
|
||||
@@ -132,6 +138,7 @@ async def test_app_screen_with_bindings() -> None:
|
||||
# to see zero bindings in place, but do so when the screen has a child;
|
||||
# presumably making it pass as a container.
|
||||
|
||||
|
||||
class NoBindingsAndStaticWidgetNoBindings(App[None]):
|
||||
"""An app with no bindings, enclosing a widget with no bindings."""
|
||||
|
||||
@@ -153,6 +160,7 @@ async def test_just_app_no_bindings_widget_no_bindings() -> None:
|
||||
# any bindings that are in place actually fire the correct actions. To help
|
||||
# with this let's build a simple key/binding/action recorder base app.
|
||||
|
||||
|
||||
class AppKeyRecorder(App[None]):
|
||||
"""Base application class that can be used to record keystrokes."""
|
||||
|
||||
@@ -163,7 +171,7 @@ class AppKeyRecorder(App[None]):
|
||||
"""list[str]: All the test keys."""
|
||||
|
||||
@staticmethod
|
||||
def mk_bindings(prefix: str="") -> list[Binding]:
|
||||
def mk_bindings(prefix: str = "") -> list[Binding]:
|
||||
"""Make the binding list for testing an app.
|
||||
|
||||
Args:
|
||||
@@ -173,7 +181,8 @@ class AppKeyRecorder(App[None]):
|
||||
list[Binding]: The resulting list of bindings.
|
||||
"""
|
||||
return [
|
||||
Binding( key, f"{prefix}record('{key}')", key ) for key in [*AppKeyRecorder.ALPHAS, *MOVEMENT_KEYS]
|
||||
Binding(key, f"{prefix}record('{key}')", key)
|
||||
for key in [*AppKeyRecorder.ALPHAS, *MOVEMENT_KEYS]
|
||||
]
|
||||
|
||||
def __init__(self) -> None:
|
||||
@@ -189,7 +198,7 @@ class AppKeyRecorder(App[None]):
|
||||
"""
|
||||
self.pressed_keys.append(key)
|
||||
|
||||
def all_recorded(self, prefix: str="") -> None:
|
||||
def all_recorded(self, prefix: str = "") -> None:
|
||||
"""Were all the bindings recorded from the presses?
|
||||
|
||||
Args:
|
||||
@@ -206,8 +215,10 @@ class AppKeyRecorder(App[None]):
|
||||
# seeing what happens. First off let's start with an application that has
|
||||
# bindings, both for an alpha key, and also for all of the movement keys.
|
||||
|
||||
|
||||
class AppWithMovementKeysBound(AppKeyRecorder):
|
||||
"""An application with bindings."""
|
||||
|
||||
BINDINGS = AppKeyRecorder.mk_bindings()
|
||||
|
||||
|
||||
@@ -215,7 +226,7 @@ async def test_pressing_alpha_on_app() -> None:
|
||||
"""Test that pressing the alpha key, when it's bound on the app, results in an action fire."""
|
||||
async with AppWithMovementKeysBound().run_test() as pilot:
|
||||
await pilot.press(*AppKeyRecorder.ALPHAS)
|
||||
await pilot.pause(2/100)
|
||||
await pilot.pause(2 / 100)
|
||||
assert pilot.app.pressed_keys == [*AppKeyRecorder.ALPHAS]
|
||||
|
||||
|
||||
@@ -226,7 +237,7 @@ async def test_pressing_movement_keys_app() -> None:
|
||||
"""Test that pressing the movement keys, when they're bound on the app, results in an action fire."""
|
||||
async with AppWithMovementKeysBound().run_test() as pilot:
|
||||
await pilot.press(*AppKeyRecorder.ALL_KEYS)
|
||||
await pilot.pause(2/100)
|
||||
await pilot.pause(2 / 100)
|
||||
pilot.app.all_recorded()
|
||||
|
||||
|
||||
@@ -239,8 +250,10 @@ async def test_pressing_movement_keys_app() -> None:
|
||||
# able to handle all of the test keys on its own and nothing else should
|
||||
# grab them.
|
||||
|
||||
|
||||
class FocusableWidgetWithBindings(Static, can_focus=True):
|
||||
"""A widget that has its own bindings for the movement keys."""
|
||||
|
||||
BINDINGS = AppKeyRecorder.mk_bindings("local_")
|
||||
|
||||
async def action_local_record(self, key: str) -> None:
|
||||
@@ -262,9 +275,10 @@ async def test_focused_child_widget_with_movement_bindings() -> None:
|
||||
"""A focused child widget with movement bindings should handle its own actions."""
|
||||
async with AppWithWidgetWithBindings().run_test() as pilot:
|
||||
await pilot.press(*AppKeyRecorder.ALL_KEYS)
|
||||
await pilot.pause(2/100)
|
||||
await pilot.pause(2 / 100)
|
||||
pilot.app.all_recorded("locally_")
|
||||
|
||||
|
||||
##############################################################################
|
||||
# A focused widget within a screen that handles bindings.
|
||||
#
|
||||
@@ -274,9 +288,11 @@ async def test_focused_child_widget_with_movement_bindings() -> None:
|
||||
# bindings. What we should expect to see is that the bindings don't fire on
|
||||
# the widget (it has none) and instead get caught by the screen.
|
||||
|
||||
|
||||
class FocusableWidgetWithNoBindings(Static, can_focus=True):
|
||||
"""A widget that can receive focus but has no bindings."""
|
||||
|
||||
|
||||
class ScreenWithMovementBindings(Screen):
|
||||
"""A screen that binds keys, including movement keys."""
|
||||
|
||||
@@ -292,14 +308,16 @@ class ScreenWithMovementBindings(Screen):
|
||||
def on_mount(self) -> None:
|
||||
self.query_one(FocusableWidgetWithNoBindings).focus()
|
||||
|
||||
|
||||
class AppWithScreenWithBindingsWidgetNoBindings(AppKeyRecorder):
|
||||
"""An app with a non-default screen that handles movement key bindings."""
|
||||
|
||||
SCREENS = {"main":ScreenWithMovementBindings}
|
||||
SCREENS = {"main": ScreenWithMovementBindings}
|
||||
|
||||
def on_mount(self) -> None:
|
||||
self.push_screen("main")
|
||||
|
||||
|
||||
@pytest.mark.xfail(
|
||||
reason="Movement keys never make it to the screen with such bindings due to key inheritance and pre-bound movement keys [issue#1343]"
|
||||
)
|
||||
@@ -307,9 +325,10 @@ async def test_focused_child_widget_with_movement_bindings_on_screen() -> None:
|
||||
"""A focused child widget, with movement bindings in the screen, should trigger screen actions."""
|
||||
async with AppWithScreenWithBindingsWidgetNoBindings().run_test() as pilot:
|
||||
await pilot.press(*AppKeyRecorder.ALL_KEYS)
|
||||
await pilot.pause(2/100)
|
||||
await pilot.pause(2 / 100)
|
||||
pilot.app.all_recorded("screenly_")
|
||||
|
||||
|
||||
##############################################################################
|
||||
# A focused widget within a container within a screen that handles bindings.
|
||||
#
|
||||
@@ -322,6 +341,7 @@ async def test_focused_child_widget_with_movement_bindings_on_screen() -> None:
|
||||
# Although it's not at the end of the unit tests, this is potentially the
|
||||
# "final boss" of these tests.
|
||||
|
||||
|
||||
class ScreenWithMovementBindingsAndContainerAroundWidget(Screen):
|
||||
"""A screen that binds keys, including movement keys."""
|
||||
|
||||
@@ -337,14 +357,16 @@ class ScreenWithMovementBindingsAndContainerAroundWidget(Screen):
|
||||
def on_mount(self) -> None:
|
||||
self.query_one(FocusableWidgetWithNoBindings).focus()
|
||||
|
||||
|
||||
class AppWithScreenWithBindingsWrappedWidgetNoBindings(AppKeyRecorder):
|
||||
"""An app with a non-default screen that handles movement key bindings."""
|
||||
|
||||
SCREENS = {"main":ScreenWithMovementBindings}
|
||||
SCREENS = {"main": ScreenWithMovementBindings}
|
||||
|
||||
def on_mount(self) -> None:
|
||||
self.push_screen("main")
|
||||
|
||||
|
||||
@pytest.mark.xfail(
|
||||
reason="Movement keys never make it to the screen with such bindings due to key inheritance and pre-bound movement keys [issue#1343]"
|
||||
)
|
||||
@@ -352,9 +374,10 @@ async def test_contained_focused_child_widget_with_movement_bindings_on_screen()
|
||||
"""A contained focused child widget, with movement bindings in the screen, should trigger screen actions."""
|
||||
async with AppWithScreenWithBindingsWrappedWidgetNoBindings().run_test() as pilot:
|
||||
await pilot.press(*AppKeyRecorder.ALL_KEYS)
|
||||
await pilot.pause(2/100)
|
||||
await pilot.pause(2 / 100)
|
||||
pilot.app.all_recorded("screenly_")
|
||||
|
||||
|
||||
##############################################################################
|
||||
# A focused widget with bindings but no inheriting of bindings, on app.
|
||||
#
|
||||
@@ -365,8 +388,10 @@ async def test_contained_focused_child_widget_with_movement_bindings_on_screen()
|
||||
#
|
||||
# We should expect to see all of the test keys recorded post-press.
|
||||
|
||||
|
||||
class WidgetWithBindingsNoInherit(Static, can_focus=True, inherit_bindings=False):
|
||||
"""A widget that has its own bindings for the movement keys, no binding inheritance."""
|
||||
|
||||
BINDINGS = AppKeyRecorder.mk_bindings("local_")
|
||||
|
||||
async def action_local_record(self, key: str) -> None:
|
||||
@@ -388,9 +413,10 @@ async def test_focused_child_widget_with_movement_bindings_no_inherit() -> None:
|
||||
"""A focused child widget with movement bindings and inherit_bindings=False should handle its own actions."""
|
||||
async with AppWithWidgetWithBindingsNoInherit().run_test() as pilot:
|
||||
await pilot.press(*AppKeyRecorder.ALL_KEYS)
|
||||
await pilot.pause(2/100)
|
||||
await pilot.pause(2 / 100)
|
||||
pilot.app.all_recorded("locally_")
|
||||
|
||||
|
||||
##############################################################################
|
||||
# A focused widget with no bindings and no inheriting of bindings, on screen.
|
||||
#
|
||||
@@ -403,9 +429,13 @@ async def test_focused_child_widget_with_movement_bindings_no_inherit() -> None:
|
||||
# NOTE: no bindings are declared for the widget, which is different from
|
||||
# zero bindings declared.
|
||||
|
||||
class FocusableWidgetWithNoBindingsNoInherit(Static, can_focus=True, inherit_bindings=False):
|
||||
|
||||
class FocusableWidgetWithNoBindingsNoInherit(
|
||||
Static, can_focus=True, inherit_bindings=False
|
||||
):
|
||||
"""A widget that can receive focus but has no bindings and doesn't inherit bindings."""
|
||||
|
||||
|
||||
class ScreenWithMovementBindingsNoInheritChild(Screen):
|
||||
"""A screen that binds keys, including movement keys."""
|
||||
|
||||
@@ -421,21 +451,24 @@ class ScreenWithMovementBindingsNoInheritChild(Screen):
|
||||
def on_mount(self) -> None:
|
||||
self.query_one(FocusableWidgetWithNoBindingsNoInherit).focus()
|
||||
|
||||
|
||||
class AppWithScreenWithBindingsWidgetNoBindingsNoInherit(AppKeyRecorder):
|
||||
"""An app with a non-default screen that handles movement key bindings, child no-inherit."""
|
||||
|
||||
SCREENS = {"main":ScreenWithMovementBindingsNoInheritChild}
|
||||
SCREENS = {"main": ScreenWithMovementBindingsNoInheritChild}
|
||||
|
||||
def on_mount(self) -> None:
|
||||
self.push_screen("main")
|
||||
|
||||
|
||||
async def test_focused_child_widget_no_inherit_with_movement_bindings_on_screen() -> None:
|
||||
"""A focused child widget, that doesn't inherit bindings, with movement bindings in the screen, should trigger screen actions."""
|
||||
async with AppWithScreenWithBindingsWidgetNoBindingsNoInherit().run_test() as pilot:
|
||||
await pilot.press(*AppKeyRecorder.ALL_KEYS)
|
||||
await pilot.pause(2/100)
|
||||
await pilot.pause(2 / 100)
|
||||
pilot.app.all_recorded("screenly_")
|
||||
|
||||
|
||||
##############################################################################
|
||||
# A focused widget with zero bindings declared, but no inheriting of
|
||||
# bindings, on screen.
|
||||
@@ -449,10 +482,15 @@ async def test_focused_child_widget_no_inherit_with_movement_bindings_on_screen(
|
||||
# NOTE: zero bindings are declared for the widget, which is different from
|
||||
# no bindings declared.
|
||||
|
||||
class FocusableWidgetWithEmptyBindingsNoInherit(Static, can_focus=True, inherit_bindings=False):
|
||||
|
||||
class FocusableWidgetWithEmptyBindingsNoInherit(
|
||||
Static, can_focus=True, inherit_bindings=False
|
||||
):
|
||||
"""A widget that can receive focus but has empty bindings and doesn't inherit bindings."""
|
||||
|
||||
BINDINGS = []
|
||||
|
||||
|
||||
class ScreenWithMovementBindingsNoInheritEmptyChild(Screen):
|
||||
"""A screen that binds keys, including movement keys."""
|
||||
|
||||
@@ -468,17 +506,19 @@ class ScreenWithMovementBindingsNoInheritEmptyChild(Screen):
|
||||
def on_mount(self) -> None:
|
||||
self.query_one(FocusableWidgetWithEmptyBindingsNoInherit).focus()
|
||||
|
||||
|
||||
class AppWithScreenWithBindingsWidgetEmptyBindingsNoInherit(AppKeyRecorder):
|
||||
"""An app with a non-default screen that handles movement key bindings, child no-inherit."""
|
||||
|
||||
SCREENS = {"main":ScreenWithMovementBindingsNoInheritEmptyChild}
|
||||
SCREENS = {"main": ScreenWithMovementBindingsNoInheritEmptyChild}
|
||||
|
||||
def on_mount(self) -> None:
|
||||
self.push_screen("main")
|
||||
|
||||
|
||||
async def test_focused_child_widget_no_inherit_empty_bindings_with_movement_bindings_on_screen() -> None:
|
||||
"""A focused child widget, that doesn't inherit bindings and sets BINDINGS empty, with movement bindings in the screen, should trigger screen actions."""
|
||||
async with AppWithScreenWithBindingsWidgetEmptyBindingsNoInherit().run_test() as pilot:
|
||||
await pilot.press(*AppKeyRecorder.ALL_KEYS)
|
||||
await pilot.pause(2/100)
|
||||
await pilot.pause(2 / 100)
|
||||
pilot.app.all_recorded("screenly_")
|
||||
|
||||
Reference in New Issue
Block a user