File: test_input_key_modification_actions.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 (181 lines) | stat: -rw-r--r-- 7,229 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
"""Unit tests for Input widget value modification actions."""

from __future__ import annotations

from textual.app import App, ComposeResult
from textual.widgets import Input

TEST_INPUTS: dict[str | None, str] = {
    "empty": "",
    "multi-no-punctuation": "Curse your sudden but inevitable betrayal",
    "multi-punctuation": "We have done the impossible, and that makes us mighty.",
    "multi-and-hyphenated": "Long as she does it quiet-like",
}


class InputTester(App[None]):
    """Input widget testing app."""

    def compose(self) -> ComposeResult:
        for input_id, value in TEST_INPUTS.items():
            yield Input(value, id=input_id)


async def test_delete_left_from_home() -> None:
    """Deleting left from home should do nothing."""
    async with InputTester().run_test() as pilot:
        for input in pilot.app.query(Input):
            input.cursor_position = 0
            input.action_delete_left()
            assert input.cursor_position == 0
            assert input.value == TEST_INPUTS[input.id]


async def test_delete_left_from_end() -> None:
    """Deleting left from end should remove the last character (if there is one)."""
    async with InputTester().run_test() as pilot:
        for input in pilot.app.query(Input):
            input.action_end()
            input.action_delete_left()
            assert input.cursor_position == len(input.value)
            assert input.value == TEST_INPUTS[input.id][:-1]


async def test_delete_left_word_from_home() -> None:
    """Deleting word left from home should do nothing."""
    async with InputTester().run_test() as pilot:
        for input in pilot.app.query(Input):
            input.cursor_position = 0
            input.action_delete_left_word()
            assert input.cursor_position == 0
            assert input.value == TEST_INPUTS[input.id]


async def test_delete_left_word_from_inside_first_word() -> None:
    async with InputTester().run_test() as pilot:
        for input in pilot.app.query(Input):
            input.cursor_position = 1
            input.action_delete_left_word()
            assert input.cursor_position == 0
            assert input.value == TEST_INPUTS[input.id][1:]


async def test_delete_left_word_from_end() -> None:
    """Deleting word left from end should remove the expected text."""
    async with InputTester().run_test() as pilot:
        expected: dict[str | None, str] = {
            "empty": "",
            "multi-no-punctuation": "Curse your sudden but inevitable ",
            "multi-punctuation": "We have done the impossible, and that makes us ",
            "multi-and-hyphenated": "Long as she does it quiet-",
        }
        for input in pilot.app.query(Input):
            input.action_delete_left_word()
            assert input.cursor_position == len(input.value)
            assert input.value == expected[input.id]


async def test_password_delete_left_word_from_end() -> None:
    """Deleting word left from end of a password input should delete everything."""
    async with InputTester().run_test() as pilot:
        for input in pilot.app.query(Input):
            input.password = True
            input.action_delete_left_word()
            assert input.cursor_position == 0
            assert input.value == ""


async def test_delete_left_all_from_home() -> None:
    """Deleting all left from home should do nothing."""
    async with InputTester().run_test() as pilot:
        for input in pilot.app.query(Input):
            input.cursor_position = 0
            input.action_delete_left_all()
            assert input.cursor_position == 0
            assert input.value == TEST_INPUTS[input.id]


async def test_delete_left_all_from_end() -> None:
    """Deleting all left from end should empty the input value."""
    async with InputTester().run_test() as pilot:
        for input in pilot.app.query(Input):
            input.action_end()
            input.action_delete_left_all()
            assert input.cursor_position == 0
            assert input.value == ""


async def test_delete_right_from_home() -> None:
    """Deleting right from home should delete one character (if there is any to delete)."""
    async with InputTester().run_test() as pilot:
        for input in pilot.app.query(Input):
            input.cursor_position = 0
            input.action_delete_right()
            assert input.cursor_position == 0
            assert input.value == TEST_INPUTS[input.id][1:]


async def test_delete_right_from_end() -> None:
    """Deleting right from end should not change the input's value."""
    async with InputTester().run_test() as pilot:
        for input in pilot.app.query(Input):
            input.action_delete_right()
            assert input.cursor_position == len(input.value)
            assert input.value == TEST_INPUTS[input.id]


async def test_delete_right_word_from_home() -> None:
    """Deleting word right from home should delete one word (if there is one)."""
    async with InputTester().run_test() as pilot:
        expected: dict[str | None, str] = {
            "empty": "",
            "multi-no-punctuation": "your sudden but inevitable betrayal",
            "multi-punctuation": "have done the impossible, and that makes us mighty.",
            "multi-and-hyphenated": "as she does it quiet-like",
        }
        for input in pilot.app.query(Input):
            input.cursor_position = 0
            input.action_delete_right_word()
            assert input.cursor_position == 0
            assert input.value == expected[input.id]


async def test_password_delete_right_word_from_home() -> None:
    """Deleting word right from home of a password input should delete everything."""
    async with InputTester().run_test() as pilot:
        for input in pilot.app.query(Input):
            input.cursor_position = 0
            input.password = True
            input.action_delete_right_word()
            assert input.cursor_position == 0
            assert input.value == ""


async def test_delete_right_word_from_end() -> None:
    """Deleting word right from end should not change the input's value."""
    async with InputTester().run_test() as pilot:
        for input in pilot.app.query(Input):
            input.action_end()
            input.action_delete_right_word()
            assert input.cursor_position == len(input.value)
            assert input.value == TEST_INPUTS[input.id]


async def test_delete_right_all_from_home() -> None:
    """Deleting all right home should remove everything in the input."""
    async with InputTester().run_test() as pilot:
        for input in pilot.app.query(Input):
            input.cursor_position = 0
            input.action_delete_right_all()
            assert input.cursor_position == 0
            assert input.value == ""


async def test_delete_right_all_from_end() -> None:
    """Deleting all right from end should not change the input's value."""
    async with InputTester().run_test() as pilot:
        for input in pilot.app.query(Input):
            input.action_end()
            input.action_delete_right_all()
            assert input.cursor_position == len(input.value)
            assert input.value == TEST_INPUTS[input.id]