Ejemplo n.º 1
0
def test_create_parser_simple() -> None:
    def func_to_test(a: int, b: str) -> int:
        return a + len(b)

    parser = create_parser(Command.from_func(func_to_test))
    args = parser.parse(["--a", "42", "--b", "1234"])
    assert args == {"a": 42, "b": "1234"}
Ejemplo n.º 2
0
def test_type_from_default() -> None:
    def func_to_test(a=4) -> int:
        return a

    parser = create_parser(Command.from_func(func_to_test))
    args = parser.parse(["--a", "4"])
    assert args == {"a": 4}
Ejemplo n.º 3
0
def test_run_command(mocker: pytest_mock.MockFixture) -> None:
    mocked_cmd = mocker.MagicMock()
    registered_commands = {"test_cmd": Command.from_func(mocked_cmd, name="test_cmd")}
    mocker.patch("auto_cli.cli._load_app", return_value=registered_commands)

    cli.run_command("my_app", ["test_cmd"])
    mocked_cmd.assert_called_once()
Ejemplo n.º 4
0
def test_override_param_type() -> None:
    def func_to_test(a: int, b):
        return a + b

    command = Command("cmd_name", func_to_test, {"b": int})
    parser = create_parser(command)
    args = parser.parse(["--a", "4", "--b", "5"])
    assert args == {"a": 4, "b": 5}
Ejemplo n.º 5
0
def test_create_parser_tuple() -> None:
    def func_to_test(a: Tuple[int, int], b: bool) -> int:
        return sum(a)

    parser = create_parser(Command.from_func(func_to_test))
    nums = (42, 1337)
    args = parser.parse(["--a"] + list(map(str, nums)))

    assert args == {"a": nums, "b": False}
Ejemplo n.º 6
0
def test_create_parser_list() -> None:
    def func_to_test(a: List[int]) -> int:
        return sum(a)

    parser = create_parser(Command.from_func(func_to_test))
    nums = [1, 3, 5, 7]
    args = parser.parse(["--a"] + list(map(str, nums)))

    assert args == {"a": nums}
Ejemplo n.º 7
0
def test_create_parser_tuple_var_length() -> None:
    def func_to_test(a: Tuple[int, ...]) -> int:
        return sum(a)

    command = Command.from_func(func_to_test)
    parser = create_parser(command)
    nums = (1, 2, 3)
    args = parser.parse(["--a"] + list(map(str, nums)))
    assert args == {"a": nums}
Ejemplo n.º 8
0
def test_create_parser_short_opts() -> None:
    def func_to_test(long_name: int) -> int:
        return long_name

    command = Command.from_func(func_to_test)
    command = command._replace(short_names={"long_name": "-l"})
    parser = create_parser(command)
    args = parser.parse(["-l", "4"])
    assert args == {"long_name": 4}
Ejemplo n.º 9
0
def test_create_parser_bool() -> None:
    def func_to_test(a: bool) -> bool:
        return a

    parser = create_parser(Command.from_func(func_to_test))
    args_with_flag = parser.parse(["--a"])
    assert args_with_flag == {"a": True}

    args_without_flag = parser.parse([])
    assert args_without_flag == {"a": False}
Ejemplo n.º 10
0
def test_create_parser_defaults() -> None:
    def func_to_test(a: int, b: int = 38) -> int:
        return a + b

    parser = create_parser(Command.from_func(func_to_test))
    args_no_default = parser.parse(["--a", "1", "--b", "42"])
    assert args_no_default == {"a": 1, "b": 42}

    args_with_default = parser.parse(["--a", "1"])
    assert args_with_default == {"a": 1, "b": 38}
Ejemplo n.º 11
0
def test_parse_function_doc() -> None:
    def func_to_test(a: int, b: str) -> int:
        """This is a function
        which does some interesting things

        :param int a: the number that is returned
        :param b: ignored
        """
        return a

    command = Command.from_func(func_to_test)
    docs = command.parse_function_doc()
    assert docs.description == "This is a function which does some interesting things"
    assert docs.param_docs == {
        "a": "the number that is returned",
        "b": "ignored"
    }
Ejemplo n.º 12
0
def test_create_parser_unsupported_type() -> None:
    def func_to_test(a: Tuple[int, float]) -> float:
        return sum(a)

    with pytest.raises(SystemExit):
        create_parser(Command.from_func(func_to_test))
Ejemplo n.º 13
0
def test_run_func_with_argv() -> None:
    def func_to_test(a: int, b: int = 38) -> int:
        return a + b

    result = cli.run_func_with_argv(Command.from_func(func_to_test), ["--a", "4"])
    assert result == 42