test_types.py
This file
Path
tests/test_types.py
121 lines · 3.5 KiB · Python
Last changed by
➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
History of this fileTake 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(names: list[str] = typer.Argument(["World"], envvar="NAMES")) -> None:35 for name in names:36 print(f"Hello {name}!")373839@app.command()40def split_variadic_and_pair(items: list[str], pair: tuple[str, str]) -> None:41 print(f"items={items}")42 print(f"pair={pair}")434445runner = CliRunner()464748def test_enum_choice() -> None:49 result = runner.invoke(50 app, ["hello-option", "--name", "morty"], catch_exceptions=False51 )52 assert result.exit_code == 053 assert "Hello Morty!" in result.output5455 result = runner.invoke(app, ["hello-option", "--name", "Rick"])56 assert result.exit_code == 057 assert "Hello Rick!" in result.output5859 result = runner.invoke(app, ["hello-option", "--name", "RICK"])60 assert result.exit_code == 061 assert "Hello Rick!" in result.output6263 result = runner.invoke(app, ["hello-no-choices", "--name", "RICK"])64 assert result.exit_code == 065 assert "Hello Rick!" in result.output6667 result = runner.invoke(app, ["hello-argument", "RICK"])68 assert result.exit_code == 069 assert "Hello Rick!" in result.output707172def test_enum_choice_repr() -> None:73 root_command = typer.main.get_command(app)74 command = root_command.commands["hello-option"]75 name_param = next(param for param in command.params if param.name == "name")76 assert repr(name_param.type).startswith("Choice([")777879def test_enum_choice_help() -> None:80 result = runner.invoke(app, ["hello-argument", "--help"])81 assert result.exit_code == 082 assert "{rick|morty}" in result.output8384 result = runner.invoke(app, ["hello-option", "--help"])85 assert result.exit_code == 086 assert "[rick|morty]" in result.output8788 result = runner.invoke(app, ["hello-no-choices", "--help"])89 assert result.exit_code == 090 assert "--name" in result.output91 assert "rick|morty" not in result.output929394def test_enum_choice_missing_message() -> None:95 result = runner.invoke(app, ["hello-argument"])96 assert result.exit_code != 097 assert "Missing argument" in result.output98 assert "Choose from:" in result.output99 assert "rick" in result.output100 assert "morty" in result.output101102103def test_split_envvar_value(monkeypatch) -> None:104 # This will use split_envvar_value to produce two strings from the envvar105 monkeypatch.setenv("NAMES", "Rick Morty")106 result = runner.invoke(app, ["hello-all"])107 assert result.exit_code == 0108 assert "Hello Rick!" in result.output109 assert "Hello Morty!" in result.output110111112def test_list_pair() -> None:113 result = runner.invoke(app, ["split-variadic-and-pair", "a", "b", "c", "x", "y"])114 assert result.exit_code == 0115 assert "items=['a', 'b', 'c']" in result.output116 assert "pair=('x', 'y')" in result.output117118119def test_float_range_open_bounds_with_clamp_not_allowed():120 with pytest.raises(TypeError, match="Clamping is not supported for open bounds."):121 _click.types.FloatRange(min=0.0, min_open=True, clamp=True)