File: test_run_on_select.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 (67 lines) | stat: -rw-r--r-- 2,390 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
from functools import partial

from textual.app import App
from textual.command import CommandPalette, Hit, Hits, Provider
from textual.widgets import Input


class SimpleSource(Provider):
    async def search(self, _: str) -> Hits:
        def goes_nowhere_does_nothing(selection: int) -> None:
            assert isinstance(self.app, CommandPaletteRunOnSelectApp)
            self.app.selection = selection

        for n in range(100):
            yield Hit(
                n + 1 / 100,
                str(n),
                partial(goes_nowhere_does_nothing, n),
                str(n),
                f"This is help for {n}",
            )


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

    def __init__(self) -> None:
        super().__init__()
        self.selection: int | None = None


async def test_with_run_on_select_on() -> None:
    """With run on select on, the callable should be instantly run."""
    async with CommandPaletteRunOnSelectApp().run_test() as pilot:
        save = CommandPalette.run_on_select
        CommandPalette.run_on_select = True
        assert isinstance(pilot.app, CommandPaletteRunOnSelectApp)
        pilot.app.action_command_palette()
        await pilot.press("0")
        await pilot.app.screen.workers.wait_for_complete()
        await pilot.press("down")
        await pilot.press("enter")
        assert pilot.app.selection is not None
        CommandPalette.run_on_select = save


class CommandPaletteDoNotRunOnSelectApp(CommandPaletteRunOnSelectApp):
    def __init__(self) -> None:
        super().__init__()


async def test_with_run_on_select_off() -> None:
    """With run on select off, the callable should not be instantly run."""
    async with CommandPaletteDoNotRunOnSelectApp().run_test() as pilot:
        save = CommandPalette.run_on_select
        CommandPalette.run_on_select = False
        assert isinstance(pilot.app, CommandPaletteDoNotRunOnSelectApp)
        pilot.app.action_command_palette()
        await pilot.press("0")
        await pilot.app.screen.workers.wait_for_complete()
        await pilot.press("down")
        await pilot.press("enter")
        assert pilot.app.selection is None
        assert pilot.app.screen.query_one(Input).value != ""
        await pilot.press("enter")
        assert pilot.app.selection is not None
        CommandPalette.run_on_select = save