Skip to content
Myr

test_types.py

This file

Path

tests/test_types.py

165 lines 路 4.7 KiB 路 Python

Last changed by

馃悰 Ensure that the default of a list argument is used correctly (#1821)Sofie Van Landeghembefore adoption06fce7d

History of this file

Take it

1from enum import Enum23import pytest4import typer5from typer import _click6from typer.testing import CliRunner78app = typer.Typer(context_settings={"token_normalize_func": str.lower})91011class User(str, Enum):12    rick = "Rick"13    morty = "Morty"141516@app.command()17def hello_option(name: User = User.rick) -> None:18    print(f"Hello {name.value}!")192021@app.command()22def hello_argument(name: User) -> None:23    print(f"Hello {name.value}!")242526@app.command()27def hello_no_choices(28    name: User = typer.Option(..., "--name", show_choices=False),29):30    print(f"Hello {name.value}!")313233@app.command()34def hello_all_options(name: list[str] = typer.Option(["World"])) -> None:35    for n in name:36        print(f"Hello {n}!")373839@app.command()40def hello_all_args(names: list[str] = typer.Argument(["World"])) -> None:41    for name in names:42        print(f"Hello {name}!")434445@app.command()46def hello_all_envvar(47    names: list[str] = typer.Argument(["World"], envvar="NAMES"),48) -> None:49    for name in names:50        print(f"Hello {name}!")515253@app.command()54def split_variadic_and_pair(items: list[str], pair: tuple[str, str]) -> None:55    print(f"items={items}")56    print(f"pair={pair}")575859runner = CliRunner()606162def test_enum_choice() -> None:63    result = runner.invoke(64        app, ["hello-option", "--name", "morty"], catch_exceptions=False65    )66    assert result.exit_code == 067    assert "Hello Morty!" in result.output6869    result = runner.invoke(app, ["hello-option", "--name", "Rick"])70    assert result.exit_code == 071    assert "Hello Rick!" in result.output7273    result = runner.invoke(app, ["hello-option", "--name", "RICK"])74    assert result.exit_code == 075    assert "Hello Rick!" in result.output7677    result = runner.invoke(app, ["hello-no-choices", "--name", "RICK"])78    assert result.exit_code == 079    assert "Hello Rick!" in result.output8081    result = runner.invoke(app, ["hello-argument", "RICK"])82    assert result.exit_code == 083    assert "Hello Rick!" in result.output848586def test_enum_choice_repr() -> None:87    root_command = typer.main.get_command(app)88    command = root_command.commands["hello-option"]89    name_param = next(param for param in command.params if param.name == "name")90    assert repr(name_param.type).startswith("Choice([")919293def test_enum_choice_help() -> None:94    result = runner.invoke(app, ["hello-argument", "--help"])95    assert result.exit_code == 096    assert "{rick|morty}" in result.output9798    result = runner.invoke(app, ["hello-option", "--help"])99    assert result.exit_code == 0100    assert "[rick|morty]" in result.output101102    result = runner.invoke(app, ["hello-no-choices", "--help"])103    assert result.exit_code == 0104    assert "--name" in result.output105    assert "rick|morty" not in result.output106107108def test_enum_choice_missing_message() -> None:109    result = runner.invoke(app, ["hello-argument"])110    assert result.exit_code != 0111    assert "Missing argument" in result.output112    assert "Choose from:" in result.output113    assert "rick" in result.output114    assert "morty" in result.output115116117def test_list() -> None:118    result = runner.invoke(119        app, ["hello-all-options", "--name", "Rick", "--name", "Morty"]120    )121    assert result.exit_code == 0122    assert "Hello World!" not in result.output123    assert "Hello Rick!" in result.output124    assert "Hello Morty!" in result.output125126    result = runner.invoke(app, ["hello-all-args", "Rick", "Morty"])127    assert result.exit_code == 0128    assert "Hello World!" not in result.output129    assert "Hello Rick!" in result.output130    assert "Hello Morty!" in result.output131132133def test_list_empty() -> None:134    result = runner.invoke(app, ["hello-all-args"])135    assert result.exit_code == 0136    assert "Hello World!" in result.output137138    result = runner.invoke(app, ["hello-all-options"])139    assert result.exit_code == 0140    assert "Hello World!" in result.output141142    result = runner.invoke(app, ["hello-all-envvar"])143    assert result.exit_code == 0144    assert "Hello World!" in result.output145146147def test_split_envvar_value(monkeypatch) -> None:148    # This will use split_envvar_value to produce two strings from the envvar149    monkeypatch.setenv("NAMES", "Rick   Morty")150    result = runner.invoke(app, ["hello-all-envvar"])151    assert result.exit_code == 0152    assert "Hello Rick!" in result.output153    assert "Hello Morty!" in result.output154155156def test_list_pair() -> None:157    result = runner.invoke(app, ["split-variadic-and-pair", "a", "b", "c", "x", "y"])158    assert result.exit_code == 0159    assert "items=['a', 'b', 'c']" in result.output160    assert "pair=('x', 'y')" in result.output161162163def test_float_range_open_bounds_with_clamp_not_allowed():164    with pytest.raises(TypeError, match="Clamping is not supported for open bounds."):165        _click.types.FloatRange(min=0.0, min_open=True, clamp=True)