File: test_command_source_environment.py

package info (click to toggle)
textual 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,080 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (40 lines) | stat: -rw-r--r-- 1,247 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
from __future__ import annotations

from textual.app import App, ComposeResult
from textual.command import Hit, Hits, Provider
from textual.screen import Screen
from textual.widget import Widget
from textual.widgets import Input


class SimpleSource(Provider):
    environment: set[tuple[App, Screen, Widget | None]] = set()

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

        SimpleSource.environment.add((self.app, self.screen, self.focused))
        yield Hit(1, "Hit", goes_nowhere_does_nothing, "Hit")


class CommandPaletteApp(App[None]):
    COMMANDS = {SimpleSource}

    def compose(self) -> ComposeResult:
        yield Input()

    def on_mount(self) -> None:
        self.action_command_palette()


async def test_command_source_environment() -> None:
    """The command source should see the app and default screen."""
    async with CommandPaletteApp().run_test() as pilot:
        base_screen, *_ = pilot.app.children
        assert base_screen is not None
        await pilot.press(*"test")
        assert len(SimpleSource.environment) == 1
        assert SimpleSource.environment == {
            (pilot.app, base_screen, base_screen.query_one(Input))
        }