|
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 | from rich.console import Console |
| 7 | +from typing_extensions import Annotated |
7 | 8 |
|
8 | 9 | import cappa |
9 | | -from tests.utils import Backend, backends, parse |
| 10 | +from tests.utils import ( |
| 11 | + Backend, |
| 12 | + backends, |
| 13 | + parse, |
| 14 | + strip_trailing_whitespace, |
| 15 | + terminal_width, |
| 16 | +) |
10 | 17 |
|
11 | 18 |
|
12 | 19 | @pytest.mark.help |
@@ -38,3 +45,57 @@ class Args: |
38 | 45 | assert "This\x1b[0m" in result.out |
39 | 46 | assert " \x1b[1mis\x1b[0m" in result.out # typos: ignore |
40 | 47 | assert " \x1b[3mneat\x1b[0m" in result.out |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.help |
| 51 | +@backends |
| 52 | +def test_multi_paragraph_arg_help_preserves_paragraph_breaks( |
| 53 | + backend: Backend, capsys: Any |
| 54 | +): |
| 55 | + @dataclass |
| 56 | + class Args: |
| 57 | + foo: Annotated[ |
| 58 | + str, |
| 59 | + cappa.Arg( |
| 60 | + help="First paragraph summary.\n\nSecond paragraph body.", |
| 61 | + ), |
| 62 | + ] |
| 63 | + |
| 64 | + with terminal_width(80), pytest.raises(cappa.Exit): |
| 65 | + parse(Args, "--help", backend=backend) |
| 66 | + |
| 67 | + out = strip_trailing_whitespace(capsys.readouterr().out) |
| 68 | + |
| 69 | + assert "First paragraph summary." in out |
| 70 | + assert "Second paragraph body." in out |
| 71 | + |
| 72 | + summary_pos = out.index("First paragraph summary.") |
| 73 | + body_pos = out.index("Second paragraph body.") |
| 74 | + between = out[summary_pos + len("First paragraph summary.") : body_pos] |
| 75 | + assert "\n\n" in between |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.help |
| 79 | +@backends |
| 80 | +def test_soft_line_breaks_fold_into_spaces(backend: Backend, capsys: Any): |
| 81 | + @dataclass |
| 82 | + class SoftBreakArgs: |
| 83 | + foo: str |
| 84 | + """ |
| 85 | + This is a long sentence that goes on and on. |
| 86 | + This second soft-break line continues the same paragraph. |
| 87 | + """ |
| 88 | + |
| 89 | + with terminal_width(80), pytest.raises(cappa.Exit): |
| 90 | + parse(SoftBreakArgs, "--help", backend=backend, completion=False) |
| 91 | + |
| 92 | + out = strip_trailing_whitespace(capsys.readouterr().out) |
| 93 | + |
| 94 | + # Words from adjacent soft-break lines must be separated by a space |
| 95 | + assert "on.This" not in out |
| 96 | + assert "on. This" in out |
| 97 | + |
| 98 | + # And must not have a blank line between them (same paragraph) |
| 99 | + first = out.index("goes on and on.") |
| 100 | + second = out.index("same paragraph.") |
| 101 | + assert "\n\n" not in out[first:second] |
0 commit comments