File: test_segment_tools.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 (195 lines) | stat: -rw-r--r-- 6,131 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
from rich.segment import Segment
from rich.style import Style

from textual._segment_tools import align_lines, line_crop, line_pad, line_trim
from textual.geometry import Size


def test_line_crop():
    bold = Style(bold=True)
    italic = Style(italic=True)
    segments = [
        Segment("Hello", bold),
        Segment(" World!", italic),
    ]
    total = sum(segment.cell_length for segment in segments)

    assert line_crop(segments, 1, 2, total) == [Segment("e", bold)]
    assert line_crop(segments, 4, 20, total) == [
        Segment("o", bold),
        Segment(" World!", italic),
    ]


def test_line_crop_emoji():
    bold = Style(bold=True)
    italic = Style(italic=True)
    segments = [
        Segment("Hello", bold),
        Segment("💩💩💩", italic),
    ]
    total = sum(segment.cell_length for segment in segments)
    assert line_crop(segments, 8, 11, total) == [Segment(" 💩", italic)]
    assert line_crop(segments, 9, 11, total) == [Segment("💩", italic)]


def test_line_crop_edge():
    segments = [Segment("foo"), Segment("bar"), Segment("baz")]
    total = sum(segment.cell_length for segment in segments)

    assert line_crop(segments, 2, 9, total) == [
        Segment("o"),
        Segment("bar"),
        Segment("baz"),
    ]
    assert line_crop(segments, 3, 9, total) == [Segment("bar"), Segment("baz")]
    assert line_crop(segments, 4, 9, total) == [Segment("ar"), Segment("baz")]
    assert line_crop(segments, 4, 8, total) == [Segment("ar"), Segment("ba")]


def test_line_crop_edge_2():
    segments = [
        Segment("╭─"),
        Segment(
            "────── Placeholder ───────",
        ),
        Segment(
            "─╮",
        ),
    ]
    total = sum(segment.cell_length for segment in segments)
    result = line_crop(segments, 30, 60, total)
    expected = []
    print(repr(result))
    assert result == expected


def test_line_trim_ascii():
    segments = [Segment("foo")]

    assert line_trim(segments, False, False) == segments
    assert line_trim(segments, True, False) == [Segment("oo")]
    assert line_trim(segments, False, True) == [Segment("fo")]
    assert line_trim(segments, True, True) == [Segment("o")]

    fob_segments = [Segment("f"), Segment("o"), Segment("b")]

    assert line_trim(fob_segments, True, False) == [
        Segment("o"),
        Segment("b"),
    ]

    assert line_trim(fob_segments, False, True) == [
        Segment("f"),
        Segment("o"),
    ]

    assert line_trim(fob_segments, True, True) == [
        Segment("o"),
    ]

    assert line_trim([], True, True) == []


def test_line_pad():
    segments = [Segment("foo"), Segment("bar")]
    style = Style.parse("red")
    assert line_pad(segments, 2, 3, style) == [
        Segment("  ", style),
        *segments,
        Segment("   ", style),
    ]

    assert line_pad(segments, 0, 3, style) == [
        *segments,
        Segment("   ", style),
    ]

    assert line_pad(segments, 2, 0, style) == [
        Segment("  ", style),
        *segments,
    ]

    assert line_pad(segments, 0, 0, style) == segments


def test_align_lines_vertical_middle():
    """Regression test for an issue found while working on
    https://github.com/Textualize/textual/issues/3628 - an extra vertical line
    was being produced when aligning. If you passed in a Size of height=1 to
    `align_lines`, it was producing a result containing 2 lines instead of 1."""
    lines = [[Segment("  "), Segment("hello"), Segment("   ")]]
    result = align_lines(
        lines, Style(), size=Size(10, 3), horizontal="center", vertical="middle"
    )
    assert list(result) == [
        [Segment("          ", Style())],
        [Segment("  "), Segment("hello"), Segment("   ")],
        [Segment("          ", Style())],
    ]


def test_align_lines_top_left():
    lines = [
        [Segment("hello")],
        [Segment("world")],
    ]

    result = align_lines(
        lines, Style(), size=Size(10, 4), horizontal="left", vertical="top"
    )

    assert list(result) == [
        [Segment("hello"), Segment("     ", Style())],
        [Segment("world"), Segment("     ", Style())],
        [Segment("          ", Style())],
        [Segment("          ", Style())],
    ]


def test_align_lines_top_right():
    lines = [
        [Segment("hello")],
        [Segment("world")],
    ]

    result = align_lines(
        lines, Style(), size=Size(10, 4), horizontal="right", vertical="top"
    )

    assert list(result) == [
        [Segment("     ", Style()), Segment("hello")],
        [Segment("     ", Style()), Segment("world")],
        [Segment("          ", Style())],
        [Segment("          ", Style())],
    ]


def test_align_lines_perfect_fit_horizontal_left():
    lines = [[Segment("  "), Segment("hello"), Segment("   ")]]  # 10 cells
    result = align_lines(
        lines, Style(), size=Size(10, 1), horizontal="left", vertical="middle"
    )
    assert list(result) == [[Segment("  "), Segment("hello"), Segment("   ")]]


def test_align_lines_perfect_fit_horizontal_center():
    """When the content perfectly fits the available horizontal space,
    no empty segments should be produced. This is a regression test for
    the issue https://github.com/Textualize/textual/issues/3628."""
    lines = [[Segment("  "), Segment("hello"), Segment("   ")]]  # 10 cells of content
    result = align_lines(
        lines, Style(), size=Size(10, 1), horizontal="center", vertical="middle"
    )
    assert list(result) == [[Segment("  "), Segment("hello"), Segment("   ")]]


def test_align_lines_perfect_fit_horizontal_right():
    """When the content perfectly fits the available horizontal space,
    no empty segments should be produced. This is a regression test for
    the issue https://github.com/Textualize/textual/issues/3628."""
    lines = [[Segment("  "), Segment("hello"), Segment("   ")]]  # 10 cells of content
    result = align_lines(
        lines, Style(), size=Size(10, 1), horizontal="right", vertical="middle"
    )
    assert list(result) == [[Segment("  "), Segment("hello"), Segment("   ")]]