2019-12-11 18:35:25 +00:00
|
|
|
from rich.segment import Segment
|
2019-12-11 18:53:51 +00:00
|
|
|
from rich.style import Style
|
2019-12-11 18:35:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_repr():
|
|
|
|
assert repr(Segment("foo")) == "Segment('foo', None)"
|
|
|
|
|
|
|
|
|
|
|
|
def test_line():
|
|
|
|
assert Segment.line() == Segment("\n")
|
2019-12-11 18:53:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
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_and_crop_lines():
|
|
|
|
assert list(
|
|
|
|
Segment.split_and_crop_lines([Segment("Hello\nWorld!\n"), Segment("foo")], 4)
|
2019-12-18 16:46:24 +00:00
|
|
|
) == [[Segment("Hell")], [Segment("Worl")], [Segment("foo"), Segment(" ")]]
|
2019-12-11 18:53:51 +00:00
|
|
|
|
|
|
|
|
2019-12-24 12:48:57 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-12-11 18:53:51 +00:00
|
|
|
def test_get_line_length():
|
|
|
|
assert Segment.get_line_length([Segment("foo"), Segment("bar")]) == 6
|
2019-12-24 12:48:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
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([])) == []
|