Debian Codegraph @

tests/test_panel.py

raw
1 import io
2
3 import pytest
4
5 from rich.console import Console
6 from rich.panel import Panel
7 from rich.segment import Segment
8 from rich.style import Style
9 from rich.text import Text
10
11 tests = [
12 Panel("Hello, World", padding=0),
13 Panel("Hello, World", expand=False, padding=0),
14 Panel.fit("Hello, World", padding=0),
15 Panel("Hello, World", width=8, padding=0),
16 Panel(Panel("Hello, World", padding=0), padding=0),
17 Panel("Hello, World", title="FOO", padding=0),
18 Panel("Hello, World", subtitle="FOO", padding=0),
19 ]
20
21 expected = [
22 "╭────────────────────────────────────────────────╮\n│Hello, World │\n╰────────────────────────────────────────────────╯\n",
23 "╭────────────╮\n│Hello, World│\n╰────────────╯\n",
24 "╭────────────╮\n│Hello, World│\n╰────────────╯\n",
25 "╭──────╮\n│Hello,│\n│World │\n╰──────╯\n",
26 "╭────────────────────────────────────────────────╮\n│╭──────────────────────────────────────────────╮│\n││Hello, World ││\n│╰──────────────────────────────────────────────╯│\n╰────────────────────────────────────────────────╯\n",
27 "╭───────────────────── FOO ──────────────────────╮\n│Hello, World │\n╰────────────────────────────────────────────────╯\n",
28 "╭────────────────────────────────────────────────╮\n│Hello, World │\n╰───────────────────── FOO ──────────────────────╯\n",
29 ]
30
31
32 def render(panel, width=50) -> str:
33 console = Console(file=io.StringIO(), width=50, legacy_windows=False)
34 console.print(panel)
35 result = console.file.getvalue()
36 print(result)
37 return result
38
39
40 @pytest.mark.parametrize("panel,expected", zip(tests, expected))
41 def test_render_panel(panel, expected) -> None:
42 assert render(panel) == expected
43
44
45 def test_console_width() -> None:
46 console = Console(file=io.StringIO(), width=50, legacy_windows=False)
47 panel = Panel("Hello, World", expand=False)
48 min_width, max_width = panel.__rich_measure__(console, console.options)
49 assert min_width == 16
50 assert max_width == 16
51
52
53 def test_fixed_width() -> None:
54 console = Console(file=io.StringIO(), width=50, legacy_windows=False)
55 panel = Panel("Hello World", width=20)
56 min_width, max_width = panel.__rich_measure__(console, console.options)
57 assert min_width == 20
58 assert max_width == 20
59
60
61 def test_render_size() -> None:
62 console = Console(width=63, height=46, legacy_windows=False)
63 options = console.options.update_dimensions(80, 4)
64 lines = console.render_lines(Panel("foo", title="Hello"), options=options)
65 print(repr(lines))
66 expected = [
67 [
68 Segment("╭─", Style()),
69 Segment(
70 "────────────────────────────────── Hello ───────────────────────────────────"
71 ),
72 Segment("─╮", Style()),
73 ],
74 [
75 Segment("│", Style()),
76 Segment(" ", Style()),
77 Segment("foo"),
78 Segment(
79 " ",
80 Style(),
81 ),
82 Segment(" ", Style()),
83 Segment("│", Style()),
84 ],
85 [
86 Segment("│", Style()),
87 Segment(" ", Style()),
88 Segment(
89 " ",
90 Style(),
91 ),
92 Segment(" ", Style()),
93 Segment("│", Style()),
94 ],
95 [
96 Segment(
97 "╰──────────────────────────────────────────────────────────────────────────────╯",
98 Style(),
99 )
100 ],
101 ]
102 assert lines == expected
103
104
105 def test_title_text() -> None:
106 panel = Panel(
107 "Hello, World",
108 title=Text("title", style="red"),
109 subtitle=Text("subtitle", style="magenta bold"),
110 )
111 console = Console(
112 file=io.StringIO(),
113 width=50,
114 height=20,
115 legacy_windows=False,
116 force_terminal=True,
117 color_system="truecolor",
118 )
119 console.print(panel)
120
121 result = console.file.getvalue()
122 print(repr(result))
123 expected = "╭────────────────────\x1b[31m title \x1b[0m─────────────────────╮\n│ Hello, World │\n╰───────────────────\x1b[1;35m subtitle \x1b[0m───────────────────╯\n"
124 assert result == expected
125
126
127 def test_title_text_with_border_color() -> None:
128 """Regression test for https://github.com/Textualize/rich/issues/2745"""
129 panel = Panel(
130 "Hello, World",
131 border_style="blue",
132 title=Text("title", style="red"),
133 subtitle=Text("subtitle", style="magenta bold"),
134 )
135 console = Console(
136 file=io.StringIO(),
137 width=50,
138 height=20,
139 legacy_windows=False,
140 force_terminal=True,
141 color_system="truecolor",
142 )
143 console.print(panel)
144
145 result = console.file.getvalue()
146 print(repr(result))
147 expected = "\x1b[34m╭─\x1b[0m\x1b[34m───────────────────\x1b[0m\x1b[31m title \x1b[0m\x1b[34m────────────────────\x1b[0m\x1b[34m─╮\x1b[0m\n\x1b[34m│\x1b[0m Hello, World \x1b[34m│\x1b[0m\n\x1b[34m╰─\x1b[0m\x1b[34m──────────────────\x1b[0m\x1b[1;35m subtitle \x1b[0m\x1b[34m──────────────────\x1b[0m\x1b[34m─╯\x1b[0m\n"
148 assert result == expected
149
150
151 def test_title_text_with_panel_background() -> None:
152 """Regression test for https://github.com/Textualize/rich/issues/3569"""
153 panel = Panel(
154 "Hello, World",
155 style="on blue",
156 title=Text("title", style="red"),
157 subtitle=Text("subtitle", style="magenta bold"),
158 )
159 console = Console(
160 file=io.StringIO(),
161 width=50,
162 height=20,
163 legacy_windows=False,
164 force_terminal=True,
165 color_system="truecolor",
166 )
167 console.print(panel)
168
169 result = console.file.getvalue()
170 print(repr(result))
171 expected = "\x1b[44m╭─\x1b[0m\x1b[44m───────────────────\x1b[0m\x1b[31;44m title \x1b[0m\x1b[44m────────────────────\x1b[0m\x1b[44m─╮\x1b[0m\n\x1b[44m│\x1b[0m\x1b[44m \x1b[0m\x1b[44mHello, World\x1b[0m\x1b[44m \x1b[0m\x1b[44m \x1b[0m\x1b[44m│\x1b[0m\n\x1b[44m╰─\x1b[0m\x1b[44m──────────────────\x1b[0m\x1b[1;35;44m subtitle \x1b[0m\x1b[44m──────────────────\x1b[0m\x1b[44m─╯\x1b[0m\n"
172 assert result == expected
173
174
175 if __name__ == "__main__":
176 expected = []
177 for panel in tests:
178 result = render(panel)
179 print(result)
180 expected.append(result)
181 print("--")
182 print()
183 print(f"expected={repr(expected)}")