summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDavid Lord <davidism@gmail.com>2021-09-03 16:09:02 +0200
committerDavid Lord <davidism@gmail.com>2022-02-19 08:19:21 -0800
commit2d5a1563dc81b67641fd0e483d41e3b019552b92 (patch)
tree73a04c2ceafb035158ac1b3d892f5f4ceb109b55 /tests
parentbf8c03e81f31a8ad0742a191532f15ad549232fa (diff)
downloadclick-2d5a1563dc81b67641fd0e483d41e3b019552b92.tar.gz
parametrize bool switch and flag tests
Co-authored-by: Maximilian Wassink <wassink.maximilian@protonmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/test_basic.py51
1 files changed, 25 insertions, 26 deletions
diff --git a/tests/test_basic.py b/tests/test_basic.py
index eab7d18..d68b962 100644
--- a/tests/test_basic.py
+++ b/tests/test_basic.py
@@ -202,37 +202,36 @@ def test_float_option(runner, args, expect):
assert result.exception is None
-def test_boolean_option(runner):
- for default in True, False:
+@pytest.mark.parametrize("default", [True, False])
+@pytest.mark.parametrize(
+ ("args", "expect"), [(["--on"], True), (["--off"], False), ([], None)]
+)
+def test_boolean_switch(runner, default, args, expect):
+ @click.command()
+ @click.option("--on/--off", default=default)
+ def cli(on):
+ return on
- @click.command()
- @click.option("--with-foo/--without-foo", default=default)
- def cli(with_foo):
- click.echo(with_foo)
+ if expect is None:
+ expect = default
- result = runner.invoke(cli, ["--with-foo"])
- assert not result.exception
- assert result.output == "True\n"
- result = runner.invoke(cli, ["--without-foo"])
- assert not result.exception
- assert result.output == "False\n"
- result = runner.invoke(cli, [])
- assert not result.exception
- assert result.output == f"{default}\n"
+ result = runner.invoke(cli, args, standalone_mode=False)
+ assert result.return_value is expect
- for default in True, False:
- @click.command()
- @click.option("--flag", is_flag=True, default=default)
- def cli(flag):
- click.echo(flag)
+@pytest.mark.parametrize("default", [True, False])
+@pytest.mark.parametrize(("args", "expect"), [(["--f"], True), ([], False)])
+def test_boolean_flag(runner, default, args, expect):
+ @click.command()
+ @click.option("--f", is_flag=True, default=default)
+ def cli(f):
+ return f
- result = runner.invoke(cli, ["--flag"])
- assert not result.exception
- assert result.output == f"{not default}\n"
- result = runner.invoke(cli, [])
- assert not result.exception
- assert result.output == f"{default}\n"
+ if default:
+ expect = not expect
+
+ result = runner.invoke(cli, args, standalone_mode=False)
+ assert result.return_value is expect
@pytest.mark.parametrize(