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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
from io import StringIO
import pytest
from rich.cells import cell_len
from rich.segment import ControlType, Segment, SegmentLines, Segments
from rich.style import Style
def test_repr():
assert repr(Segment("foo")) == "Segment('foo')"
home = (ControlType.HOME, 0)
assert (
repr(Segment("foo", None, [home]))
== "Segment('foo', None, [(<ControlType.HOME: 3>, 0)])"
)
def test_line():
assert Segment.line() == Segment("\n")
def test_apply_style():
segments = [Segment("foo"), Segment("bar", Style(bold=True))]
assert Segment.apply_style(segments, None) is segments
assert list(Segment.apply_style(segments, Style(italic=True))) == [
Segment("foo", Style(italic=True)),
Segment("bar", Style(italic=True, bold=True)),
]
def test_split_lines():
lines = [Segment("Hello\nWorld")]
assert list(Segment.split_lines(lines)) == [[Segment("Hello")], [Segment("World")]]
def test_split_and_crop_lines():
assert list(
Segment.split_and_crop_lines([Segment("Hello\nWorld!\n"), Segment("foo")], 4)
) == [
[Segment("Hell"), Segment("\n", None)],
[Segment("Worl"), Segment("\n", None)],
[Segment("foo"), Segment(" ")],
]
def test_adjust_line_length():
line = [Segment("Hello", "foo")]
assert Segment.adjust_line_length(line, 10, style="bar") == [
Segment("Hello", "foo"),
Segment(" ", "bar"),
]
line = [Segment("H"), Segment("ello, World!")]
assert Segment.adjust_line_length(line, 5) == [Segment("H"), Segment("ello")]
line = [Segment("Hello")]
assert Segment.adjust_line_length(line, 5) == line
def test_get_line_length():
assert Segment.get_line_length([Segment("foo"), Segment("bar")]) == 6
def test_get_shape():
assert Segment.get_shape([[Segment("Hello")]]) == (5, 1)
assert Segment.get_shape([[Segment("Hello")], [Segment("World!")]]) == (6, 2)
def test_set_shape():
assert Segment.set_shape([[Segment("Hello")]], 10) == [
[Segment("Hello"), Segment(" ")]
]
assert Segment.set_shape([[Segment("Hello")]], 10, 2) == [
[Segment("Hello"), Segment(" ")],
[Segment(" " * 10)],
]
def test_simplify():
assert list(
Segment.simplify([Segment("Hello"), Segment(" "), Segment("World!")])
) == [Segment("Hello World!")]
assert list(
Segment.simplify(
[Segment("Hello", "red"), Segment(" ", "red"), Segment("World!", "blue")]
)
) == [Segment("Hello ", "red"), Segment("World!", "blue")]
assert list(Segment.simplify([])) == []
def test_filter_control():
control_code = (ControlType.HOME, 0)
segments = [Segment("foo"), Segment("bar", None, (control_code,))]
assert list(Segment.filter_control(segments)) == [Segment("foo")]
assert list(Segment.filter_control(segments, is_control=True)) == [
Segment("bar", None, (control_code,))
]
def test_strip_styles():
segments = [Segment("foo", Style(bold=True))]
assert list(Segment.strip_styles(segments)) == [Segment("foo", None)]
def test_strip_links():
segments = [Segment("foo", Style(bold=True, link="https://www.example.org"))]
assert list(Segment.strip_links(segments)) == [Segment("foo", Style(bold=True))]
def test_remove_color():
segments = [
Segment("foo", Style(bold=True, color="red")),
Segment("bar", None),
]
assert list(Segment.remove_color(segments)) == [
Segment("foo", Style(bold=True)),
Segment("bar", None),
]
def test_is_control():
assert Segment("foo", Style(bold=True)).is_control == False
assert Segment("foo", Style(bold=True), []).is_control == True
assert Segment("foo", Style(bold=True), [(ControlType.HOME, 0)]).is_control == True
def test_segments_renderable():
segments = Segments([Segment("foo")])
assert list(segments.__rich_console__(None, None)) == [Segment("foo")]
segments = Segments([Segment("foo")], new_lines=True)
assert list(segments.__rich_console__(None, None)) == [
Segment("foo"),
Segment.line(),
]
def test_divide():
bold = Style(bold=True)
italic = Style(italic=True)
segments = [
Segment("Hello", bold),
Segment(" World!", italic),
]
assert list(Segment.divide(segments, [])) == []
assert list(Segment.divide([], [1])) == [[]]
assert list(Segment.divide(segments, [1])) == [[Segment("H", bold)]]
assert list(Segment.divide(segments, [1, 2])) == [
[Segment("H", bold)],
[Segment("e", bold)],
]
assert list(Segment.divide(segments, [1, 2, 12])) == [
[Segment("H", bold)],
[Segment("e", bold)],
[Segment("llo", bold), Segment(" World!", italic)],
]
assert list(Segment.divide(segments, [4, 20])) == [
[Segment("Hell", bold)],
[Segment("o", bold), Segment(" World!", italic)],
]
# https://github.com/textualize/rich/issues/1755
def test_divide_complex():
MAP = (
"[on orange4] [on green]XX[on orange4] \n"
" \n"
" \n"
" \n"
" [bright_red on black]Y[on orange4] \n"
"[on green]X[on orange4] [on green]X[on orange4] \n"
" [on green]X[on orange4] [on green]X\n"
"[on orange4] \n"
" [on green]XX[on orange4] \n"
)
from rich.console import Console
from rich.text import Text
text = Text.from_markup(MAP)
console = Console(
color_system="truecolor", width=30, force_terminal=True, file=StringIO()
)
console.print(text)
result = console.file.getvalue()
print(repr(result))
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"
assert result == expected
def test_divide_emoji():
bold = Style(bold=True)
italic = Style(italic=True)
segments = [
Segment("Hello", bold),
Segment("💩💩💩", italic),
]
assert list(Segment.divide(segments, [7])) == [
[Segment("Hello", bold), Segment("💩", italic)],
]
assert list(Segment.divide(segments, [8])) == [
[Segment("Hello", bold), Segment("💩 ", italic)],
]
assert list(Segment.divide(segments, [9])) == [
[Segment("Hello", bold), Segment("💩💩", italic)],
]
assert list(Segment.divide(segments, [8, 11])) == [
[Segment("Hello", bold), Segment("💩 ", italic)],
[Segment(" 💩", italic)],
]
assert list(Segment.divide(segments, [9, 11])) == [
[Segment("Hello", bold), Segment("💩💩", italic)],
[Segment("💩", italic)],
]
def test_divide_edge():
segments = [Segment("foo"), Segment("bar"), Segment("baz")]
result = list(Segment.divide(segments, [1, 3, 9]))
print(result)
assert result == [
[Segment("f")],
[Segment("oo")],
[Segment("bar"), Segment("baz")],
]
def test_divide_edge_2():
segments = [
Segment("╭─"),
Segment(
"────── Placeholder ───────",
),
Segment(
"─╮",
),
]
result = list(Segment.divide(segments, [30, 60]))
expected = [segments, []]
print(repr(result))
assert result == expected
@pytest.mark.parametrize(
"text,split,result",
[
("XX", 4, (Segment("XX"), Segment(""))),
("X", 1, (Segment("X"), Segment(""))),
("💩", 1, (Segment(" "), Segment(" "))),
("XY", 1, (Segment("X"), Segment("Y"))),
("💩X", 1, (Segment(" "), Segment(" X"))),
("💩💩", 1, (Segment(" "), Segment(" 💩"))),
("X💩Y", 2, (Segment("X "), Segment(" Y"))),
("X💩YZ", 2, (Segment("X "), Segment(" YZ"))),
("X💩💩Z", 2, (Segment("X "), Segment(" 💩Z"))),
("X💩💩Z", 3, (Segment("X💩"), Segment("💩Z"))),
("X💩💩Z", 4, (Segment("X💩 "), Segment(" Z"))),
("X💩💩Z", 5, (Segment("X💩💩"), Segment("Z"))),
("X💩💩Z", 6, (Segment("X💩💩Z"), Segment(""))),
("XYZABC💩💩", 6, (Segment("XYZABC"), Segment("💩💩"))),
("XYZABC💩💩", 7, (Segment("XYZABC "), Segment(" 💩"))),
("XYZABC💩💩", 8, (Segment("XYZABC💩"), Segment("💩"))),
("XYZABC💩💩", 9, (Segment("XYZABC💩 "), Segment(" "))),
("XYZABC💩💩", 10, (Segment("XYZABC💩💩"), Segment(""))),
("💩💩💩💩💩", 3, (Segment("💩 "), Segment(" 💩💩💩"))),
("💩💩💩💩💩", 4, (Segment("💩💩"), Segment("💩💩💩"))),
("💩X💩Y💩Z💩A💩", 4, (Segment("💩X "), Segment(" Y💩Z💩A💩"))),
("XYZABC", 4, (Segment("XYZA"), Segment("BC"))),
("XYZABC", 5, (Segment("XYZAB"), Segment("C"))),
(
"a1あ11bcdaef",
9,
(Segment("a1あ11b"), Segment("cdaef")),
),
],
)
def test_split_cells_emoji(text, split, result):
assert Segment(text).split_cells(split) == result
@pytest.mark.parametrize(
"segment",
[
Segment("早乙女リリエル (CV: 徳井青)"),
Segment("メイド・イン・きゅんクチュアリ☆ "),
Segment("TVアニメ「メルクストーリア -無気力少年と瓶の中の少女-」 主題歌CD"),
Segment("南無阿弥JKうらめしや?! "),
Segment("メルク (CV: 水瀬いのり) "),
Segment(" メルク (CV: 水瀬いのり) "),
Segment(" メルク (CV: 水瀬いのり) "),
Segment(" メルク (CV: 水瀬いのり) "),
],
)
def test_split_cells_mixed(segment: Segment) -> None:
"""Check that split cells splits on cell positions."""
# Caused https://github.com/Textualize/textual/issues/4996 in Textual
for position in range(0, segment.cell_length + 1):
left, right = Segment.split_cells(segment, position)
assert all(
cell_len(c) > 0 for c in segment.text
) # Sanity check there aren't any sneaky control codes
assert cell_len(left.text) == position
assert cell_len(right.text) == segment.cell_length - position
def test_split_cells_doubles() -> None:
"""Check that split cells splits on cell positions with all double width characters."""
test = Segment("早" * 20)
for position in range(1, test.cell_length):
left, right = Segment.split_cells(test, position)
assert cell_len(left.text) == position
assert cell_len(right.text) == test.cell_length - position
def test_split_cells_single() -> None:
"""Check that split cells splits on cell positions with all single width characters."""
test = Segment("A" * 20)
for position in range(1, test.cell_length):
left, right = Segment.split_cells(test, position)
assert cell_len(left.text) == position
assert cell_len(right.text) == test.cell_length - position
def test_segment_lines_renderable():
lines = [[Segment("hello"), Segment(" "), Segment("world")], [Segment("foo")]]
segment_lines = SegmentLines(lines)
assert list(segment_lines.__rich_console__(None, None)) == [
Segment("hello"),
Segment(" "),
Segment("world"),
Segment("foo"),
]
segment_lines = SegmentLines(lines, new_lines=True)
assert list(segment_lines.__rich_console__(None, None)) == [
Segment("hello"),
Segment(" "),
Segment("world"),
Segment("\n"),
Segment("foo"),
Segment("\n"),
]
def test_align_top():
lines = [[Segment("X")]]
assert Segment.align_top(lines, 3, 1, Style()) == lines
assert Segment.align_top(lines, 3, 3, Style()) == [
[Segment("X")],
[Segment(" ", Style())],
[Segment(" ", Style())],
]
def test_align_middle():
lines = [[Segment("X")]]
assert Segment.align_middle(lines, 3, 1, Style()) == lines
assert Segment.align_middle(lines, 3, 3, Style()) == [
[Segment(" ", Style())],
[Segment("X")],
[Segment(" ", Style())],
]
def test_align_bottom():
lines = [[Segment("X")]]
assert Segment.align_bottom(lines, 3, 1, Style()) == lines
assert Segment.align_bottom(lines, 3, 3, Style()) == [
[Segment(" ", Style())],
[Segment(" ", Style())],
[Segment("X")],
]
|