test_defaults.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import click
  2. def test_basic_defaults(runner):
  3. @click.command()
  4. @click.option("--foo", default=42, type=click.FLOAT)
  5. def cli(foo):
  6. assert type(foo) is float
  7. click.echo(f"FOO:[{foo}]")
  8. result = runner.invoke(cli, [])
  9. assert not result.exception
  10. assert "FOO:[42.0]" in result.output
  11. def test_multiple_defaults(runner):
  12. @click.command()
  13. @click.option("--foo", default=[23, 42], type=click.FLOAT, multiple=True)
  14. def cli(foo):
  15. for item in foo:
  16. assert type(item) is float
  17. click.echo(item)
  18. result = runner.invoke(cli, [])
  19. assert not result.exception
  20. assert result.output.splitlines() == ["23.0", "42.0"]
  21. def test_nargs_plus_multiple(runner):
  22. @click.command()
  23. @click.option(
  24. "--arg", default=((1, 2), (3, 4)), nargs=2, multiple=True, type=click.INT
  25. )
  26. def cli(arg):
  27. for a, b in arg:
  28. click.echo(f"<{a:d}|{b:d}>")
  29. result = runner.invoke(cli, [])
  30. assert not result.exception
  31. assert result.output.splitlines() == ["<1|2>", "<3|4>"]
  32. def test_multiple_flag_default(runner):
  33. """Default default for flags when multiple=True should be empty tuple."""
  34. @click.command
  35. # flag due to secondary token
  36. @click.option("-y/-n", multiple=True)
  37. # flag due to is_flag
  38. @click.option("-f", is_flag=True, multiple=True)
  39. # flag due to flag_value
  40. @click.option("-v", "v", flag_value=1, multiple=True)
  41. @click.option("-q", "v", flag_value=-1, multiple=True)
  42. def cli(y, f, v):
  43. return y, f, v
  44. result = runner.invoke(cli, standalone_mode=False)
  45. assert result.return_value == ((), (), ())
  46. result = runner.invoke(cli, ["-y", "-n", "-f", "-v", "-q"], standalone_mode=False)
  47. assert result.return_value == ((True, False), (True,), (1, -1))