File: test_tree_cursor.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 (102 lines) | stat: -rw-r--r-- 3,614 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
101
102
from __future__ import annotations

from typing import Any

from textual import on
from textual.app import App, ComposeResult
from textual.widgets import Tree
from textual.widgets.tree import NodeID, TreeNode


class TreeApp(App[None]):
    def __init__(self, *args: Any, **kwargs: Any) -> None:
        super().__init__(*args, **kwargs)
        self.messages: list[tuple[str, NodeID]] = []

    def compose(self) -> ComposeResult:
        tree = Tree[str](label="tree")
        self._node = tree.root.add_leaf("leaf")
        tree.root.expand()
        yield tree

    @property
    def node(self) -> TreeNode[str]:
        return self._node

    @on(Tree.NodeHighlighted)
    @on(Tree.NodeSelected)
    @on(Tree.NodeCollapsed)
    @on(Tree.NodeExpanded)
    def record_event(
        self,
        event: (
            Tree.NodeHighlighted[str]
            | Tree.NodeSelected[str]
            | Tree.NodeCollapsed[str]
            | Tree.NodeExpanded[str]
        ),
    ) -> None:
        self.messages.append((event.__class__.__name__, event.node.id))


async def test_move_cursor() -> None:
    """Test moving the cursor to a node (updating the highlighted node)."""
    async with TreeApp().run_test() as pilot:
        app = pilot.app
        tree: Tree[str] = app.query_one(Tree)
        node_to_move_to = app.node
        tree.move_cursor(node_to_move_to)
        await pilot.pause()

        # Note there are no Selected messages. We only move the cursor.
        assert app.messages == [
            ("NodeExpanded", 0),  # From the call to `tree.root.expand()` in compose
            ("NodeHighlighted", 0),  # From the initial highlight of the root node
            ("NodeHighlighted", 1),  # From the call to `tree.move_cursor`
        ]


async def test_move_cursor_reset() -> None:
    async with TreeApp().run_test() as pilot:
        app = pilot.app
        tree: Tree[str] = app.query_one(Tree)
        tree.move_cursor(app.node)
        tree.move_cursor(None)
        await pilot.pause()
        assert app.messages == [
            ("NodeExpanded", 0),  # From the call to `tree.root.expand()` in compose
            ("NodeHighlighted", 0),  # From the initial highlight of the root node
            ("NodeHighlighted", 1),  # From the 1st call to `tree.move_cursor`
            ("NodeHighlighted", 0),  # From the call to `tree.move_cursor(None)`
        ]


async def test_select_node() -> None:
    async with TreeApp().run_test() as pilot:
        app = pilot.app
        tree: Tree[str] = app.query_one(Tree)
        tree.select_node(app.node)
        await pilot.pause()
        assert app.messages == [
            ("NodeExpanded", 0),  # From the call to `tree.root.expand()` in compose
            ("NodeHighlighted", 0),  # From the initial highlight of the root node
            ("NodeHighlighted", 1),  # From the `tree.select_node` call
            ("NodeSelected", 1),  # From the call to `tree.select_node`
        ]


async def test_select_node_reset() -> None:
    async with TreeApp().run_test() as pilot:
        app = pilot.app
        tree: Tree[str] = app.query_one(Tree)
        tree.move_cursor(app.node)
        tree.select_node(None)
        await pilot.pause()

        # Notice no Selected messages.
        assert app.messages == [
            ("NodeExpanded", 0),  # From the call to `tree.root.expand()` in compose
            ("NodeHighlighted", 0),  # From the initial highlight of the root node
            ("NodeHighlighted", 1),  # From the `tree.move_cursor` call
            ("NodeHighlighted", 0),  # From the call to `tree.select_node(None)`
        ]