Coverage for tests/test_rich_markup_mode.py: 100%
115 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-19 20:29 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-19 20:29 +0000
1from typing import List 1abcdefghi
3import pytest 1abcdefghi
4import typer 1abcdefghi
5import typer.completion 1abcdefghi
6from typer.testing import CliRunner 1abcdefghi
8runner = CliRunner() 1abcdefghi
9rounded = ["╭", "─", "┬", "╮", "│", "├", "┼", "┤", "╰", "┴", "╯"] 1abcdefghi
12def test_rich_markup_mode_none(): 1abcdefghi
13 app = typer.Typer(rich_markup_mode=None) 1abcdefghi
15 @app.command() 1abcdefghi
16 def main(arg: str): 1abcdefghi
17 """Main function"""
18 print(f"Hello {arg}") 1abcdefghi
20 assert app.rich_markup_mode is None 1abcdefghi
22 result = runner.invoke(app, ["World"]) 1abcdefghi
23 assert "Hello World" in result.stdout 1abcdefghi
25 result = runner.invoke(app, ["--help"]) 1abcdefghi
26 assert all(c not in result.stdout for c in rounded) 1abcdefghi
29def test_rich_markup_mode_rich(): 1abcdefghi
30 app = typer.Typer(rich_markup_mode="rich") 1abcdefghi
32 @app.command() 1abcdefghi
33 def main(arg: str): 1abcdefghi
34 """Main function"""
35 print(f"Hello {arg}") 1abcdefghi
37 assert app.rich_markup_mode == "rich" 1abcdefghi
39 result = runner.invoke(app, ["World"]) 1abcdefghi
40 assert "Hello World" in result.stdout 1abcdefghi
42 result = runner.invoke(app, ["--help"]) 1abcdefghi
43 assert any(c in result.stdout for c in rounded) 1abcdefghi
46@pytest.mark.parametrize( 1abcdefghi
47 "mode,lines",
48 [
49 pytest.param(
50 "markdown",
51 ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""],
52 ),
53 pytest.param(
54 "rich", ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""]
55 ),
56 pytest.param(
57 None, ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""]
58 ),
59 ],
60)
61def test_markup_mode_newline_pr815(mode: str, lines: List[str]): 1abcdefghi
62 app = typer.Typer(rich_markup_mode=mode) 1abcdefghi
64 @app.command() 1abcdefghi
65 def main(arg: str): 1abcdefghi
66 """First line
68 Line 1
70 Line 2
72 Line 3
73 """
74 print(f"Hello {arg}") 1abcdefghi
76 assert app.rich_markup_mode == mode 1abcdefghi
78 result = runner.invoke(app, ["World"]) 1abcdefghi
79 assert "Hello World" in result.stdout 1abcdefghi
81 result = runner.invoke(app, ["--help"]) 1abcdefghi
82 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefghi
83 if mode: 1abcdefghi
84 assert any(c in result.stdout for c in rounded) 1abcdefghi
85 help_start = result_lines.index("First line") 1abcdefghi
86 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefghi
87 assert help_start != -1 1abcdefghi
88 assert result_lines[help_start:arg_start] == lines 1abcdefghi
91@pytest.mark.parametrize( 1abcdefghi
92 "mode,lines",
93 [
94 pytest.param("markdown", ["First line", "", "Line 1 Line 2 Line 3", ""]),
95 pytest.param("rich", ["First line", "", "Line 1", "Line 2", "Line 3", ""]),
96 pytest.param(None, ["First line", "", "Line 1 Line 2 Line 3", ""]),
97 ],
98)
99def test_markup_mode_newline_issue447(mode: str, lines: List[str]): 1abcdefghi
100 app = typer.Typer(rich_markup_mode=mode) 1abcdefghi
102 @app.command() 1abcdefghi
103 def main(arg: str): 1abcdefghi
104 """First line
106 Line 1
107 Line 2
108 Line 3
109 """
110 print(f"Hello {arg}") 1abcdefghi
112 assert app.rich_markup_mode == mode 1abcdefghi
114 result = runner.invoke(app, ["World"]) 1abcdefghi
115 assert "Hello World" in result.stdout 1abcdefghi
117 result = runner.invoke(app, ["--help"]) 1abcdefghi
118 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefghi
119 if mode: 1abcdefghi
120 assert any(c in result.stdout for c in rounded) 1abcdefghi
121 help_start = result_lines.index("First line") 1abcdefghi
122 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefghi
123 assert help_start != -1 1abcdefghi
124 assert result_lines[help_start:arg_start] == lines 1abcdefghi
127@pytest.mark.parametrize( 1abcdefghi
128 "mode,lines",
129 [
130 pytest.param(
131 "markdown",
132 [
133 "This header is long",
134 "",
135 "Line 1",
136 "",
137 "Line 2 continues here",
138 "",
139 "Line 3",
140 "",
141 ],
142 ),
143 pytest.param(
144 "rich",
145 [
146 "This header is long",
147 "",
148 "Line 1",
149 "",
150 "Line 2",
151 "continues here",
152 "",
153 "Line 3",
154 "",
155 ],
156 ),
157 pytest.param(
158 None,
159 [
160 "This header is long",
161 "",
162 "Line 1",
163 "",
164 "Line 2 continues here",
165 "",
166 "Line 3",
167 "",
168 ],
169 ),
170 ],
171)
172def test_markup_mode_newline_mixed(mode: str, lines: List[str]): 1abcdefghi
173 app = typer.Typer(rich_markup_mode=mode) 1abcdefghi
175 @app.command() 1abcdefghi
176 def main(arg: str): 1abcdefghi
177 """This header
178 is long
180 Line 1
182 Line 2
183 continues here
185 Line 3
186 """
187 print(f"Hello {arg}") 1abcdefghi
189 assert app.rich_markup_mode == mode 1abcdefghi
191 result = runner.invoke(app, ["World"]) 1abcdefghi
192 assert "Hello World" in result.stdout 1abcdefghi
194 result = runner.invoke(app, ["--help"]) 1abcdefghi
195 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefghi
196 if mode: 1abcdefghi
197 assert any(c in result.stdout for c in rounded) 1abcdefghi
198 help_start = [i for i, row in enumerate(result_lines) if "This header" in row][0] 1abcdefghi
199 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefghi
200 assert help_start != -1 1abcdefghi
201 assert result_lines[help_start:arg_start] == lines 1abcdefghi
204@pytest.mark.parametrize( 1abcdefghi
205 "mode,lines",
206 [
207 pytest.param(
208 "markdown",
209 ["First line", "", "• 1", "• 2", "• 3", ""],
210 marks=pytest.mark.xfail,
211 ),
212 pytest.param("rich", ["First line", "", "- 1", "- 2", "- 3", ""]),
213 pytest.param(None, ["First line", "", "- 1 - 2 - 3", ""]),
214 ],
215)
216def test_markup_mode_bullets_single_newline(mode: str, lines: List[str]): 1abcdefghi
217 app = typer.Typer(rich_markup_mode=mode) 1abcdefghi
219 @app.command() 1abcdefghi
220 def main(arg: str): 1abcdefghi
221 """First line
223 - 1
224 - 2
225 - 3
226 """
227 print(f"Hello {arg}") 1abcdefghi
229 assert app.rich_markup_mode == mode 1abcdefghi
231 result = runner.invoke(app, ["World"]) 1abcdefghi
232 assert "Hello World" in result.stdout 1abcdefghi
234 result = runner.invoke(app, ["--help"]) 1abcdefghi
235 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefghi
236 if mode: 1abcdefghi
237 assert any(c in result.stdout for c in rounded) 1abcdefghi
238 help_start = result_lines.index("First line") 1abcdefghi
239 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefghi
240 assert help_start != -1 1abcdefghi
241 assert result_lines[help_start:arg_start] == lines 1abcdefghi
244@pytest.mark.parametrize( 1abcdefghi
245 "mode,lines",
246 [
247 pytest.param(
248 "markdown",
249 ["First line", "", "• 1", "• 2", "• 3", ""],
250 marks=pytest.mark.xfail,
251 ),
252 (
253 "rich",
254 ["First line", "", "- 1", "", "- 2", "", "- 3", ""],
255 ),
256 (None, ["First line", "", "- 1", "", "- 2", "", "- 3", ""]),
257 ],
258)
259def test_markup_mode_bullets_double_newline(mode: str, lines: List[str]): 1abcdefghi
260 app = typer.Typer(rich_markup_mode=mode) 1abcdefghi
262 @app.command() 1abcdefghi
263 def main(arg: str): 1abcdefghi
264 """First line
266 - 1
268 - 2
270 - 3
271 """
272 print(f"Hello {arg}") 1abcdefghi
274 assert app.rich_markup_mode == mode 1abcdefghi
276 result = runner.invoke(app, ["World"]) 1abcdefghi
277 assert "Hello World" in result.stdout 1abcdefghi
279 result = runner.invoke(app, ["--help"]) 1abcdefghi
280 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefghi
281 if mode: 1abcdefghi
282 assert any(c in result.stdout for c in rounded) 1abcdefghi
283 help_start = result_lines.index("First line") 1abcdefghi
284 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefghi
285 assert help_start != -1 1abcdefghi
286 assert result_lines[help_start:arg_start] == lines 1abcdefghi
289def test_markup_mode_default(): 1abcdefghi
290 # We're assuming the test suite is run with rich installed
291 app = typer.Typer() 1abcdefghi
292 assert app.rich_markup_mode == "rich" 1abcdefghi