File: test_utils.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 (75 lines) | stat: -rw-r--r-- 1,335 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
"""
Unit Tests For Utility Functions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"""

from argparse import ArgumentParser, _SubParsersAction

import pytest

from argh.utils import SubparsersNotDefinedError, get_subparsers, unindent


def test_util_unindent():
    "Self-test the unindent() helper function"

    # target case
    one = """
    a
     b
      c
    """
    assert (
        unindent(one)
        == """
a
 b
  c
"""
    )

    # edge case: lack of indentation on first non-empty line
    two = """
a
  b
    c
"""

    assert unindent(two) == two

    # edge case: unexpectedly unindented in between
    three = """
    a
b
    c
    """

    assert (
        unindent(three)
        == """
a
b
c
"""
    )


def test_get_subparsers_existing() -> None:
    parser = ArgumentParser()
    parser.add_subparsers(help="hello")
    sub_parsers_action = get_subparsers(parser)
    assert isinstance(sub_parsers_action, _SubParsersAction)
    assert sub_parsers_action.help == "hello"


def test_get_subparsers_create() -> None:
    parser = ArgumentParser()
    sub_parsers_action = get_subparsers(parser, create=True)
    assert isinstance(sub_parsers_action, _SubParsersAction)


def test_get_subparsers_error() -> None:
    parser = ArgumentParser()
    with pytest.raises(SubparsersNotDefinedError):
        get_subparsers(parser)