File: test_decorators.py

package info (click to toggle)
python-argh 0.31.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 352 kB
  • sloc: python: 3,234; makefile: 4
file content (111 lines) | stat: -rw-r--r-- 2,887 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
Unit Tests For Decorators
~~~~~~~~~~~~~~~~~~~~~~~~~
"""

import pytest

import argh
from argh.dto import ParserAddArgumentSpec
from argh.utils import (
    CliArgToFuncArgGuessingError,
    MixedPositionalAndOptionalArgsError,
    TooManyPositionalArgumentNames,
    naive_guess_func_arg_name,
)


def test_aliases():
    @argh.aliases("one", "two")
    def func():
        pass

    attr = getattr(func, argh.constants.ATTR_ALIASES)
    assert attr == ("one", "two")


def test_arg():
    @argh.arg("foo", help="my help", nargs="+")
    @argh.arg("--bar", default=1)
    def func():
        pass

    attrs = getattr(func, argh.constants.ATTR_ARGS)
    assert attrs == [
        ParserAddArgumentSpec(
            func_arg_name="foo",
            cli_arg_names=["foo"],
            nargs="+",
            other_add_parser_kwargs={
                "help": "my help",
            },
        ),
        ParserAddArgumentSpec(
            func_arg_name="bar",
            cli_arg_names=["--bar"],
            default_value=1,
        ),
    ]


def test_named():
    @argh.named("new-name")
    def func():
        pass

    attr = getattr(func, argh.constants.ATTR_NAME)
    assert attr == "new-name"


def test_wrap_errors():
    @argh.wrap_errors([KeyError, ValueError])
    def func():
        pass

    attr = getattr(func, argh.constants.ATTR_WRAPPED_EXCEPTIONS)
    assert attr == [KeyError, ValueError]


def test_wrap_errors_processor():
    @argh.wrap_errors(processor="STUB")
    def func():
        pass

    attr = getattr(func, argh.constants.ATTR_WRAPPED_EXCEPTIONS_PROCESSOR)
    assert attr == "STUB"


def test_naive_guess_func_arg_name() -> None:
    # none (error)
    with pytest.raises(CliArgToFuncArgGuessingError):
        argh.arg()(lambda foo: foo)

    # positional
    assert naive_guess_func_arg_name(("foo",)) == "foo"

    # positional — more than one (error)
    with pytest.raises(TooManyPositionalArgumentNames):
        argh.arg("foo", "bar")(lambda foo: foo)

    # option
    assert naive_guess_func_arg_name(("-x",)) == "x"
    assert naive_guess_func_arg_name(("--foo",)) == "foo"
    assert naive_guess_func_arg_name(("--foo", "-f")) == "foo"
    assert naive_guess_func_arg_name(("-f", "--foo")) == "foo"
    assert naive_guess_func_arg_name(("-x", "--foo", "--bar")) == "foo"

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

    # mixed (errors)
    with pytest.raises(MixedPositionalAndOptionalArgsError):
        argh.arg("foo", "--foo")(lambda foo: foo)

    with pytest.raises(MixedPositionalAndOptionalArgsError):
        argh.arg("--foo", "foo")(lambda foo: foo)

    with pytest.raises(MixedPositionalAndOptionalArgsError):
        argh.arg("-f", "foo")(lambda foo: foo)

    with pytest.raises(MixedPositionalAndOptionalArgsError):
        argh.arg("foo", "-f")(lambda foo: foo)