python-click/tests/test_defaults.py

41 lines
1.1 KiB
Python
Raw Normal View History

2014-10-16 20:40:34 +02:00
import click
def test_basic_defaults(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--foo", default=42, type=click.FLOAT)
2014-10-16 20:40:34 +02:00
def cli(foo):
assert type(foo) is float
2021-10-10 03:31:57 +02:00
click.echo(f"FOO:[{foo}]")
2014-10-16 20:40:34 +02:00
result = runner.invoke(cli, [])
assert not result.exception
2020-07-21 08:23:42 +02:00
assert "FOO:[42.0]" in result.output
2014-10-16 20:40:34 +02:00
def test_multiple_defaults(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--foo", default=[23, 42], type=click.FLOAT, multiple=True)
2014-10-16 20:40:34 +02:00
def cli(foo):
for item in foo:
assert type(item) is float
click.echo(item)
result = runner.invoke(cli, [])
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output.splitlines() == ["23.0", "42.0"]
2014-10-16 20:40:34 +02:00
def test_nargs_plus_multiple(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option(
"--arg", default=((1, 2), (3, 4)), nargs=2, multiple=True, type=click.INT
)
2014-10-16 20:40:34 +02:00
def cli(arg):
2021-10-10 03:31:57 +02:00
for a, b in arg:
click.echo(f"<{a:d}|{b:d}>")
2014-10-16 20:40:34 +02:00
result = runner.invoke(cli, [])
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output.splitlines() == ["<1|2>", "<3|4>"]