Skip to content

Commit cc3942a

Browse files
committed
test: add two new tests for Parameter.__init__ in src/click/core.py
The function Parameter.__init__ does not have 100% coverage. This commit adds two new test cases that improve the coverage. The first one tests that a ValueError is raised when `nargs != self.type.arity`. The second one tests that a ValueError is raised when `_check_iter(check_default)` raises `TypeError`. Fixes pallets#2860 Recreation of pallets#2861
1 parent 0be2bd3 commit cc3942a

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

tests/test_arguments.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ def copy(item):
6363
assert result.output.splitlines() == ["name=peter id=1"]
6464

6565

66+
def test_nargs_mismatch_with_tuple_type():
67+
with pytest.raises(ValueError, match="nargs.*must be 2.*but it was 3"):
68+
69+
@click.command()
70+
@click.argument("test", type=(str, int), nargs=3)
71+
def cli(_):
72+
pass
73+
74+
6675
def test_nargs_err(runner):
6776
@click.command()
6877
@click.argument("x")

tests/test_options.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,3 +1453,33 @@ def cli(one, two):
14531453

14541454
with pytest.warns(UserWarning):
14551455
runner.invoke(cli, [])
1456+
1457+
1458+
@pytest.mark.parametrize(
1459+
("multiple", "nargs", "default", "expected_message"),
1460+
[
1461+
(
1462+
False, # multiple
1463+
2, # nargs
1464+
42, # default (not a list)
1465+
"'default' must be a list when 'nargs' != 1.",
1466+
),
1467+
(
1468+
True, # multiple
1469+
2, # nargs
1470+
[
1471+
"test string which is not a list in the list"
1472+
], # default (list of non-lists)
1473+
"'default' must be a list of lists when 'multiple' is true"
1474+
" and 'nargs' != 1.",
1475+
),
1476+
],
1477+
)
1478+
def test_default_type_error_raises(multiple, nargs, default, expected_message):
1479+
with pytest.raises(ValueError, match=expected_message):
1480+
click.Option(
1481+
["--test"],
1482+
multiple=multiple,
1483+
nargs=nargs,
1484+
default=default,
1485+
)

0 commit comments

Comments
 (0)