Debian Codegraph @

tests/test_markdown_formatter.py

raw symbols SCIP
1 """Tests for the MarkdownFormatter."""
2
3 from textwrap import dedent
4
5 from cyclopts.help import HelpEntry, HelpPanel, MarkdownFormatter
6
7
8 def test_markdown_formatter_empty_panel():
9 """Test that empty panels produce no output."""
10 formatter = MarkdownFormatter()
11 panel = HelpPanel(format="command", title="Commands")
12
13 formatter(None, None, panel)
14 output = formatter.get_output()
15
16 assert output == ""
17
18
19 def test_markdown_formatter_command_panel_table():
20 """Test command panel formatting (always uses list style now)."""
21 formatter = MarkdownFormatter(table_style="table")
22 panel = HelpPanel(
23 format="command",
24 title="Commands",
25 entries=[
26 HelpEntry(positive_names=("serve",), description="Start the server"),
27 HelpEntry(positive_names=("build",), positive_shorts=("-b",), description="Build the project"),
28 ],
29 )
30
31 formatter(None, None, panel)
32 output = formatter.get_output()
33
34 expected = dedent("""\
35 ## Commands
36
37 * `serve`: Start the server
38 * `build (-b)`: Build the project
39
40 """)
41
42 assert output == expected
43
44
45 def test_markdown_formatter_command_panel_list():
46 """Test command panel formatting as a list."""
47 formatter = MarkdownFormatter(table_style="list")
48 panel = HelpPanel(
49 format="command",
50 title="Commands",
51 entries=[
52 HelpEntry(positive_names=("serve",), description="Start the server"),
53 HelpEntry(positive_names=("build",), positive_shorts=("-b",), description="Build the project"),
54 ],
55 )
56
57 formatter(None, None, panel)
58 output = formatter.get_output()
59
60 expected = dedent("""\
61 ## Commands
62
63 * `serve`: Start the server
64 * `build (-b)`: Build the project
65
66 """)
67
68 assert output == expected
69
70
71 def test_markdown_formatter_parameter_panel_table():
72 """Test parameter panel formatting as a table."""
73 formatter = MarkdownFormatter(table_style="table")
74 panel = HelpPanel(
75 format="parameter",
76 title="Parameters",
77 entries=[
78 HelpEntry(
79 positive_names=("--port",),
80 positive_shorts=("-p",),
81 description="Port number",
82 required=True,
83 type="int",
84 default="8080",
85 ),
86 HelpEntry(
87 positive_names=("--verbose",),
88 positive_shorts=("-v",),
89 description="Enable verbose mode",
90 required=False,
91 choices=("true", "false"),
92 ),
93 ],
94 )
95
96 formatter(None, None, panel)
97 output = formatter.get_output()
98
99 expected = dedent("""\
100 ## Parameters
101
102 * `--port, -p`: Port number **[required]** *[default: 8080]*
103 * `--verbose, -v`: Enable verbose mode *[choices: true, false]*
104
105 """)
106
107 assert output == expected
108
109
110 def test_markdown_formatter_parameter_panel_list():
111 """Test parameter panel formatting as a list."""
112 formatter = MarkdownFormatter(table_style="list")
113 panel = HelpPanel(
114 format="parameter",
115 title="Parameters",
116 entries=[
117 HelpEntry(
118 positive_names=("--port",),
119 positive_shorts=("-p",),
120 description="Port number",
121 required=True,
122 type="int",
123 default="8080",
124 ),
125 HelpEntry(
126 positive_names=("--verbose",),
127 description="Enable verbose mode",
128 env_var=("VERBOSE",),
129 ),
130 ],
131 )
132
133 formatter(None, None, panel)
134 output = formatter.get_output()
135
136 expected = dedent("""\
137 ## Parameters
138
139 * `--port, -p`: Port number **[required]** *[default: 8080]*
140 * `--verbose`: Enable verbose mode *[env: VERBOSE]*
141
142 """)
143
144 assert output == expected
145
146
147 def test_markdown_formatter_heading_level():
148 """Test custom heading levels."""
149 formatter = MarkdownFormatter(heading_level=3)
150 panel = HelpPanel(
151 format="command", title="Commands", entries=[HelpEntry(positive_names=("test",), description="Test command")]
152 )
153
154 formatter(None, None, panel)
155 output = formatter.get_output()
156
157 expected = dedent("""\
158 ### Commands
159
160 * `test`: Test command
161
162 """)
163
164 assert output == expected
165
166
167 def test_markdown_formatter_with_panel_description():
168 """Test panel with description."""
169 formatter = MarkdownFormatter()
170 panel = HelpPanel(
171 format="command",
172 title="Commands",
173 description="Available commands for the application",
174 entries=[HelpEntry(positive_names=("test",), description="Test command")],
175 )
176
177 formatter(None, None, panel)
178 output = formatter.get_output()
179
180 expected = dedent("""\
181 ## Commands
182
183 Available commands for the application
184
185 * `test`: Test command
186
187 """)
188
189 assert output == expected
190
191
192 def test_markdown_formatter_render_usage():
193 """Test usage rendering."""
194 formatter = MarkdownFormatter()
195 usage = "myapp [OPTIONS] COMMAND"
196
197 formatter.render_usage(None, None, usage)
198 output = formatter.get_output()
199
200 expected = dedent("""\
201 ```
202 Usage: myapp [OPTIONS] COMMAND
203 ```
204
205 """)
206
207 assert output == expected
208
209
210 def test_markdown_formatter_render_description():
211 """Test description rendering."""
212 formatter = MarkdownFormatter()
213 description = "This is a CLI application for doing awesome things."
214
215 formatter.render_description(None, None, description)
216 output = formatter.get_output()
217
218 expected = dedent("""\
219 This is a CLI application for doing awesome things.
220
221 """)
222
223 assert output == expected
224
225
226 def test_markdown_formatter_reset():
227 """Test that reset clears the output buffer."""
228 formatter = MarkdownFormatter()
229
230 # Add some content
231 formatter.render_description(None, None, "Test description")
232 assert formatter.get_output() != ""
233
234 # Reset and check it's empty
235 formatter.reset()
236 assert formatter.get_output() == ""
237
238
239 def test_markdown_escape_special_characters():
240 """Test that special markdown characters are properly escaped."""
241 from cyclopts._markup import escape_markdown
242
243 # Should escape pipe for table compatibility
244 assert escape_markdown("foo | bar") == "foo \\| bar"
245
246 # Should not escape if it looks like markdown already
247 assert escape_markdown("**bold**") == "**bold**"
248 assert escape_markdown("`code`") == "`code`"
249 assert escape_markdown("[link](url)") == "[link](url)"
250
251 # Should handle empty/None
252 assert escape_markdown("") == ""
253 assert escape_markdown(None) is None
254
255
256 def test_extract_text():
257 """Test text extraction from various objects."""
258 from cyclopts._markup import extract_text
259
260 # Test None
261 assert extract_text(None) == ""
262
263 # Test string
264 assert extract_text("hello world") == "hello world"
265
266 # Test object with __str__
267 class TestObj:
268 def __str__(self):
269 return "test object"
270
271 assert extract_text(TestObj()) == "test object"
272
273
274 def test_parameter_table_with_all_metadata():
275 """Test parameter table with all possible metadata fields."""
276 formatter = MarkdownFormatter(table_style="table")
277 panel = HelpPanel(
278 format="parameter",
279 title="Parameters",
280 entries=[
281 HelpEntry(
282 positive_names=("--config",),
283 positive_shorts=("-c",),
284 description="Configuration file path",
285 required=True,
286 type="Path",
287 choices=("/etc/app.conf", "~/.app.conf", "./app.conf"),
288 env_var=("APP_CONFIG", "CONFIG_PATH"),
289 default="/etc/app.conf",
290 ),
291 ],
292 )
293
294 formatter(None, None, panel)
295 output = formatter.get_output()
296
297 expected = dedent("""\
298 ## Parameters
299
300 * `--config, -c`: Configuration file path **[required]** *[choices: /etc/app.conf, ~/.app.conf, ./app.conf]* *[env: APP_CONFIG, CONFIG_PATH]* *[default: /etc/app.conf]*
301
302 """)
303
304 assert output == expected
305
306
307 def test_parameter_table_no_required_column():
308 """Test parameter table when no parameters are required."""
309 formatter = MarkdownFormatter(table_style="table")
310 panel = HelpPanel(
311 format="parameter",
312 title="Options",
313 entries=[
314 HelpEntry(
315 positive_names=("--debug",),
316 description="Enable debug mode",
317 type="bool",
318 default="False",
319 ),
320 HelpEntry(
321 positive_names=("--quiet",),
322 positive_shorts=("-q",),
323 description="Suppress output",
324 ),
325 ],
326 )
327
328 formatter(None, None, panel)
329 output = formatter.get_output()
330
331 expected = dedent("""\
332 ## Options
333
334 * `--debug`: Enable debug mode *[default: False]*
335 * `--quiet, -q`: Suppress output
336
337 """)
338
339 assert output == expected
340
341
342 def test_parameter_with_no_description():
343 """Test parameters with no description."""
344 formatter = MarkdownFormatter(table_style="table")
345 panel = HelpPanel(
346 format="parameter",
347 title="Parameters",
348 entries=[
349 HelpEntry(
350 positive_names=("--flag",),
351 type="bool",
352 default="True",
353 ),
354 ],
355 )
356
357 formatter(None, None, panel)
358 output = formatter.get_output()
359
360 expected = dedent("""\
361 ## Parameters
362
363 * `--flag`: *[default: True]*
364
365 """)
366
367 assert output == expected
368
369
370 def test_command_table_empty_description():
371 """Test command table with entry having no description."""
372 formatter = MarkdownFormatter(table_style="table")
373 panel = HelpPanel(
374 format="command",
375 title="Commands",
376 entries=[
377 HelpEntry(positive_names=("test",)), # No description
378 HelpEntry(positive_names=("run",), description="Run the application"),
379 ],
380 )
381
382 formatter(None, None, panel)
383 output = formatter.get_output()
384
385 expected = dedent("""\
386 ## Commands
387
388 * `test`:
389 * `run`: Run the application
390
391 """)
392
393 assert output == expected
394
395
396 def test_command_list_with_shorts_and_aliases():
397 """Test command list with short options and aliases in parentheses."""
398 formatter = MarkdownFormatter(table_style="list")
399 panel = HelpPanel(
400 format="command",
401 title="Available Commands",
402 entries=[
403 HelpEntry(
404 positive_names=("serve", "server"),
405 positive_shorts=("s",),
406 description="Start the development server",
407 ),
408 HelpEntry(
409 positive_names=("test",),
410 positive_shorts=("t", "T"),
411 description="Run tests",
412 ),
413 ],
414 )
415
416 formatter(None, None, panel)
417 output = formatter.get_output()
418
419 expected = dedent("""\
420 ## Available Commands
421
422 * `serve (server, s)`: Start the development server
423 * `test (t, T)`: Run tests
424
425 """)
426
427 assert output == expected
428
429
430 def test_multiple_panels_accumulate():
431 """Test that multiple panels accumulate in the formatter."""
432 formatter = MarkdownFormatter()
433
434 # First panel
435 panel1 = HelpPanel(
436 format="command", title="Commands", entries=[HelpEntry(positive_names=("help",), description="Show help")]
437 )
438
439 # Second panel
440 panel2 = HelpPanel(
441 format="parameter",
442 title="Global Options",
443 entries=[
444 HelpEntry(positive_names=("--verbose",), description="Verbose output", type="bool"),
445 ],
446 )
447
448 formatter(None, None, panel1)
449 formatter(None, None, panel2)
450 output = formatter.get_output()
451
452 expected = dedent("""\
453 ## Commands
454
455 * `help`: Show help
456
457 ## Global Options
458
459 * `--verbose`: Verbose output
460
461 """)
462
463 assert output == expected
464
465
466 def test_escape_markdown_in_descriptions():
467 """Test that pipe characters are escaped in table cells."""
468 formatter = MarkdownFormatter(table_style="table")
469 panel = HelpPanel(
470 format="command",
471 title="Commands",
472 entries=[
473 HelpEntry(positive_names=("pipe",), description="Use | to separate values"),
474 ],
475 )
476
477 formatter(None, None, panel)
478 output = formatter.get_output()
479
480 expected = dedent("""\
481 ## Commands
482
483 * `pipe`: Use | to separate values
484
485 """)
486
487 assert output == expected