File: test_common.py

package info (click to toggle)
python-cloup 3.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 936 kB
  • sloc: python: 5,371; makefile: 120
file content (75 lines) | stat: -rw-r--r-- 2,638 bytes parent folder | download | duplicates (2)
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
from click import Argument, Option

from cloup.constraints.common import (
    format_param, format_param_list, get_param_label, join_with_and, param_value_is_set,
)
from tests.util import bool_opt, flag_opt, int_opt, parametrize, multi_opt, tuple_opt


@parametrize(
    'param_type,    value,      expected',
    (Argument,      None,       False),
    (Argument,      'bu',       True),
    (Option,        None,       False),
    (Option,        'bu',       True),
    (int_opt,       0,          True),
    (bool_opt,      False,      True),   # non-flag boolean opts are set even if False
    (flag_opt,      False,      False),
    (flag_opt,      True,       True),
    (tuple_opt,     (),         False),
    (tuple_opt,     (1, 2),     True),
    (multi_opt,     (),         False),
    (multi_opt,     (1, 2),     True),
    ids=lambda val: val.__name__ if callable(val) else None,
)
def test_param_value_is_set(param_type, value, expected):
    param = param_type(['-o'])
    actual = param_value_is_set(param, value)
    assert actual == expected


def test_get_param_label():
    assert get_param_label(Argument(['arg'])) == 'ARG'
    assert get_param_label(Option(['--opt'])) == '--opt'
    assert get_param_label(Option(['--opt', '-o'])) == '--opt'
    assert get_param_label(Option(['-o', '--opt'])) == '--opt'
    assert get_param_label(Option(['-o/-O', '--opt/--no-opt'])) == '--opt'


def test_format_param():
    assert format_param(Argument(['arg'])) == 'ARG'
    assert format_param(Option(['--opt'])) == '--opt'
    assert format_param(Option(['-o'])) == '-o'
    assert format_param(Option(['--opt', '-o'])) == '--opt (-o)'
    assert format_param(Option(['-o', '--opt'])) == '--opt (-o)'
    assert format_param(Option(['-o/-O', '--opt/--no-opt', 'blah'])) == '--opt (-o)'
    # Multiple long opts
    assert format_param(Option(['--name', '--username'])) == (
        '--name (--username)'
    )
    # Multiple short opts
    assert format_param(Option(['--name', '-n', '-u'])) == (
        '--name (-n, -u)'
    )
    # Multiple short and long opts
    assert format_param(Option(['-n', '-u', '--name', '--username'])) == (
        '--name (--username, -n, -u)'
    )


def test_format_param_list():
    params = [
        Argument(['arg']),
        Option(['--one']),
        Option(['--two', '-t']),
    ]
    expected = (' ARG\n'
                ' --one\n'
                ' --two (-t)\n')
    assert format_param_list(params, indent=1) == expected


def test_join_with_and():
    assert join_with_and([]) == ''
    assert join_with_and('A') == 'A'
    assert join_with_and('ABC', sep='; ') == 'A; B and C'