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
|
import io
import pytest
from rich.console import Console
from rich.panel import Panel
from rich.segment import Segment
from rich.style import Style
from rich.text import Text
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),
Panel("Hello, World", subtitle="FOO", padding=0),
]
expected = [
"╭────────────────────────────────────────────────╮\n│Hello, World │\n╰────────────────────────────────────────────────╯\n",
"╭────────────╮\n│Hello, World│\n╰────────────╯\n",
"╭────────────╮\n│Hello, World│\n╰────────────╯\n",
"╭──────╮\n│Hello,│\n│World │\n╰──────╯\n",
"╭────────────────────────────────────────────────╮\n│╭──────────────────────────────────────────────╮│\n││Hello, World ││\n│╰──────────────────────────────────────────────╯│\n╰────────────────────────────────────────────────╯\n",
"╭───────────────────── FOO ──────────────────────╮\n│Hello, World │\n╰────────────────────────────────────────────────╯\n",
"╭────────────────────────────────────────────────╮\n│Hello, World │\n╰───────────────────── FOO ──────────────────────╯\n",
]
def render(panel, width=50) -> str:
console = Console(file=io.StringIO(), width=50, legacy_windows=False)
console.print(panel)
result = console.file.getvalue()
print(result)
return result
@pytest.mark.parametrize("panel,expected", zip(tests, expected))
def test_render_panel(panel, expected) -> None:
assert render(panel) == expected
def test_console_width() -> None:
console = Console(file=io.StringIO(), width=50, legacy_windows=False)
panel = Panel("Hello, World", expand=False)
min_width, max_width = panel.__rich_measure__(console, console.options)
assert min_width == 16
assert max_width == 16
def test_fixed_width() -> None:
console = Console(file=io.StringIO(), width=50, legacy_windows=False)
panel = Panel("Hello World", width=20)
min_width, max_width = panel.__rich_measure__(console, console.options)
assert min_width == 20
assert max_width == 20
def test_render_size() -> None:
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()),
Segment(
"────────────────────────────────── Hello ───────────────────────────────────"
),
Segment("─╮", Style()),
],
[
Segment("│", Style()),
Segment(" ", Style()),
Segment("foo"),
Segment(
" ",
Style(),
),
Segment(" ", Style()),
Segment("│", Style()),
],
[
Segment("│", Style()),
Segment(" ", Style()),
Segment(
" ",
Style(),
),
Segment(" ", Style()),
Segment("│", Style()),
],
[
Segment(
"╰──────────────────────────────────────────────────────────────────────────────╯",
Style(),
)
],
]
assert lines == expected
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,
height=20,
legacy_windows=False,
force_terminal=True,
color_system="truecolor",
)
console.print(panel)
result = console.file.getvalue()
print(repr(result))
expected = "╭────────────────────\x1b[31m title \x1b[0m─────────────────────╮\n│ Hello, World │\n╰───────────────────\x1b[1;35m subtitle \x1b[0m───────────────────╯\n"
assert result == expected
if __name__ == "__main__":
expected = []
for panel in tests:
result = render(panel)
print(result)
expected.append(result)
print("--")
print()
print(f"expected={repr(expected)}")
|