File: test_widget_navigation.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 (154 lines) | stat: -rw-r--r-- 4,414 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
from __future__ import annotations

import pytest

from textual._widget_navigation import (
    find_first_enabled,
    find_last_enabled,
    find_next_enabled,
    find_next_enabled_no_wrap,
    get_directed_distance,
)


class _D:
    def __init__(self, disabled):
        self.disabled = disabled


# Represent disabled/enabled objects that are compact to write in tests.
D = _D(True)
E = _D(False)


@pytest.mark.parametrize(
    ["index", "start", "direction", "wrap_at", "dist"],
    [
        (2, 8, 1, 10, 4),
        (2, 8, -1, 10, 6),
        (8, 2, -1, 10, 4),
        (8, 2, 1, 10, 6),
        (8, 2, 1, 1234123512, 6),
        (2, 8, 1, 11, 5),
        (2, 8, 1, 12, 6),
        (5, 5, 1, 10, 0),
    ],
)
def test_distance(index, start, direction, wrap_at, dist):
    assert (
        get_directed_distance(
            index=index,
            start=start,
            direction=direction,
            wrap_at=wrap_at,
        )
        == dist
    )


@pytest.mark.parametrize(
    "function",
    [
        find_first_enabled,
        find_last_enabled,
    ],
)
def test_find_enabled_returns_none_on_empty(function):
    assert function([]) is None


@pytest.mark.parametrize(
    ["candidates", "anchor", "direction", "result"],
    [
        # No anchor & no candidates -> no next
        ([], None, 1, None),
        ([], None, -1, None),
        # No anchor but candidates -> get first/last one
        ([E], None, 1, 0),
        ([E, D], None, 1, 0),
        ([E, E], None, 1, 0),
        ([D, E], None, 1, 1),
        ([D, D, E], None, 1, 2),
        ([E], None, -1, 0),
        ([E, E], None, -1, 1),
        ([E, D], None, -1, 0),
        ([E, D, D], None, -1, 0),
        # No enabled candidates -> return the anchor
        ([D, D, D], 0, 1, 0),
        ([D, D, D], 1, 1, 1),
        ([D, D, D], 1, -1, 1),
        ([D, D, D], None, -1, None),
        # General case
        # 0  1  2  3  4  5
        ([E, D, D, E, E, D], 0, 1, 3),
        ([E, D, D, E, E, D], 0, -1, 4),
        ([E, D, D, E, E, D], 1, 1, 3),
        ([E, D, D, E, E, D], 1, -1, 0),
        ([E, D, D, E, E, D], 2, 1, 3),
        ([E, D, D, E, E, D], 2, -1, 0),
        ([E, D, D, E, E, D], 3, 1, 4),
        ([E, D, D, E, E, D], 3, -1, 0),
        ([E, D, D, E, E, D], 4, 1, 0),
        ([E, D, D, E, E, D], 4, -1, 3),
        ([E, D, D, E, E, D], 5, 1, 0),
        ([E, D, D, E, E, D], 5, -1, 4),
    ],
)
def test_find_next_enabled(candidates, anchor, direction, result):
    assert find_next_enabled(candidates, anchor, direction) == result


@pytest.mark.parametrize(
    ["candidates", "anchor", "direction", "result"],
    [
        # No anchor & no candidates -> no next
        ([], None, 1, None),
        ([], None, -1, None),
        # No anchor but candidates -> get first/last one
        ([E], None, 1, 0),
        ([E, D], None, 1, 0),
        ([E, E], None, 1, 0),
        ([D, E], None, 1, 1),
        ([D, D, E], None, 1, 2),
        ([E], None, -1, 0),
        ([E, E], None, -1, 1),
        ([E, D], None, -1, 0),
        ([E, D, D], None, -1, 0),
        # No enabled candidates -> return None
        ([D, D, D], 0, 1, None),
        ([D, D, D], 1, 1, None),
        ([D, D, D], 1, -1, None),
        ([D, D, D], None, -1, None),
        # General case
        # 0  1  2  3  4  5
        ([E, D, D, E, E, D], 0, 1, 3),
        ([E, D, D, E, E, D], 0, -1, None),
        ([E, D, D, E, E, D], 1, 1, 3),
        ([E, D, D, E, E, D], 1, -1, 0),
        ([E, D, D, E, E, D], 2, 1, 3),
        ([E, D, D, E, E, D], 2, -1, 0),
        ([E, D, D, E, E, D], 3, 1, 4),
        ([E, D, D, E, E, D], 3, -1, 0),
        ([E, D, D, E, E, D], 4, 1, None),
        ([E, D, D, E, E, D], 4, -1, 3),
        ([E, D, D, E, E, D], 5, 1, None),
        ([E, D, D, E, E, D], 5, -1, 4),
    ],
)
def test_find_next_enabled_no_wrap(candidates, anchor, direction, result):
    assert find_next_enabled_no_wrap(candidates, anchor, direction) == result


@pytest.mark.parametrize(
    ["function", "start", "direction"],
    [
        (find_next_enabled_no_wrap, 0, 1),
        (find_next_enabled_no_wrap, 0, -1),
        (find_next_enabled_no_wrap, 1, 1),
        (find_next_enabled_no_wrap, 1, -1),
        (find_next_enabled_no_wrap, 2, 1),
        (find_next_enabled_no_wrap, 2, -1),
    ],
)
def test_find_next_with_anchor(function, start, direction):
    assert function([E, E, E], start, direction, True) == start