Coverage for tests / test_rich_markup_mode.py: 100%
115 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-09 12:36 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-09 12:36 +0000
1import pytest 1abcdefgh
2import typer 1abcdefgh
3import typer.completion 1abcdefgh
4from typer.testing import CliRunner 1abcdefgh
6runner = CliRunner() 1abcdefgh
7rounded = ["╭", "─", "┬", "╮", "│", "├", "┼", "┤", "╰", "┴", "╯"] 1abcdefgh
10def test_rich_markup_mode_none(): 1abcdefgh
11 app = typer.Typer(rich_markup_mode=None) 1abcdefgh
13 @app.command() 1abcdefgh
14 def main(arg: str): 1abcdefgh
15 """Main function"""
16 print(f"Hello {arg}") 1abcdefgh
18 assert app.rich_markup_mode is None 1abcdefgh
20 result = runner.invoke(app, ["World"]) 1abcdefgh
21 assert "Hello World" in result.stdout 1abcdefgh
23 result = runner.invoke(app, ["--help"]) 1abcdefgh
24 assert "ARG [required]" in result.stdout 1abcdefgh
25 assert all(c not in result.stdout for c in rounded) 1abcdefgh
28def test_rich_markup_mode_rich(): 1abcdefgh
29 app = typer.Typer(rich_markup_mode="rich") 1abcdefgh
31 @app.command() 1abcdefgh
32 def main(arg: str): 1abcdefgh
33 """Main function"""
34 print(f"Hello {arg}") 1abcdefgh
36 assert app.rich_markup_mode == "rich" 1abcdefgh
38 result = runner.invoke(app, ["World"]) 1abcdefgh
39 assert "Hello World" in result.stdout 1abcdefgh
41 result = runner.invoke(app, ["--help"]) 1abcdefgh
42 assert any(c in result.stdout for c in rounded) 1abcdefgh
45@pytest.mark.parametrize( 1abcdefgh
46 "mode,lines",
47 [
48 pytest.param(
49 "markdown",
50 ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""],
51 ),
52 pytest.param(
53 "rich", ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""]
54 ),
55 pytest.param(
56 None, ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""]
57 ),
58 ],
59)
60def test_markup_mode_newline_pr815(mode: str, lines: list[str]): 1abcdefgh
61 app = typer.Typer(rich_markup_mode=mode) 1abcdefgh
63 @app.command() 1abcdefgh
64 def main(arg: str): 1abcdefgh
65 """First line
67 Line 1
69 Line 2
71 Line 3
72 """
73 print(f"Hello {arg}") 1abcdefgh
75 assert app.rich_markup_mode == mode 1abcdefgh
77 result = runner.invoke(app, ["World"]) 1abcdefgh
78 assert "Hello World" in result.stdout 1abcdefgh
80 result = runner.invoke(app, ["--help"]) 1abcdefgh
81 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefgh
82 if mode: 1abcdefgh
83 assert any(c in result.stdout for c in rounded) 1abcdefgh
84 help_start = result_lines.index("First line") 1abcdefgh
85 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefgh
86 assert help_start != -1 1abcdefgh
87 assert result_lines[help_start:arg_start] == lines 1abcdefgh
90@pytest.mark.parametrize( 1abcdefgh
91 "mode,lines",
92 [
93 pytest.param("markdown", ["First line", "", "Line 1 Line 2 Line 3", ""]),
94 pytest.param("rich", ["First line", "", "Line 1", "Line 2", "Line 3", ""]),
95 pytest.param(None, ["First line", "", "Line 1 Line 2 Line 3", ""]),
96 ],
97)
98def test_markup_mode_newline_issue447(mode: str, lines: list[str]): 1abcdefgh
99 app = typer.Typer(rich_markup_mode=mode) 1abcdefgh
101 @app.command() 1abcdefgh
102 def main(arg: str): 1abcdefgh
103 """First line
105 Line 1
106 Line 2
107 Line 3
108 """
109 print(f"Hello {arg}") 1abcdefgh
111 assert app.rich_markup_mode == mode 1abcdefgh
113 result = runner.invoke(app, ["World"]) 1abcdefgh
114 assert "Hello World" in result.stdout 1abcdefgh
116 result = runner.invoke(app, ["--help"]) 1abcdefgh
117 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefgh
118 if mode: 1abcdefgh
119 assert any(c in result.stdout for c in rounded) 1abcdefgh
120 help_start = result_lines.index("First line") 1abcdefgh
121 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefgh
122 assert help_start != -1 1abcdefgh
123 assert result_lines[help_start:arg_start] == lines 1abcdefgh
126@pytest.mark.parametrize( 1abcdefgh
127 "mode,lines",
128 [
129 pytest.param(
130 "markdown",
131 [
132 "This header is long",
133 "",
134 "Line 1",
135 "",
136 "Line 2 continues here",
137 "",
138 "Line 3",
139 "",
140 ],
141 ),
142 pytest.param(
143 "rich",
144 [
145 "This header is long",
146 "",
147 "Line 1",
148 "",
149 "Line 2",
150 "continues here",
151 "",
152 "Line 3",
153 "",
154 ],
155 ),
156 pytest.param(
157 None,
158 [
159 "This header is long",
160 "",
161 "Line 1",
162 "",
163 "Line 2 continues here",
164 "",
165 "Line 3",
166 "",
167 ],
168 ),
169 ],
170)
171def test_markup_mode_newline_mixed(mode: str, lines: list[str]): 1abcdefgh
172 app = typer.Typer(rich_markup_mode=mode) 1abcdefgh
174 @app.command() 1abcdefgh
175 def main(arg: str): 1abcdefgh
176 """This header
177 is long
179 Line 1
181 Line 2
182 continues here
184 Line 3
185 """
186 print(f"Hello {arg}") 1abcdefgh
188 assert app.rich_markup_mode == mode 1abcdefgh
190 result = runner.invoke(app, ["World"]) 1abcdefgh
191 assert "Hello World" in result.stdout 1abcdefgh
193 result = runner.invoke(app, ["--help"]) 1abcdefgh
194 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefgh
195 if mode: 1abcdefgh
196 assert any(c in result.stdout for c in rounded) 1abcdefgh
197 help_start = [i for i, row in enumerate(result_lines) if "This header" in row][0] 1abcdefgh
198 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefgh
199 assert help_start != -1 1abcdefgh
200 assert result_lines[help_start:arg_start] == lines 1abcdefgh
203@pytest.mark.parametrize( 1abcdefgh
204 "mode,lines",
205 [
206 pytest.param(
207 "markdown",
208 ["First line", "", "• 1", "• 2", "• 3", ""],
209 marks=pytest.mark.xfail,
210 ),
211 pytest.param("rich", ["First line", "", "- 1", "- 2", "- 3", ""]),
212 pytest.param(None, ["First line", "", "- 1 - 2 - 3", ""]),
213 ],
214)
215def test_markup_mode_bullets_single_newline(mode: str, lines: list[str]): 1abcdefgh
216 app = typer.Typer(rich_markup_mode=mode) 1abcdefgh
218 @app.command() 1abcdefgh
219 def main(arg: str): 1abcdefgh
220 """First line
222 - 1
223 - 2
224 - 3
225 """
226 print(f"Hello {arg}") 1abcdefgh
228 assert app.rich_markup_mode == mode 1abcdefgh
230 result = runner.invoke(app, ["World"]) 1abcdefgh
231 assert "Hello World" in result.stdout 1abcdefgh
233 result = runner.invoke(app, ["--help"]) 1abcdefgh
234 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefgh
235 if mode: 1abcdefgh
236 assert any(c in result.stdout for c in rounded) 1abcdefgh
237 help_start = result_lines.index("First line") 1abcdefgh
238 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefgh
239 assert help_start != -1 1abcdefgh
240 assert result_lines[help_start:arg_start] == lines 1abcdefgh
243@pytest.mark.parametrize( 1abcdefgh
244 "mode,lines",
245 [
246 pytest.param(
247 "markdown",
248 ["First line", "", "• 1", "• 2", "• 3", ""],
249 marks=pytest.mark.xfail,
250 ),
251 (
252 "rich",
253 ["First line", "", "- 1", "", "- 2", "", "- 3", ""],
254 ),
255 (None, ["First line", "", "- 1", "", "- 2", "", "- 3", ""]),
256 ],
257)
258def test_markup_mode_bullets_double_newline(mode: str, lines: list[str]): 1abcdefgh
259 app = typer.Typer(rich_markup_mode=mode) 1abcdefgh
261 @app.command() 1abcdefgh
262 def main(arg: str): 1abcdefgh
263 """First line
265 - 1
267 - 2
269 - 3
270 """
271 print(f"Hello {arg}") 1abcdefgh
273 assert app.rich_markup_mode == mode 1abcdefgh
275 result = runner.invoke(app, ["World"]) 1abcdefgh
276 assert "Hello World" in result.stdout 1abcdefgh
278 result = runner.invoke(app, ["--help"]) 1abcdefgh
279 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefgh
280 if mode: 1abcdefgh
281 assert any(c in result.stdout for c in rounded) 1abcdefgh
282 help_start = result_lines.index("First line") 1abcdefgh
283 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefgh
284 assert help_start != -1 1abcdefgh
285 assert result_lines[help_start:arg_start] == lines 1abcdefgh
288def test_markup_mode_default(): 1abcdefgh
289 # We're assuming the test suite is run with rich installed
290 app = typer.Typer() 1abcdefgh
291 assert app.rich_markup_mode == "rich" 1abcdefgh