Debian Codegraph @

tests/test_segment.py

raw
1 from io import StringIO
2
3 import pytest
4
5 from rich.cells import cell_len
6 from rich.segment import ControlType, Segment, SegmentLines, Segments
7 from rich.style import Style
8
9
10 def test_repr():
11 assert repr(Segment("foo")) == "Segment('foo')"
12 home = (ControlType.HOME, 0)
13 assert (
14 repr(Segment("foo", None, [home]))
15 == "Segment('foo', None, [(<ControlType.HOME: 3>, 0)])"
16 )
17
18
19 def test_line():
20 assert Segment.line() == Segment("\n")
21
22
23 def test_apply_style():
24 segments = [Segment("foo"), Segment("bar", Style(bold=True))]
25 assert Segment.apply_style(segments, None) is segments
26 assert list(Segment.apply_style(segments, Style(italic=True))) == [
27 Segment("foo", Style(italic=True)),
28 Segment("bar", Style(italic=True, bold=True)),
29 ]
30
31
32 def test_split_lines():
33 lines = [Segment("Hello\nWorld")]
34 assert list(Segment.split_lines(lines)) == [[Segment("Hello")], [Segment("World")]]
35
36
37 def test_split_lines_terminator():
38 lines = [Segment("Hello\nWorld")]
39 assert list(Segment.split_lines_terminator(lines)) == [
40 ([Segment("Hello")], True),
41 ([Segment("World")], False),
42 ]
43
44
45 def test_split_lines_terminator_single_line():
46 lines = [Segment("Hello")]
47 assert list(Segment.split_lines_terminator(lines)) == [
48 ([Segment("Hello")], False),
49 ]
50
51
52 def test_split_and_crop_lines():
53 assert list(
54 Segment.split_and_crop_lines([Segment("Hello\nWorld!\n"), Segment("foo")], 4)
55 ) == [
56 [Segment("Hell"), Segment("\n", None)],
57 [Segment("Worl"), Segment("\n", None)],
58 [Segment("foo"), Segment(" ")],
59 ]
60
61
62 def test_adjust_line_length():
63 line = [Segment("Hello", "foo")]
64 assert Segment.adjust_line_length(line, 10, style="bar") == [
65 Segment("Hello", "foo"),
66 Segment(" ", "bar"),
67 ]
68
69 line = [Segment("H"), Segment("ello, World!")]
70 assert Segment.adjust_line_length(line, 5) == [Segment("H"), Segment("ello")]
71
72 line = [Segment("Hello")]
73 assert Segment.adjust_line_length(line, 5) == line
74
75
76 def test_get_line_length():
77 assert Segment.get_line_length([Segment("foo"), Segment("bar")]) == 6
78
79
80 def test_get_shape():
81 assert Segment.get_shape([[Segment("Hello")]]) == (5, 1)
82 assert Segment.get_shape([[Segment("Hello")], [Segment("World!")]]) == (6, 2)
83
84
85 def test_set_shape():
86 assert Segment.set_shape([[Segment("Hello")]], 10) == [
87 [Segment("Hello"), Segment(" ")]
88 ]
89 assert Segment.set_shape([[Segment("Hello")]], 10, 2) == [
90 [Segment("Hello"), Segment(" ")],
91 [Segment(" " * 10)],
92 ]
93
94
95 def test_simplify():
96 assert list(
97 Segment.simplify([Segment("Hello"), Segment(" "), Segment("World!")])
98 ) == [Segment("Hello World!")]
99 assert list(
100 Segment.simplify(
101 [Segment("Hello", "red"), Segment(" ", "red"), Segment("World!", "blue")]
102 )
103 ) == [Segment("Hello ", "red"), Segment("World!", "blue")]
104 assert list(Segment.simplify([])) == []
105
106
107 def test_filter_control():
108 control_code = (ControlType.HOME, 0)
109 segments = [Segment("foo"), Segment("bar", None, (control_code,))]
110 assert list(Segment.filter_control(segments)) == [Segment("foo")]
111 assert list(Segment.filter_control(segments, is_control=True)) == [
112 Segment("bar", None, (control_code,))
113 ]
114
115
116 def test_strip_styles():
117 segments = [Segment("foo", Style(bold=True))]
118 assert list(Segment.strip_styles(segments)) == [Segment("foo", None)]
119
120
121 def test_strip_links():
122 segments = [Segment("foo", Style(bold=True, link="https://www.example.org"))]
123 assert list(Segment.strip_links(segments)) == [Segment("foo", Style(bold=True))]
124
125
126 def test_remove_color():
127 segments = [
128 Segment("foo", Style(bold=True, color="red")),
129 Segment("bar", None),
130 ]
131 assert list(Segment.remove_color(segments)) == [
132 Segment("foo", Style(bold=True)),
133 Segment("bar", None),
134 ]
135
136
137 def test_is_control():
138 assert Segment("foo", Style(bold=True)).is_control == False
139 assert Segment("foo", Style(bold=True), []).is_control == True
140 assert Segment("foo", Style(bold=True), [(ControlType.HOME, 0)]).is_control == True
141
142
143 def test_segments_renderable():
144 segments = Segments([Segment("foo")])
145 assert list(segments.__rich_console__(None, None)) == [Segment("foo")]
146
147 segments = Segments([Segment("foo")], new_lines=True)
148 assert list(segments.__rich_console__(None, None)) == [
149 Segment("foo"),
150 Segment.line(),
151 ]
152
153
154 def test_divide():
155 bold = Style(bold=True)
156 italic = Style(italic=True)
157 segments = [
158 Segment("Hello", bold),
159 Segment(" World!", italic),
160 ]
161
162 assert list(Segment.divide(segments, [])) == []
163 assert list(Segment.divide([], [1])) == [[]]
164
165 assert list(Segment.divide(segments, [1])) == [[Segment("H", bold)]]
166
167 assert list(Segment.divide(segments, [1, 2])) == [
168 [Segment("H", bold)],
169 [Segment("e", bold)],
170 ]
171
172 assert list(Segment.divide(segments, [1, 2, 12])) == [
173 [Segment("H", bold)],
174 [Segment("e", bold)],
175 [Segment("llo", bold), Segment(" World!", italic)],
176 ]
177
178 assert list(Segment.divide(segments, [4, 20])) == [
179 [Segment("Hell", bold)],
180 [Segment("o", bold), Segment(" World!", italic)],
181 ]
182
183
184 # https://github.com/textualize/rich/issues/1755
185 def test_divide_complex():
186 MAP = (
187 "[on orange4] [on green]XX[on orange4] \n"
188 " \n"
189 " \n"
190 " \n"
191 " [bright_red on black]Y[on orange4] \n"
192 "[on green]X[on orange4] [on green]X[on orange4] \n"
193 " [on green]X[on orange4] [on green]X\n"
194 "[on orange4] \n"
195 " [on green]XX[on orange4] \n"
196 )
197 from rich.console import Console
198 from rich.text import Text
199
200 text = Text.from_markup(MAP)
201 console = Console(
202 color_system="truecolor", width=30, force_terminal=True, file=StringIO()
203 )
204 console.print(text)
205 result = console.file.getvalue()
206
207 print(repr(result))
208 expected = "\x1b[48;5;94m \x1b[0m\x1b[42mXX\x1b[0m\x1b[48;5;94m \x1b[0m\n\x1b[48;5;94m \x1b[0m\n\x1b[48;5;94m \x1b[0m\n\x1b[48;5;94m \x1b[0m\n\x1b[48;5;94m \x1b[0m\x1b[91;40mY\x1b[0m\x1b[91;48;5;94m \x1b[0m\n\x1b[91;42mX\x1b[0m\x1b[91;48;5;94m \x1b[0m\x1b[91;42mX\x1b[0m\x1b[91;48;5;94m \x1b[0m\n\x1b[91;48;5;94m \x1b[0m\x1b[91;42mX\x1b[0m\x1b[91;48;5;94m \x1b[0m\x1b[91;42mX\x1b[0m\n\x1b[91;48;5;94m \x1b[0m\n\x1b[91;48;5;94m \x1b[0m\x1b[91;42mXX\x1b[0m\x1b[91;48;5;94m \x1b[0m\n\n"
209 assert result == expected
210
211
212 def test_divide_emoji():
213 bold = Style(bold=True)
214 italic = Style(italic=True)
215 segments = [
216 Segment("Hello", bold),
217 Segment("💩💩💩", italic),
218 ]
219
220 assert list(Segment.divide(segments, [7])) == [
221 [Segment("Hello", bold), Segment("💩", italic)],
222 ]
223 assert list(Segment.divide(segments, [8])) == [
224 [Segment("Hello", bold), Segment("💩 ", italic)],
225 ]
226 assert list(Segment.divide(segments, [9])) == [
227 [Segment("Hello", bold), Segment("💩💩", italic)],
228 ]
229 assert list(Segment.divide(segments, [8, 11])) == [
230 [Segment("Hello", bold), Segment("💩 ", italic)],
231 [Segment(" 💩", italic)],
232 ]
233 assert list(Segment.divide(segments, [9, 11])) == [
234 [Segment("Hello", bold), Segment("💩💩", italic)],
235 [Segment("💩", italic)],
236 ]
237
238
239 def test_divide_edge():
240 segments = [Segment("foo"), Segment("bar"), Segment("baz")]
241 result = list(Segment.divide(segments, [1, 3, 9]))
242 print(result)
243 assert result == [
244 [Segment("f")],
245 [Segment("oo")],
246 [Segment("bar"), Segment("baz")],
247 ]
248
249
250 def test_divide_edge_2():
251 segments = [
252 Segment("╭─"),
253 Segment(
254 "────── Placeholder ───────",
255 ),
256 Segment(
257 "─╮",
258 ),
259 ]
260 result = list(Segment.divide(segments, [30, 60]))
261 expected = [segments, []]
262 print(repr(result))
263 assert result == expected
264
265
266 @pytest.mark.parametrize(
267 "text,split,result",
268 [
269 ("XX", 4, (Segment("XX"), Segment(""))),
270 ("X", 1, (Segment("X"), Segment(""))),
271 ("💩", 1, (Segment(" "), Segment(" "))),
272 ("XY", 1, (Segment("X"), Segment("Y"))),
273 ("💩X", 1, (Segment(" "), Segment(" X"))),
274 ("💩💩", 1, (Segment(" "), Segment(" 💩"))),
275 ("X💩Y", 2, (Segment("X "), Segment(" Y"))),
276 ("X💩YZ", 2, (Segment("X "), Segment(" YZ"))),
277 ("X💩💩Z", 2, (Segment("X "), Segment(" 💩Z"))),
278 ("X💩💩Z", 3, (Segment("X💩"), Segment("💩Z"))),
279 ("X💩💩Z", 4, (Segment("X💩 "), Segment(" Z"))),
280 ("X💩💩Z", 5, (Segment("X💩💩"), Segment("Z"))),
281 ("X💩💩Z", 6, (Segment("X💩💩Z"), Segment(""))),
282 ("XYZABC💩💩", 6, (Segment("XYZABC"), Segment("💩💩"))),
283 ("XYZABC💩💩", 7, (Segment("XYZABC "), Segment(" 💩"))),
284 ("XYZABC💩💩", 8, (Segment("XYZABC💩"), Segment("💩"))),
285 ("XYZABC💩💩", 9, (Segment("XYZABC💩 "), Segment(" "))),
286 ("XYZABC💩💩", 10, (Segment("XYZABC💩💩"), Segment(""))),
287 ("💩💩💩💩💩", 3, (Segment("💩 "), Segment(" 💩💩💩"))),
288 ("💩💩💩💩💩", 4, (Segment("💩💩"), Segment("💩💩💩"))),
289 ("💩X💩Y💩Z💩A💩", 4, (Segment("💩X "), Segment(" Y💩Z💩A💩"))),
290 ("XYZABC", 4, (Segment("XYZA"), Segment("BC"))),
291 ("XYZABC", 5, (Segment("XYZAB"), Segment("C"))),
292 (
293 "a1あ11bcdaef",
294 9,
295 (Segment("a1あ11b"), Segment("cdaef")),
296 ),
297 ],
298 )
299 def test_split_cells_emoji(text, split, result):
300 assert Segment(text).split_cells(split) == result
301
302
303 @pytest.mark.parametrize(
304 "segment",
305 [
306 Segment("早乙女リリエル (CV: 徳井青)"),
307 Segment("メイド・イン・きゅんクチュアリ☆ "),
308 Segment("TVアニメ「メルクストーリア -無気力少年と瓶の中の少女-」 主題歌CD"),
309 Segment("南無阿弥JKうらめしや?! "),
310 Segment("メルク (CV: 水瀬いのり) "),
311 Segment(" メルク (CV: 水瀬いのり) "),
312 Segment(" メルク (CV: 水瀬いのり) "),
313 Segment(" メルク (CV: 水瀬いのり) "),
314 ],
315 )
316 def test_split_cells_mixed(segment: Segment) -> None:
317 """Check that split cells splits on cell positions."""
318 # Caused https://github.com/Textualize/textual/issues/4996 in Textual
319
320 for position in range(0, segment.cell_length + 1):
321 left, right = Segment.split_cells(segment, position)
322 assert all(
323 cell_len(c) > 0 for c in segment.text
324 ) # Sanity check there aren't any sneaky control codes
325 assert cell_len(left.text) == position
326 assert cell_len(right.text) == segment.cell_length - position
327
328
329 def test_split_cells_doubles() -> None:
330 """Check that split cells splits on cell positions with all double width characters."""
331 test = Segment("早" * 20)
332 for position in range(1, test.cell_length):
333 left, right = Segment.split_cells(test, position)
334 assert cell_len(left.text) == position
335 assert cell_len(right.text) == test.cell_length - position
336
337
338 def test_split_cells_single() -> None:
339 """Check that split cells splits on cell positions with all single width characters."""
340 test = Segment("A" * 20)
341 for position in range(1, test.cell_length):
342 left, right = Segment.split_cells(test, position)
343 assert cell_len(left.text) == position
344 assert cell_len(right.text) == test.cell_length - position
345
346
347 def test_segment_lines_renderable():
348 lines = [[Segment("hello"), Segment(" "), Segment("world")], [Segment("foo")]]
349 segment_lines = SegmentLines(lines)
350 assert list(segment_lines.__rich_console__(None, None)) == [
351 Segment("hello"),
352 Segment(" "),
353 Segment("world"),
354 Segment("foo"),
355 ]
356
357 segment_lines = SegmentLines(lines, new_lines=True)
358 assert list(segment_lines.__rich_console__(None, None)) == [
359 Segment("hello"),
360 Segment(" "),
361 Segment("world"),
362 Segment("\n"),
363 Segment("foo"),
364 Segment("\n"),
365 ]
366
367
368 def test_align_top():
369 lines = [[Segment("X")]]
370 assert Segment.align_top(lines, 3, 1, Style()) == lines
371 assert Segment.align_top(lines, 3, 3, Style()) == [
372 [Segment("X")],
373 [Segment(" ", Style())],
374 [Segment(" ", Style())],
375 ]
376
377
378 def test_align_middle():
379 lines = [[Segment("X")]]
380 assert Segment.align_middle(lines, 3, 1, Style()) == lines
381 assert Segment.align_middle(lines, 3, 3, Style()) == [
382 [Segment(" ", Style())],
383 [Segment("X")],
384 [Segment(" ", Style())],
385 ]
386
387
388 def test_align_bottom():
389 lines = [[Segment("X")]]
390 assert Segment.align_bottom(lines, 3, 1, Style()) == lines
391 assert Segment.align_bottom(lines, 3, 3, Style()) == [
392 [Segment(" ", Style())],
393 [Segment(" ", Style())],
394 [Segment("X")],
395 ]