From 6b77e735587bc0e515c17f0483f3e300829fc268 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Mon, 12 Dec 2022 20:49:19 +0000 Subject: [PATCH] Start unit tests for live key bindings plus inheriting Up until now there doesn't seem to have been any unit tests aimed squarely at setting up bindings, as part of a running application, which are only about testing the bindings. As such there was no way of slotting in tests for how inheritance of bindings works. This starts that process with a view to testing how inheriting likely *should* work. See #1343 for some background to this. --- tests/test_binding_inheritance.py | 77 +++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tests/test_binding_inheritance.py diff --git a/tests/test_binding_inheritance.py b/tests/test_binding_inheritance.py new file mode 100644 index 000000000..b7b892f7f --- /dev/null +++ b/tests/test_binding_inheritance.py @@ -0,0 +1,77 @@ +import pytest + +from textual.app import App, ComposeResult +from textual.widgets import Static +from textual.screen import Screen +from textual.binding import Binding + +############################################################################## +class NoBindings(App[None]): + """An app with zero bindings.""" + + +async def test_just_app_no_bindings() -> None: + """An app with no bindings should have no bindings, other than ctrl+c.""" + async with NoBindings().run_test() as pilot: + assert list(pilot.app._bindings.keys.keys()) == ["ctrl+c"] + + +############################################################################## +class AlphaBinding(App[None]): + """An app with a simple alpha key binding.""" + + BINDINGS = [Binding("a", "a", "a")] + + +async def test_just_app_alpha_binding() -> None: + """An app with a single binding should have just the one binding.""" + async with AlphaBinding().run_test() as pilot: + assert sorted(pilot.app._bindings.keys.keys()) == sorted(["ctrl+c", "a"]) + + +############################################################################## +class ScreenNoBindings(Screen): + """A screen with no added bindings.""" + + +class AppWithScreenNoBindings(App[None]): + """An app with no extra bindings but with a custom screen.""" + + SCREENS = {"main": ScreenNoBindings} + + def on_mount(self) -> None: + self.push_screen("main") + + +@pytest.mark.xfail( + reason="Screen is incorrectly starting with bindings for movement keys [issue#1343]" +) +async def test_app_screen_no_bindings() -> None: + """An screen with no bindings should have no bindings.""" + async with AppWithScreenNoBindings().run_test() as pilot: + assert list(pilot.app.screen._bindings.keys.keys()) == [] + + +############################################################################## +class ScreenWithBindings(Screen): + """A screen with a simple alpha key binding.""" + + BINDINGS = [Binding("a", "a", "a")] + + +class AppWithScreenThatHasABinding(App[None]): + """An app with no extra bindings but with a custom screen with a binding.""" + + SCREENS = {"main": ScreenWithBindings} + + def on_mount(self) -> None: + self.push_screen("main") + + +@pytest.mark.xfail( + reason="Screen is incorrectly starting with bindings for movement keys [issue#1343]" +) +async def test_app_screen_with_bindings() -> None: + """An app with a screen and a binding should only have ctrl+c as a binding.""" + async with AppWithScreenThatHasABinding().run_test() as pilot: + assert list(pilot.app.screen._bindings.keys.keys()) == ["a"]