rich/tests/test_padding.py

61 lines
1.6 KiB
Python
Raw Normal View History

2020-04-26 15:28:18 +00:00
import pytest
from rich.padding import Padding
2021-02-14 11:07:16 +00:00
from rich.console import Console, ConsoleDimensions, ConsoleOptions
2020-10-08 11:21:49 +00:00
from rich.style import Style
from rich.segment import Segment
2020-04-26 15:28:18 +00:00
def test_repr():
padding = Padding("foo", (1, 2))
assert isinstance(repr(padding), str)
2020-10-08 09:07:30 +00:00
def test_indent():
2020-10-08 11:21:49 +00:00
indent_result = Padding.indent("test", 4)
assert indent_result.top == 0
assert indent_result.right == 0
assert indent_result.bottom == 0
assert indent_result.left == 4
2020-10-08 09:07:30 +00:00
2020-04-26 15:28:18 +00:00
def test_unpack():
assert Padding.unpack(3) == (3, 3, 3, 3)
assert Padding.unpack((3,)) == (3, 3, 3, 3)
assert Padding.unpack((3, 4)) == (3, 4, 3, 4)
assert Padding.unpack((3, 4, 5, 6)) == (3, 4, 5, 6)
with pytest.raises(ValueError):
Padding.unpack((1, 2, 3))
2020-10-08 11:21:49 +00:00
2020-10-19 15:41:15 +00:00
def test_expand_false():
console = Console(width=100, color_system=None)
console.begin_capture()
console.print(Padding("foo", 1, expand=False))
assert console.end_capture() == " \n foo \n \n"
2020-10-08 11:21:49 +00:00
def test_rich_console():
renderable = "test renderable"
style = Style(color="red")
options = ConsoleOptions(
2021-02-14 11:07:16 +00:00
ConsoleDimensions(80, 25),
2021-07-11 13:25:14 +00:00
max_height=25,
2020-10-11 16:33:16 +00:00
legacy_windows=False,
min_width=10,
max_width=20,
is_terminal=False,
encoding="utf-8",
2020-10-08 11:21:49 +00:00
)
expected_outputs = [
Segment(renderable, style=style),
Segment(" " * (20 - len(renderable)), style=style),
Segment("\n", style=None),
]
padding_generator = Padding(renderable, style=style).__rich_console__(
Console(), options
)
for output, expected in zip(padding_generator, expected_outputs):
assert output == expected