Skip to content

Commit af049d4

Browse files
committed
added test
1 parent bb499f6 commit af049d4

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/textual/app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,11 +1386,10 @@ async def invoke_ready_callback() -> None:
13861386
raise
13871387

13881388
finally:
1389+
self._running = True
13891390
await self._ready()
13901391
await invoke_ready_callback()
13911392

1392-
self._running = True
1393-
13941393
try:
13951394
await self._process_messages_loop()
13961395
except asyncio.CancelledError:

tests/test_binding_inheritance.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111

1212
from __future__ import annotations
1313

14-
import pytest
15-
1614
from textual.app import App, ComposeResult
17-
from textual.widgets import Static
18-
from textual.screen import Screen
1915
from textual.binding import Binding
2016
from textual.containers import Container
17+
from textual.screen import Screen
18+
from textual.widget import Widget
19+
from textual.widgets import Static
2120

2221
##############################################################################
2322
# These are the movement keys within Textual; they kind of have a special
@@ -574,6 +573,7 @@ def compose(self) -> ComposeResult:
574573
def on_mount(self) -> None:
575574
self.query_one(PriorityOverlapWidget).focus()
576575

576+
577577
class PriorityOverlapApp(AppKeyRecorder):
578578
"""An application with a priority binding."""
579579

@@ -607,3 +607,40 @@ async def test_overlapping_priority_bindings() -> None:
607607
"app_e",
608608
"screen_f",
609609
]
610+
611+
612+
async def test_skip_action() -> None:
613+
"""Test that a binding may be skipped by an action returning False"""
614+
615+
class Handle(Widget, can_focus=True):
616+
BINDINGS = [("t", "test('foo')", "Test")]
617+
618+
def action_test(self, text: str) -> None:
619+
self.app.exit(text)
620+
621+
no_handle_invoked = False
622+
623+
class NoHandle(Widget, can_focus=True):
624+
BINDINGS = [("t", "test('bar')", "Test")]
625+
626+
def action_test(self, text: str) -> bool:
627+
nonlocal no_handle_invoked
628+
no_handle_invoked = True
629+
return False
630+
631+
class SkipApp(App):
632+
def compose(self) -> ComposeResult:
633+
yield Handle(NoHandle())
634+
635+
def on_mount(self) -> None:
636+
self.query_one(NoHandle).focus()
637+
638+
async with SkipApp().run_test() as pilot:
639+
# Check the NoHandle widget has focus
640+
assert pilot.app.query_one(NoHandle).has_focus
641+
# Press the "t" key
642+
await pilot.press("t")
643+
# Check the action on the no handle widget was called
644+
assert no_handle_invoked
645+
# Check the return value, confirming that the action on Handle was called
646+
assert pilot.app.return_value == "foo"

0 commit comments

Comments
 (0)