File: test_declare_sources.py

package info (click to toggle)
textual 2.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,084 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (100 lines) | stat: -rw-r--r-- 3,288 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from textual.app import App
from textual.command import CommandPalette, Hit, Hits, Provider
from textual.screen import Screen
from textual.system_commands import SystemCommandsProvider


async def test_sources_with_no_known_screen() -> None:
    """A command palette with no known screen should have an empty source set."""
    assert CommandPalette()._provider_classes == set()


class ExampleCommandSource(Provider):
    async def search(self, _: str) -> Hits:
        def goes_nowhere_does_nothing() -> None:
            pass

        yield Hit(1, "Hit", goes_nowhere_does_nothing, "Hit")


class AppWithActiveCommandPalette(App[None]):
    def on_mount(self) -> None:
        self.action_command_palette()


class AppWithNoSources(AppWithActiveCommandPalette):
    pass


async def test_no_app_command_sources() -> None:
    """An app with no sources declared should work fine."""
    async with AppWithNoSources().run_test() as pilot:
        assert isinstance(pilot.app.screen, CommandPalette)
        assert pilot.app.screen._provider_classes == {SystemCommandsProvider}


class AppWithSources(AppWithActiveCommandPalette):
    COMMANDS = {ExampleCommandSource}


async def test_app_command_sources() -> None:
    """Command sources declared on an app should be in the command palette."""
    async with AppWithSources().run_test() as pilot:
        assert isinstance(pilot.app.screen, CommandPalette)
        assert pilot.app.screen._provider_classes == {ExampleCommandSource}


class AppWithInitialScreen(App[None]):
    def __init__(self, screen: Screen) -> None:
        super().__init__()
        self._test_screen = screen

    def on_mount(self) -> None:
        self.push_screen(self._test_screen)


class ScreenWithNoSources(Screen[None]):
    def on_mount(self) -> None:
        self.app.action_command_palette()


async def test_no_screen_command_sources() -> None:
    """An app with a screen with no sources declared should work fine."""
    async with AppWithInitialScreen(ScreenWithNoSources()).run_test() as pilot:
        assert isinstance(pilot.app.screen, CommandPalette)
        assert pilot.app.screen._provider_classes == {SystemCommandsProvider}


class ScreenWithSources(ScreenWithNoSources):
    COMMANDS = {ExampleCommandSource}


async def test_screen_command_sources() -> None:
    """Command sources declared on a screen should be in the command palette."""
    async with AppWithInitialScreen(ScreenWithSources()).run_test() as pilot:
        assert isinstance(pilot.app.screen, CommandPalette)
        assert pilot.app.screen._provider_classes == {
            SystemCommandsProvider,
            ExampleCommandSource,
        }


class AnotherCommandSource(ExampleCommandSource):
    pass


class CombinedSourceApp(App[None]):
    COMMANDS = {AnotherCommandSource}

    def on_mount(self) -> None:
        self.push_screen(ScreenWithSources())


async def test_app_and_screen_command_sources_combine() -> None:
    """If an app and the screen have command sources they should combine."""
    async with CombinedSourceApp().run_test() as pilot:
        assert isinstance(pilot.app.screen, CommandPalette)
        assert (
            pilot.app.screen._provider_classes
            == CombinedSourceApp.COMMANDS | ScreenWithSources.COMMANDS
        )