File: test_wrapped_document.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 (204 lines) | stat: -rw-r--r-- 5,879 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
198
199
200
201
202
203
204
import pytest

from textual.document._document import Document
from textual.document._wrapped_document import WrappedDocument
from textual.geometry import Offset

SIMPLE_TEXT = "123 4567\n12345\n123456789\n"


def test_wrap():
    document = Document(SIMPLE_TEXT)
    wrapped_document = WrappedDocument(document, width=4)

    assert wrapped_document.lines == [
        ["123 ", "4567"],
        ["1234", "5"],
        ["1234", "5678", "9"],
        [""],
    ]


def test_wrap_empty_document():
    document = Document("")
    wrapped_document = WrappedDocument(document, width=4)

    assert wrapped_document.lines == [[""]]


def test_wrap_width_zero_no_wrapping():
    document = Document(SIMPLE_TEXT)
    wrapped_document = WrappedDocument(document, width=0)

    assert wrapped_document.lines == [
        ["123 4567"],
        ["12345"],
        ["123456789"],
        [""],
    ]


def test_refresh_range():
    """The post-edit content is not wrapped."""
    document = Document(SIMPLE_TEXT)
    wrapped_document = WrappedDocument(document, width=4)

    start_location = (1, 0)
    old_end_location = (3, 0)

    edit_result = document.replace_range(start_location, old_end_location, "123")

    # Inform the wrapped document about the range impacted by the edit
    wrapped_document.wrap_range(
        start_location,
        old_end_location,
        edit_result.end_location,
    )

    # Now confirm the resulting wrapped version is as we would expect
    assert wrapped_document.lines == [["123 ", "4567"], ["123"]]


def test_refresh_range_new_text_wrapped():
    """The post-edit content itself must be wrapped."""
    document = Document(SIMPLE_TEXT)
    wrapped_document = WrappedDocument(document, width=4)

    start_location = (1, 0)
    old_end_location = (3, 0)

    edit_result = document.replace_range(
        start_location, old_end_location, "12 34567 8901"
    )

    # Inform the wrapped document about the range impacted by the edit
    wrapped_document.wrap_range(
        start_location, old_end_location, edit_result.end_location
    )

    # Now confirm the resulting wrapped version is as we would expect
    assert wrapped_document.lines == [
        ["123 ", "4567"],
        ["12 ", "3456", "7 ", "8901"],
    ]


def test_refresh_range_wrapping_at_previously_unavailable_range():
    """When we insert new content at the end of the document, ensure it wraps correctly."""
    document = Document(SIMPLE_TEXT)
    wrapped_document = WrappedDocument(document, width=4)

    edit_result = document.replace_range((3, 0), (3, 0), "012 3456\n78 90123\n45")
    wrapped_document.wrap_range((3, 0), (3, 0), edit_result.end_location)

    assert wrapped_document.lines == [
        ["123 ", "4567"],
        ["1234", "5"],
        ["1234", "5678", "9"],
        ["012 ", "3456"],
        ["78 ", "9012", "3"],
        ["45"],
    ]


def test_refresh_range_wrapping_disabled_previously_unavailable_range():
    document = Document(SIMPLE_TEXT)
    wrapped_document = WrappedDocument(document, width=0)  # wrapping disabled

    edit_result = document.replace_range((3, 0), (3, 0), "012 3456\n78 90123\n45")
    wrapped_document.wrap_range((3, 0), (3, 0), edit_result.end_location)

    assert wrapped_document.lines == [
        ["123 4567"],
        ["12345"],
        ["123456789"],
        ["012 3456"],
        ["78 90123"],
        ["45"],
    ]


@pytest.mark.parametrize(
    "offset,location",  # location is (row, column)
    [
        (Offset(x=0, y=0), (0, 0)),
        (Offset(x=1, y=0), (0, 1)),
        (Offset(x=2, y=1), (0, 6)),
        (Offset(x=0, y=3), (1, 4)),
        (Offset(x=1, y=3), (1, 5)),
        (Offset(x=200, y=3), (1, 5)),
        (Offset(x=0, y=6), (2, 8)),
        (Offset(x=0, y=7), (3, 0)),  # Clicking on the final, empty line
        (Offset(x=0, y=1000), (3, 0)),
    ],
)
def test_offset_to_location_wrapping_enabled(offset, location):
    document = Document(SIMPLE_TEXT)
    wrapped_document = WrappedDocument(document, width=4)

    assert wrapped_document.offset_to_location(offset) == location


@pytest.mark.parametrize(
    "offset,location",  # location is (row, column)
    [
        (Offset(x=0, y=0), (0, 0)),
        (Offset(x=1, y=0), (0, 1)),
        (Offset(x=2, y=1), (1, 2)),
        (Offset(x=0, y=3), (3, 0)),
        (Offset(x=1, y=3), (3, 0)),
        (Offset(x=200, y=3), (3, 0)),
        (Offset(x=200, y=200), (3, 0)),  # Clicking below the document
    ],
)
def test_offset_to_location_wrapping_disabled(offset, location):
    document = Document(SIMPLE_TEXT)
    wrapped_document = WrappedDocument(document, width=0)

    assert wrapped_document.offset_to_location(offset) == location


@pytest.mark.parametrize(
    "offset,location",
    [
        [Offset(-3, 0), (0, 0)],
        [Offset(0, -10), (0, 0)],
    ],
)
def test_offset_to_location_invalid_offset_clamps_to_valid_offset(offset, location):
    document = Document(SIMPLE_TEXT)
    wrapped_document = WrappedDocument(document, width=4)

    result = wrapped_document.offset_to_location(offset)
    assert result == location


@pytest.mark.parametrize(
    "line_index, offsets",
    [
        (0, [4]),
        (1, [4]),
        (2, [4, 8]),
    ],
)
def test_get_offsets(line_index, offsets):
    document = Document(SIMPLE_TEXT)
    wrapped_document = WrappedDocument(document, width=4)

    assert wrapped_document.get_offsets(line_index) == offsets


def test_get_offsets_no_wrapping():
    document = Document("abc")
    wrapped_document = WrappedDocument(document, width=4)

    assert wrapped_document.get_offsets(0) == []


@pytest.mark.parametrize("line_index", [-4, 10000])
def test_get_offsets_invalid_line_index(line_index):
    document = Document(SIMPLE_TEXT)
    wrapped_document = WrappedDocument(document, width=4)

    with pytest.raises(ValueError):
        wrapped_document.get_offsets(line_index)