File: test_dataclasses.py

package info (click to toggle)
python-libtmux 0.46.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,372 kB
  • sloc: python: 7,716; makefile: 199; sh: 21
file content (189 lines) | stat: -rw-r--r-- 5,120 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""Tests for libtmux object model, querying and traversal, etc."""

from __future__ import annotations

import typing as t

import pytest

from libtmux._internal.query_list import (
    MultipleObjectsReturned,
    ObjectDoesNotExist,
    QueryList,
)
from libtmux.constants import ResizeAdjustmentDirection
from libtmux.pane import Pane
from libtmux.session import Session
from libtmux.window import Window

if t.TYPE_CHECKING:
    import pathlib

    from libtmux.server import Server

    ListCmd = t.Literal["list-sessions", "list-windows", "list-panes"]
    ListExtraArgs = t.Optional[tuple[str]]


OutputRaw = dict[str, t.Any]
OutputsRaw = list[OutputRaw]


def test_pane(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: pathlib.Path,
    server: Server,
) -> None:
    """Verify Pane dataclass object."""
    monkeypatch.chdir(tmp_path)

    try:
        session_ = server.sessions[0]
    except Exception:
        session_ = server.new_session()

    assert session_ is not None

    window_ = session_.active_window
    window_.split()
    pane_ = window_.split()
    window_.select_layout("main-vertical")

    assert pane_ is not None
    assert pane_.pane_id is not None

    assert isinstance(pane_.pane_id, str)

    pane = Pane.from_pane_id(server=pane_.server, pane_id=pane_.pane_id)

    assert isinstance(pane, Pane)

    #
    # Refreshing
    #
    pane.refresh()

    old_pane_size = pane.pane_height

    pane.resize_pane(adjustment_direction=ResizeAdjustmentDirection.Down, adjustment=25)
    pane.resize_pane(
        adjustment_direction=ResizeAdjustmentDirection.Right,
        adjustment=25,
    )

    assert old_pane_size != pane.pane_height
    assert pane.pane_current_command is not None

    #
    # Relations
    #
    assert pane.window is not None
    assert isinstance(pane.window, Window)

    assert pane.window.session is not None
    assert isinstance(pane.window.session, Session)

    assert pane.session is not None
    assert isinstance(pane.session, Session)

    #
    # Relations: Child objects
    #
    assert isinstance(pane, Pane)
    assert isinstance(pane.session, Session)

    # Session
    assert pane.session.windows
    assert isinstance(pane.session.windows, list)
    assert len(pane.session.windows) > 0
    for w in pane.session.windows:
        assert isinstance(w, Window)
        assert isinstance(w.session, Session)
        assert w.session_id == pane.session.session_id

    assert len(pane.session.panes) > 0
    for _p in pane.session.panes:
        assert isinstance(_p, Pane)
        assert isinstance(_p.session, Session)
        assert _p.session_id == pane.session.session_id

    # Session -> QueryList
    assert len(pane.window.panes) > 0
    session = pane.session
    window = session.new_window(window_name="test")
    assert len(session.windows) > 1
    for _w in session.windows.filter(window_name=window.window_name):
        assert isinstance(_w, Window)
        assert isinstance(_w.session, Session)
        assert _w.window_name == window.window_name

    # Window
    assert len(pane.session.panes) > 0
    assert len(window.panes) > 0
    for _p in window.panes:
        assert isinstance(_p, Pane)
        assert isinstance(_p.session, Session)
        assert _p.window_id == window.window_id

    #
    # Split window
    #

    # Window-level
    new_pane = window.split()
    assert new_pane.pane_id != pane.pane_id
    assert new_pane.window_id == window.window_id

    # Pane-level
    new_pane_2 = new_pane.split()
    assert new_pane_2.pane_id != new_pane.pane_id
    assert new_pane_2.window_id == new_pane.window_id


@pytest.fixture
def session(session: Session) -> Session:
    """Verify creating Session with Session.from_session_id()."""
    assert session.session_id is not None
    return Session.from_session_id(server=session.server, session_id=session.session_id)


def test_querylist(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: pathlib.Path,
    session: Session,
) -> None:
    """Verify QueryList behavior with libtmux object."""
    monkeypatch.chdir(tmp_path)

    session.new_window(window_name="test_2")
    session.new_window(window_name="test_3")

    qs = QueryList(session.windows)

    assert qs.count(session.windows[0]) == 1
    assert len(qs) == 3

    for w in qs.filter():
        assert isinstance(w, Window)

    for w in qs.filter(window_name="test_2"):
        assert isinstance(w, Window)
        assert w.window_name == "test_2"

    w_2 = qs.get(window_name="test_2")
    assert isinstance(w_2, Window)
    assert w_2.window_name == "test_2"

    with pytest.raises(ObjectDoesNotExist):
        qs.get(window_name="non_existent")

    result = qs.get(window_name="non_existent", default="default_value")
    assert result == "default_value"

    # Test for multiple objects
    server = session.server
    second_session = server.new_session("second session")
    second_session.new_window(window_name="test_2")
    assert len(server.windows.filter(window_name="test_2")) == 2
    with pytest.raises(MultipleObjectsReturned):
        server.windows.get(window_name="test_2")