File: test_tmuxobject.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 (197 lines) | stat: -rw-r--r-- 6,565 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
190
191
192
193
194
195
196
197
"""Tests for libtmux TmuxRelationalObject and TmuxMappingObject."""

from __future__ import annotations

import logging
import typing as t

from libtmux.pane import Pane
from libtmux.session import Session
from libtmux.test.constants import TEST_SESSION_PREFIX
from libtmux.test.random import namer
from libtmux.window import Window

if t.TYPE_CHECKING:
    from libtmux.server import Server

logger = logging.getLogger(__name__)


"""Test the :class:`TmuxRelationalObject` base class object."""


def test_find_where(server: Server, session: Session) -> None:
    """Test that find_where() retrieves single matching object."""
    # server.find_where
    for session in server.sessions:
        session_id = session.session_id
        assert session_id is not None

        assert server.sessions.get(session_id=session_id) == session
        assert isinstance(server.sessions.filter(session_id=session_id)[0], Session)

        # session.find_where
        for window in session.windows:
            window_id = window.window_id
            assert window_id is not None

            assert session.windows.get(window_id=window_id) == window
            assert isinstance(session.windows.get(window_id=window_id), Window)

            # window.find_where
            for pane in window.panes:
                pane_id = pane.pane_id
                assert pane_id is not None

                assert window.panes.get(pane_id=pane_id) == pane
                assert isinstance(window.panes.get(pane_id=pane_id), Pane)


def test_find_where_None(server: Server, session: Session) -> None:
    """.find_where returns None if no results found."""
    while True:
        nonexistent_session = TEST_SESSION_PREFIX + next(namer)

        if not server.has_session(nonexistent_session):
            break

    assert len(server.sessions.filter(session_name=nonexistent_session)) == 0


def test_find_where_multiple_infos(server: Server, session: Session) -> None:
    """.find_where returns objects with multiple attributes."""
    for session in server.sessions:
        session_id = session.session_id
        assert session_id is not None
        session_name = session.session_name
        assert session_name is not None

        find_where = server.sessions.get(
            session_id=session_id,
            session_name=session_name,
        )

        assert find_where == session
        assert isinstance(find_where, Session)

        # session.find_where
        for window in session.windows:
            window_id = window.window_id
            assert window_id is not None
            window_index = window.window_index
            assert window_index is not None

            find_window_where = session.windows.get(
                window_id=window_id,
                window_index=window_index,
            )

            assert find_window_where == window
            assert isinstance(find_window_where, Window)

            # window.find_where
            for pane in window.panes:
                pane_id = pane.pane_id
                assert pane_id is not None
                pane_tty = pane.pane_tty
                assert pane_tty is not None

                find_pane_where = window.panes.get(pane_id=pane_id, pane_tty=pane_tty)

                assert find_pane_where == pane
                assert isinstance(find_pane_where, Pane)


def test_where(server: Server, session: Session) -> None:
    """Test self.where() returns matching objects."""
    window = session.active_window
    window.split()  # create second pane

    for session in server.sessions:
        session_id = session.session_id
        assert session_id is not None
        session_name = session.session_name
        assert session_name is not None

        server_sessions = server.sessions.filter(
            session_id=session_id,
            session_name=session_name,
        )

        assert len(server_sessions) == 1
        assert isinstance(server_sessions, list)
        assert server_sessions[0] == session
        assert isinstance(server_sessions[0], Session)

        # session.where
        for window in session.windows:
            window_id = window.window_id
            assert window_id is not None

            window_index = window.window_index
            assert window_index is not None

            session_windows = session.windows.filter(
                window_id=window_id,
                window_index=window_index,
            )

            assert len(session_windows) == 1
            assert isinstance(session_windows, list)
            assert session_windows[0] == window
            assert isinstance(session_windows[0], Window)

            # window.where
            for pane in window.panes:
                pane_id = pane.pane_id
                assert pane_id is not None

                pane_tty = pane.pane_tty
                assert pane_tty is not None

                window_panes = window.panes.filter(pane_id=pane_id, pane_tty=pane_tty)

                assert len(window_panes) == 1
                assert isinstance(window_panes, list)
                assert window_panes[0] == pane
                assert isinstance(window_panes[0], Pane)


def test_filter(server: Server) -> None:
    """Test self.filter() retrieves child object."""
    sess = server.new_session("test")
    window = sess.active_window

    window.split()  # create second pane

    for session in server.sessions:
        session_id = session.session_id
        assert session_id is not None
        get_session_by_id = server.sessions.get(session_id=session_id)

        assert get_session_by_id == session
        assert isinstance(get_session_by_id, Session)
        assert len(server.windows.filter(window_id="$" + next(namer))) == 0

        # session.filter
        for window in session.windows:
            window_id = window.window_id
            assert window_id is not None

            get_window_by_id = session.windows.get(window_id=window_id)

            assert get_window_by_id == window
            assert isinstance(get_window_by_id, Window)

            assert len(session.windows.filter(window_id="@" + next(namer))) == 0

            # window.filter
            for pane in window.panes:
                pane_id = pane.pane_id
                assert pane_id is not None

                get_pane_by_id = window.panes.get(pane_id=pane_id)

                assert get_pane_by_id == pane
                assert isinstance(get_pane_by_id, Pane)
                assert len(window.panes.filter(pane_id="%" + next(namer))) == 0