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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
from typing import Union
import pytest
from textual import on
from textual.app import App, ComposeResult
from textual.validation import Failure, ValidationResult
from textual.widgets import MaskedInput
InputEvent = Union[MaskedInput.Changed, MaskedInput.Submitted]
class InputApp(App[None]):
def __init__(self, template: str, placeholder: str = ""):
super().__init__()
self.messages: list[InputEvent] = []
self.template = template
self.placeholder = placeholder
def compose(self) -> ComposeResult:
yield MaskedInput(
template=self.template, placeholder=self.placeholder, select_on_focus=False
)
@on(MaskedInput.Changed)
@on(MaskedInput.Submitted)
def on_changed_or_submitted(self, event: InputEvent) -> None:
self.messages.append(event)
async def test_missing_required():
app = InputApp(">9999-99-99")
async with app.run_test() as pilot:
input = app.query_one(MaskedInput)
input.value = "2024-12"
assert not input.is_valid
await pilot.pause()
assert len(app.messages) == 1
assert app.messages[0].validation_result == ValidationResult.failure(
failures=[
Failure(
value="2024-12",
validator=input._template,
description="Value does not match template!",
)
],
)
async def test_valid_required():
app = InputApp(">9999-99-99")
async with app.run_test() as pilot:
input = app.query_one(MaskedInput)
input.value = "2024-12-31"
assert input.is_valid
await pilot.pause()
assert len(app.messages) == 1
assert app.messages[0].validation_result == ValidationResult.success()
async def test_missing_optional():
app = InputApp(">9999-99-00")
async with app.run_test() as pilot:
input = app.query_one(MaskedInput)
input.value = "2024-12"
assert input.is_valid
await pilot.pause()
assert len(app.messages) == 1
assert app.messages[0].validation_result == ValidationResult.success()
async def test_editing():
serial = "ABCDE-FGHIJ-KLMNO-PQRST"
app = InputApp(">NNNNN-NNNNN-NNNNN-NNNNN;_")
async with app.run_test() as pilot:
input = app.query_one(MaskedInput)
await pilot.press("A", "B", "C", "D")
assert input.cursor_position == 4
assert input.value == "ABCD"
await pilot.press("E")
assert input.cursor_position == 6
assert input.value == "ABCDE-"
await pilot.press("backspace")
assert input.cursor_position == 4
assert input.value == "ABCD"
input.value = serial
input.action_end()
assert input.is_valid
app.set_focus(None)
input.focus()
await pilot.pause()
assert input.cursor_position == len(serial)
await pilot.press("U")
assert input.cursor_position == len(serial)
async def test_key_movement_actions():
serial = "ABCDE-FGHIJ-KLMNO-PQRST"
app = InputApp(">NNNNN-NNNNN-NNNNN-NNNNN;_")
async with app.run_test():
input = app.query_one(MaskedInput)
input.value = serial
input.action_home()
assert input.is_valid
input.action_cursor_right_word()
assert input.cursor_position == 6
input.action_cursor_right()
input.action_cursor_right_word()
assert input.cursor_position == 12
input.action_cursor_left()
input.action_cursor_left()
assert input.cursor_position == 9
input.action_cursor_left_word()
assert input.cursor_position == 6
async def test_key_modification_actions():
serial = "ABCDE-FGHIJ-KLMNO-PQRST"
app = InputApp(">NNNNN-NNNNN-NNNNN-NNNNN;_")
async with app.run_test() as pilot:
input = app.query_one(MaskedInput)
input.value = serial
assert input.is_valid
input.cursor_position = 0
input.action_delete_right()
assert input.value == " BCDE-FGHIJ-KLMNO-PQRST"
input.cursor_position = 3
input.action_delete_left()
assert input.value == " B DE-FGHIJ-KLMNO-PQRST"
input.cursor_position = 6
input.action_delete_left()
assert input.value == " B D -FGHIJ-KLMNO-PQRST"
input.cursor_position = 9
input.action_delete_left_word()
assert input.value == " B D - IJ-KLMNO-PQRST"
input.action_delete_left_word()
assert input.value == " - IJ-KLMNO-PQRST"
input.cursor_position = 15
input.action_delete_right_word()
assert input.value == " - IJ-KLM -PQRST"
input.action_delete_right_word()
assert input.value == " - IJ-KLM"
input.cursor_position = 10
input.action_delete_right_all()
assert input.value == " - I"
await pilot.press("J")
assert input.value == " - IJ-"
input.action_cursor_left()
input.action_delete_left_all()
assert input.value == " - J-"
input.clear()
assert input.value == ""
async def test_cursor_word_right_after_last_separator():
app = InputApp(">NNN-NNN-NNN-NNNNN;_")
async with app.run_test():
input = app.query_one(MaskedInput)
input.value = "123-456-789-012"
input.cursor_position = 13
input.action_cursor_right_word()
assert input.cursor_position == 15
async def test_case_conversion_meta_characters():
app = InputApp("NN<-N!N>N")
async with app.run_test() as pilot:
input = app.query_one(MaskedInput)
await pilot.press("a", "B", "C", "D", "e")
assert input.value == "aB-cDE"
assert input.is_valid
async def test_case_conversion_override():
app = InputApp(">-<NN")
async with app.run_test() as pilot:
input = app.query_one(MaskedInput)
await pilot.press("a", "B")
assert input.value == "-ab"
assert input.is_valid
async def test_case_conversion_cancel():
app = InputApp("-!N-")
async with app.run_test() as pilot:
input = app.query_one(MaskedInput)
await pilot.press("a")
assert input.value == "-a-"
assert input.is_valid
async def test_only_separators__raises_ValueError():
app = InputApp("---")
with pytest.raises(ValueError):
async with app.run_test() as pilot:
await pilot.press("a")
async def test_custom_separator_escaping():
app = InputApp("N\\aN\\N\\cN")
async with app.run_test() as pilot:
input = app.query_one(MaskedInput)
await pilot.press("D", "e", "F")
assert input.value == "DaeNcF"
assert input.is_valid
async def test_digits_not_required():
app = InputApp("00;_")
async with app.run_test() as pilot:
input = app.query_one(MaskedInput)
await pilot.press("a", "1")
assert input.value == "1"
assert input.is_valid
async def test_digits_required():
app = InputApp("99;_")
async with app.run_test() as pilot:
input = app.query_one(MaskedInput)
await pilot.press("a", "1")
assert input.value == "1"
assert not input.is_valid
|