summaryrefslogtreecommitdiff
path: root/tests/test_acargparse.py
blob: 617afd4fa5e869361b275e5c98e3137bcace4dc3 (plain)
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
"""
Unit/functional testing for argparse customizations in cmd2

Copyright 2018 Eric Lin <anselor@gmail.com>
Released under MIT license, see LICENSE file
"""
import pytest
from cmd2.argparse_completer import ACArgumentParser, token_resembles_flag


def test_acarg_narg_empty_tuple():
    with pytest.raises(ValueError) as excinfo:
        parser = ACArgumentParser(prog='test')
        parser.add_argument('invalid_tuple', nargs=())
    assert 'Ranged values for nargs must be a tuple of 2 integers' in str(excinfo.value)


def test_acarg_narg_single_tuple():
    with pytest.raises(ValueError) as excinfo:
        parser = ACArgumentParser(prog='test')
        parser.add_argument('invalid_tuple', nargs=(1,))
    assert 'Ranged values for nargs must be a tuple of 2 integers' in str(excinfo.value)


def test_acarg_narg_tuple_triple():
    with pytest.raises(ValueError) as excinfo:
        parser = ACArgumentParser(prog='test')
        parser.add_argument('invalid_tuple', nargs=(1, 2, 3))
    assert 'Ranged values for nargs must be a tuple of 2 integers' in str(excinfo.value)


def test_acarg_narg_tuple_order():
    with pytest.raises(ValueError) as excinfo:
        parser = ACArgumentParser(prog='test')
        parser.add_argument('invalid_tuple', nargs=(2, 1))
    assert 'Invalid nargs range. The first value must be less than the second' in str(excinfo.value)


def test_acarg_narg_tuple_negative():
    with pytest.raises(ValueError) as excinfo:
        parser = ACArgumentParser(prog='test')
        parser.add_argument('invalid_tuple', nargs=(-1, 1))
    assert 'Negative numbers are invalid for nargs range' in str(excinfo.value)


def test_acarg_narg_tuple_zero_base():
    parser = ACArgumentParser(prog='test')
    parser.add_argument('tuple', nargs=(0, 3))


def test_acarg_narg_tuple_zero_to_one():
    parser = ACArgumentParser(prog='test')
    parser.add_argument('tuple', nargs=(0, 1))


def test_token_resembles_flag():
    parser = ACArgumentParser()

    # Not valid flags
    assert not token_resembles_flag('', parser)
    assert not token_resembles_flag('non-flag', parser)
    assert not token_resembles_flag('-', parser)
    assert not token_resembles_flag('--has space', parser)
    assert not token_resembles_flag('-2', parser)

    # Valid flags
    assert token_resembles_flag('-flag', parser)
    assert token_resembles_flag('--flag', parser)