From 499ed143456363ee65874d4f276d8e2fcd46c58b Mon Sep 17 00:00:00 2001 From: Alex Lowe Date: Fri, 12 Jan 2024 13:30:53 -0500 Subject: [PATCH] test(dispatcher): GlobalArgument tests --- tests/unit/test_dispatcher.py | 112 ++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/tests/unit/test_dispatcher.py b/tests/unit/test_dispatcher.py index c57e491..c044571 100644 --- a/tests/unit/test_dispatcher.py +++ b/tests/unit/test_dispatcher.py @@ -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