Skip to content

Commit

Permalink
test(dispatcher): GlobalArgument tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau committed Jan 12, 2024
1 parent 02deb38 commit 499ed14
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions tests/unit/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,118 @@
from craft_cli.errors import ArgumentParsingError
from tests.factory import create_command

# --- Tests for global arguments


@pytest.mark.parametrize(
("params", "expected"),
[
pytest.param(
{"type": "flag"},
GlobalArgument(
name="my-arg",
short_option=None,
long_option="--long-option",
help_message="A global argument for testing",
type="flag",
),
id="basic-flag",
),
pytest.param(
{"type": "option"},
GlobalArgument(
name="my-arg",
short_option=None,
long_option="--long-option",
help_message="A global argument for testing",
type="option",
),
id="basic-option",
),
pytest.param(
{"type": "option", "choices": ["a", "b", "c"]},
GlobalArgument(
name="my-arg",
short_option=None,
long_option="--long-option",
help_message="A global argument for testing",
type="option",
choices=["a", "b", "c"],
),
id="choices",
),
pytest.param(
{"type": "option", "choices": "ABC", "case_sensitive": False},
GlobalArgument(
name="my-arg",
short_option=None,
long_option="--long-option",
help_message="A global argument for testing",
type="option",
choices=["a", "b", "c"],
case_sensitive=False,
),
id="case-insensitive",
),
pytest.param(
{"type": "option", "validator": int},
GlobalArgument(
name="my-arg",
short_option=None,
long_option="--long-option",
help_message="A global argument for testing",
type="option",
validator=int,
),
id="int-validator",
),
pytest.param(
{"type": "option", "choices": "123", "validator": int},
GlobalArgument(
name="my-arg",
short_option=None,
long_option="--long-option",
help_message="A global argument for testing",
type="option",
choices="123",
validator=int,
),
id="limited-ints",
),
],
)
def test_global_argument_init_success(params, expected):
my_params = {
"name": "my-arg",
"short_option": None,
"long_option": "--long-option",
"help_message": "A global argument for testing",
}
my_params.update(params)
assert GlobalArgument(**my_params) == expected


@pytest.mark.parametrize(
("params", "match"),
[
(
{"type": "flag", "choices": "something"},
"A flag argument cannot have choices or a validator.",
)
],
)
def test_global_argument_init_error(params, match):
my_params = {
"name": "my-arg",
"short_option": None,
"long_option": "--long-option",
"help_message": "A global argument for testing",
}
my_params.update(params)
with pytest.raises(TypeError, match=match):
GlobalArgument(**my_params)


# --- Tests for the Dispatcher


Expand Down

0 comments on commit 499ed14

Please sign in to comment.