Skip to content

Commit

Permalink
test: ensure 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
neithere committed Oct 13, 2023
1 parent 43528ff commit 0a757e0
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/argh/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
ATTR_WRAPPED_EXCEPTIONS_PROCESSOR,
)
from argh.dto import ParserAddArgumentSpec
from argh.utils import naive_guess_func_arg_name, CliArgToFuncArgGuessingError
from argh.utils import CliArgToFuncArgGuessingError, naive_guess_func_arg_name

__all__ = ["aliases", "named", "arg", "wrap_errors", "expects_obj"]

Expand Down
4 changes: 3 additions & 1 deletion tests/test_assembling.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ def func(foo: str) -> str:

parser_mock = MagicMock(spec=argparse.ArgumentParser)
parser_mock.add_help = False
parser_mock.add_argument.side_effect = argparse.ArgumentError(None, "my hat's on fire!")
parser_mock.add_argument.side_effect = argparse.ArgumentError(
None, "my hat's on fire!"
)

with pytest.raises(argh.AssemblingError):
argh.set_default_command(parser_mock, func)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_naive_guess_func_arg_name() -> None:
assert naive_guess_func_arg_name(("-x", "--foo", "--bar")) == "foo"

with pytest.raises(CliArgToFuncArgGuessingError):
naive_guess_func_arg_name(("-x", '-y'))
naive_guess_func_arg_name(("-x", "-y"))

# mixed (errors)
with pytest.raises(MixedPositionalAndOptionalArgsError):
Expand Down
125 changes: 125 additions & 0 deletions tests/test_dto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
"""
Unit Tests For the Argument DTO
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
from argh.dto import ParserAddArgumentSpec


def test_update_empty_dto() -> None:

def stub_completer():
...

dto = ParserAddArgumentSpec(
func_arg_name="foo",
cli_arg_names=["-f"],
)
other_dto = ParserAddArgumentSpec(
func_arg_name="bar",
cli_arg_names=["-f", "--foo"],
is_required=True,
default_value=123,
nargs="+",
other_add_parser_kwargs={"knights": "Ni!"},
completer=stub_completer,
)

dto.update(other_dto)

assert dto == ParserAddArgumentSpec(
func_arg_name="foo",
cli_arg_names=["-f", "--foo"],
is_required=True,
default_value=123,
nargs="+",
other_add_parser_kwargs={"knights": "Ni!"},
completer=stub_completer,
)


def test_update_full_dto() -> None:

def stub_completer_one():
...

def stub_completer_two():
...

dto = ParserAddArgumentSpec(
func_arg_name="foo",
cli_arg_names=["-f"],
nargs="?",
is_required=True,
default_value=123,
other_add_parser_kwargs={"'tis but a": "scratch"},
completer=stub_completer_one,
)
other_dto = ParserAddArgumentSpec(
func_arg_name="bar",
cli_arg_names=["-f", "--foo"],
nargs="+",
is_required=False,
default_value=None,
other_add_parser_kwargs={"knights": "Ni!"},
completer=stub_completer_two,
)

dto.update(other_dto)

assert dto == ParserAddArgumentSpec(
func_arg_name="foo",
cli_arg_names=["-f", "--foo"],
is_required=False,
default_value=None,
nargs="+",
other_add_parser_kwargs={"knights": "Ni!", "'tis but a": "scratch"},
completer=stub_completer_two,
)


class TestGetAllKwargs:
...


def test_make_from_kwargs_minimal() -> None:
dto = ParserAddArgumentSpec.make_from_kwargs("foo", ["-f", "--foo"], {})

assert dto == ParserAddArgumentSpec(
func_arg_name="foo", cli_arg_names=["-f", "--foo"]
)


def test_make_from_kwargs_full() -> None:
dto = ParserAddArgumentSpec.make_from_kwargs(
"foo",
["-f", "--foo"],
{
"action": "some action",
"nargs": "?",
"default": None,
"type": str,
"choices": [1, 2, 3],
"required": False,
"help": "some help",
"metavar": "FOOOOO",
"dest": "foo_dest",
"some arbitrary key": "and its value",
},
)

assert dto == ParserAddArgumentSpec(
func_arg_name="foo",
cli_arg_names=["-f", "--foo"],
is_required=False,
default_value=None,
nargs="?",
other_add_parser_kwargs={
"action": "some action",
"type": str,
"choices": [1, 2, 3],
"help": "some help",
"metavar": "FOOOOO",
"dest": "foo_dest",
"some arbitrary key": "and its value",
},
)

0 comments on commit 0a757e0

Please sign in to comment.