Skip to content
Myr

test_annotated.py

This file

Path

tests/test_annotated.py

78 lines 路 2.0 KiB 路 Python

Last changed by

Not known.

History of this file

Take it

1import typer2from typer.testing import CliRunner3from typing_extensions import Annotated45from .utils import needs_py31067runner = CliRunner()8910def test_annotated_argument_with_default():11    app = typer.Typer()1213    @app.command()14    def cmd(val: Annotated[int, typer.Argument()] = 0):15        print(f"hello {val}")1617    result = runner.invoke(app)18    assert result.exit_code == 0, result.output19    assert "hello 0" in result.output2021    result = runner.invoke(app, ["42"])22    assert result.exit_code == 0, result.output23    assert "hello 42" in result.output242526@needs_py31027def test_annotated_argument_in_string_type_with_default():28    app = typer.Typer()2930    @app.command()31    def cmd(val: "Annotated[int, typer.Argument()]" = 0):32        print(f"hello {val}")3334    result = runner.invoke(app)35    assert result.exit_code == 0, result.output36    assert "hello 0" in result.output3738    result = runner.invoke(app, ["42"])39    assert result.exit_code == 0, result.output40    assert "hello 42" in result.output414243def test_annotated_argument_with_default_factory():44    app = typer.Typer()4546    def make_string():47        return "I made it"4849    @app.command()50    def cmd(val: Annotated[str, typer.Argument(default_factory=make_string)]):51        print(val)5253    result = runner.invoke(app)54    assert result.exit_code == 0, result.output55    assert "I made it" in result.output5657    result = runner.invoke(app, ["overridden"])58    assert result.exit_code == 0, result.output59    assert "overridden" in result.output606162def test_annotated_option_with_argname_doesnt_mutate_multiple_calls():63    app = typer.Typer()6465    @app.command()66    def cmd(force: Annotated[bool, typer.Option("--force")] = False):67        if force:68            print("Forcing operation")69        else:70            print("Not forcing")7172    result = runner.invoke(app)73    assert result.exit_code == 0, result.output74    assert "Not forcing" in result.output7576    result = runner.invoke(app, ["--force"])77    assert result.exit_code == 0, result.output78    assert "Forcing operation" in result.output