rich/tests/test_segment.py

88 lines
2.6 KiB
Python
Raw Normal View History

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():
2020-05-14 19:15:31 +00:00
assert repr(Segment("foo")) == "Segment('foo', None, False)"
2019-12-11 18:35:25 +00:00
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_lines():
lines = [Segment("Hello\nWorld")]
assert list(Segment.split_lines(lines)) == [[Segment("Hello")], [Segment("World")]]
2019-12-11 18:53:51 +00:00
def test_split_and_crop_lines():
assert list(
Segment.split_and_crop_lines([Segment("Hello\nWorld!\n"), Segment("foo")], 4)
2020-03-01 22:44:00 +00:00
) == [
[Segment("Hell"), Segment("\n", None)],
[Segment("Worl"), Segment("\n", None)],
[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([])) == []
2020-05-14 19:15:31 +00:00
def test_filter_control():
segments = [Segment("foo"), Segment("bar", is_control=True)]
assert list(Segment.filter_control(segments)) == [Segment("foo")]
assert list(Segment.filter_control(segments, is_control=True)) == [
Segment("bar", is_control=True)
]