rich/tests/test_panel.py

136 lines
6.2 KiB
Python
Raw Permalink Normal View History

2020-03-05 17:31:40 +00:00
import io
import pytest
2022-03-21 16:31:41 +00:00
from rich.console import Console
from rich.panel import Panel
from rich.segment import Segment
from rich.style import Style
2024-09-30 18:48:50 +00:00
from rich.text import Text
2022-03-21 16:31:41 +00:00
2020-03-05 17:31:40 +00:00
tests = [
Panel("Hello, World", padding=0),
Panel("Hello, World", expand=False, padding=0),
Panel.fit("Hello, World", padding=0),
Panel("Hello, World", width=8, padding=0),
Panel(Panel("Hello, World", padding=0), padding=0),
Panel("Hello, World", title="FOO", padding=0),
2021-08-23 17:08:21 +00:00
Panel("Hello, World", subtitle="FOO", padding=0),
2020-03-05 17:31:40 +00:00
]
expected = [
"╭────────────────────────────────────────────────╮\n│Hello, World │\n╰────────────────────────────────────────────────╯\n",
"╭────────────╮\n│Hello, World│\n╰────────────╯\n",
2020-07-10 16:09:41 +00:00
"╭────────────╮\n│Hello, World│\n╰────────────╯\n",
2020-03-05 17:31:40 +00:00
"╭──────╮\n│Hello,│\n│World │\n╰──────╯\n",
"╭────────────────────────────────────────────────╮\n│╭──────────────────────────────────────────────╮│\n││Hello, World ││\n│╰──────────────────────────────────────────────╯│\n╰────────────────────────────────────────────────╯\n",
2020-07-12 15:17:36 +00:00
"╭───────────────────── FOO ──────────────────────╮\n│Hello, World │\n╰────────────────────────────────────────────────╯\n",
2021-08-23 01:49:28 +00:00
"╭────────────────────────────────────────────────╮\n│Hello, World │\n╰───────────────────── FOO ──────────────────────╯\n",
2020-03-05 17:31:40 +00:00
]
def render(panel, width=50) -> str:
2020-06-20 16:51:53 +00:00
console = Console(file=io.StringIO(), width=50, legacy_windows=False)
2020-03-05 17:31:40 +00:00
console.print(panel)
2024-09-30 18:48:50 +00:00
result = console.file.getvalue()
print(result)
return result
2020-03-05 17:31:40 +00:00
@pytest.mark.parametrize("panel,expected", zip(tests, expected))
2024-09-30 18:48:50 +00:00
def test_render_panel(panel, expected) -> None:
2020-03-05 17:31:40 +00:00
assert render(panel) == expected
2024-09-30 18:48:50 +00:00
def test_console_width() -> None:
2020-06-20 16:51:53 +00:00
console = Console(file=io.StringIO(), width=50, legacy_windows=False)
2020-03-05 17:31:40 +00:00
panel = Panel("Hello, World", expand=False)
2021-03-25 21:08:36 +00:00
min_width, max_width = panel.__rich_measure__(console, console.options)
assert min_width == 16
assert max_width == 16
2020-03-05 17:31:40 +00:00
2024-09-30 18:48:50 +00:00
def test_fixed_width() -> None:
2020-11-25 17:54:16 +00:00
console = Console(file=io.StringIO(), width=50, legacy_windows=False)
panel = Panel("Hello World", width=20)
2021-03-25 21:08:36 +00:00
min_width, max_width = panel.__rich_measure__(console, console.options)
2020-11-25 17:54:16 +00:00
assert min_width == 20
assert max_width == 20
2024-09-30 18:48:50 +00:00
def test_render_size() -> None:
2022-03-21 16:31:41 +00:00
console = Console(width=63, height=46, legacy_windows=False)
options = console.options.update_dimensions(80, 4)
lines = console.render_lines(Panel("foo", title="Hello"), options=options)
print(repr(lines))
expected = [
[
Segment("╭─", Style()),
2023-07-28 08:24:13 +00:00
Segment(
"────────────────────────────────── Hello ───────────────────────────────────"
),
2022-03-21 16:31:41 +00:00
Segment("─╮", Style()),
],
[
Segment("", Style()),
Segment(" ", Style()),
Segment("foo"),
Segment(
2022-07-11 11:49:56 +00:00
" ",
Style(),
2022-03-21 16:31:41 +00:00
),
Segment(" ", Style()),
Segment("", Style()),
],
[
Segment("", Style()),
Segment(" ", Style()),
Segment(
" ",
Style(),
),
Segment(" ", Style()),
Segment("", Style()),
],
[
Segment(
"╰──────────────────────────────────────────────────────────────────────────────╯",
Style(),
)
],
]
assert lines == expected
2024-09-30 18:48:50 +00:00
def test_title_text() -> None:
panel = Panel(
"Hello, World",
title=Text("title", style="red"),
subtitle=Text("subtitle", style="magenta bold"),
)
console = Console(
file=io.StringIO(),
width=50,
2024-10-01 11:38:31 +00:00
height=20,
2024-09-30 18:48:50 +00:00
legacy_windows=False,
force_terminal=True,
color_system="truecolor",
)
console.print(panel)
result = console.file.getvalue()
print(repr(result))
2024-10-01 11:38:31 +00:00
expected = "╭────────────────────\x1b[31m title \x1b[0m─────────────────────╮\n│ Hello, World │\n╰───────────────────\x1b[1;35m subtitle \x1b[0m───────────────────╯\n"
2024-09-30 18:48:50 +00:00
assert result == expected
2020-03-05 17:31:40 +00:00
if __name__ == "__main__":
expected = []
for panel in tests:
result = render(panel)
print(result)
expected.append(result)
print("--")
print()
print(f"expected={repr(expected)}")