From a1803cf588877b2a68d5353936a3a74b8955aa59 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 2 Sep 2024 15:17:33 +0200 Subject: [PATCH] markdown formatting tests --- tests/test_rich_markup_mode.py | 186 +++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) diff --git a/tests/test_rich_markup_mode.py b/tests/test_rich_markup_mode.py index d9fe5bae4b..945e10ee2c 100644 --- a/tests/test_rich_markup_mode.py +++ b/tests/test_rich_markup_mode.py @@ -1,3 +1,6 @@ +from typing import List + +import pytest import typer import typer.completion from typer.testing import CliRunner @@ -38,3 +41,186 @@ def main(arg: str): result = runner.invoke(app, ["--help"]) assert any(c in result.stdout for c in rounded) + + +@pytest.mark.parametrize( + "mode,lines", + [ + ("markdown", ["First line", "Line 1 Line 2 Line 3", ""]), + ("rich", ["First line", "Line 1", "", "Line 2", "", "Line 3", ""]), + ("none", ["First line", "Line 1", "Line 2", "Line 3", ""]), + ], +) +def test_markup_mode_newline_pr815(mode: str, lines: List[str]): + app = typer.Typer(rich_markup_mode=mode) + + @app.command() + def main(arg: str): + """First line + + Line 1 + + Line 2 + + Line 3 + """ + print(f"Hello {arg}") + + assert app.rich_markup_mode == mode + + result = runner.invoke(app, ["World"]) + assert "Hello World" in result.stdout + + result = runner.invoke(app, ["--help"]) + result_lines = [line.strip() for line in result.stdout.split("\n")] + assert any(c in result.stdout for c in rounded) + help_start = result_lines.index("First line") + arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] + assert help_start != -1 + assert result_lines[help_start:arg_start] == lines + + +@pytest.mark.parametrize( + "mode,lines", + [ + ( + "markdown", + [ + "This header is just pretty long really", + "Line 1 of a very extremely super long line And a short line 2", + "", + ], + ), + ( + "rich", + [ + "This header is just pretty long really", + "Line 1 of a very", + "extremely super long", + "line", + "", + "And a short line 2", + "", + ], + ), + ( + "none", + [ + "This header is just pretty long really", + "Line 1 of a very extremely super long line", + "And a short line 2", + "", + ], + ), + ], +) +def test_markup_mode_newline_issue447(mode: str, lines: List[str]): + app = typer.Typer(rich_markup_mode=mode) + + @app.command() + def main(arg: str): + """This header + is just + pretty long + really + + Line 1 of a very + extremely super long + line + + And a short line 2 + """ + print(f"Hello {arg}") + + assert app.rich_markup_mode == mode + + result = runner.invoke(app, ["World"]) + assert "Hello World" in result.stdout + + result = runner.invoke(app, ["--help"]) + result_lines = [line.strip() for line in result.stdout.split("\n")] + assert any(c in result.stdout for c in rounded) + help_start = [i for i, row in enumerate(result_lines) if "This header" in row][0] + arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] + assert help_start != -1 + assert result_lines[help_start:arg_start] == lines + + +@pytest.mark.parametrize( + "mode,lines", + [ + pytest.param( + "markdown", + ["First line", "", "• 1", "• 2", "• 3", ""], + marks=pytest.mark.xfail(), + ), + ("rich", ["First line", "- 1", "- 2", "- 3", ""]), + ("none", ["First line", "- 1 - 2 - 3", ""]), + ], +) +def test_markup_mode_bullets(mode: str, lines: List[str]): + app = typer.Typer(rich_markup_mode=mode) + + @app.command() + def main(arg: str): + """First line + + - 1 + - 2 + - 3 + """ + print(f"Hello {arg}") + + assert app.rich_markup_mode == mode + + result = runner.invoke(app, ["World"]) + assert "Hello World" in result.stdout + + result = runner.invoke(app, ["--help"]) + result_lines = [line.strip() for line in result.stdout.split("\n")] + assert any(c in result.stdout for c in rounded) + help_start = result_lines.index("First line") + arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] + assert help_start != -1 + assert result_lines[help_start:arg_start] == lines + + +@pytest.mark.parametrize( + "mode,lines", + [ + pytest.param( + "markdown", + ["First line", "", "• 1", "• 2", "• a", "• b", "• 3", ""], + marks=pytest.mark.xfail(), + ), + ("rich", ["First line", "- 1", "- 2", "- a", "- b", "- 3", ""]), + ("none", ["First line", "- 1 - 2 - a - b - 3", ""]), + ], +) +def test_markup_mode_nested_bullets(mode: str, lines: List[str]): + app = typer.Typer(rich_markup_mode=mode) + + @app.command() + def main(arg: str): + """First line + + - 1 + - 2 + - a + - b + - 3 + """ + print(f"Hello {arg}") + + assert app.rich_markup_mode == mode + + result = runner.invoke(app, ["World"]) + assert "Hello World" in result.stdout + + result = runner.invoke(app, ["--help"]) + result_lines = [line.strip() for line in result.stdout.split("\n")] + assert any(c in result.stdout for c in rounded) + help_start = result_lines.index("First line") + arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] + assert help_start != -1 + assert result_lines[help_start:arg_start] == lines