File: test_input_replace.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 (64 lines) | stat: -rw-r--r-- 2,197 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
from __future__ import annotations

import pytest

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

_FORWARD_DELETIONS = [
    ((0, 0), "0123456789"),  # Delete nothing
    ((0, 10), ""),  # Delete all
    ((0, 1), "123456789"),  # Delete first character
    ((5, 10), "01234"),  # Delete last 5 characters
    ((3, 6), "0126789"),  # Delete middle 3 characters
    ((5, 20), "01234"),  # Delete past end results in clamp
]
_REVERSE_DELETIONS = [
    ((end, start), value_after) for (start, end), value_after in _FORWARD_DELETIONS
]
DELETIONS = _FORWARD_DELETIONS + _REVERSE_DELETIONS
"""The same deletions performed in both forward and reverse directions."""


@pytest.mark.parametrize("selection,value_after", DELETIONS)
async def test_input_delete(selection: tuple[int, int], value_after: str):
    class InputApp(App[None]):
        TEST_TEXT = "0123456789"

        def compose(self) -> ComposeResult:
            yield Input(self.TEST_TEXT)

    async with InputApp().run_test() as pilot:
        app = pilot.app
        input = app.query_one(Input)
        input.delete(*selection)
        assert input.value == value_after


_FORWARD_REPLACEMENTS = [
    ((0, 0), "", "0123456789"),
    ((0, 1), "X", "X123456789"),  # Replace first character with "X"
    ((5, 10), "X", "01234X"),  # Replace last 5 characters with "X"
    ((3, 6), "X", "012X6789"),  # Replace middle 3 characters with "X"
    ((5, 20), "X", "01234X"),  # Replace past end results in clamp
]
_REVERSE_REPLACEMENTS = [
    ((end, start), replacement, result)
    for (start, end), replacement, result in _FORWARD_REPLACEMENTS
]
REPLACEMENTS = _FORWARD_REPLACEMENTS + _REVERSE_REPLACEMENTS


@pytest.mark.parametrize("selection,replacement,result", REPLACEMENTS)
async def test_input_replace(selection: tuple[int, int], replacement: str, result: str):
    class InputApp(App[None]):
        TEST_TEXT = "0123456789"

        def compose(self) -> ComposeResult:
            yield Input(self.TEST_TEXT)

    async with InputApp().run_test() as pilot:
        app = pilot.app
        input = app.query_one(Input)
        input.replace(replacement, *selection)
        assert input.value == result