| 1 |
from unittest.mock import patch |
| 2 |
from argparse import ArgumentParser |
| 3 |
import pytest |
| 4 |
from autocommand.autoparse import autoparse, TooManySplitsError |
| 5 |
|
| 6 |
|
| 7 |
def test_basic_invocation(): |
| 8 |
@autoparse |
| 9 |
def func(arg1, arg2: int, opt1=None, opt2=2, opt3='default', flag=False): |
| 10 |
return arg1, arg2, opt1, opt2, opt3, flag |
| 11 |
|
| 12 |
arg1, arg2, opt1, opt2, opt3, flag = func( |
| 13 |
['value1', '1', '-o', 'hello', '--opt2', '10', '-f']) |
| 14 |
|
| 15 |
assert arg1 == 'value1' |
| 16 |
assert arg2 == 1 |
| 17 |
assert opt1 == 'hello' |
| 18 |
assert opt2 == 10 |
| 19 |
assert opt3 == 'default' |
| 20 |
assert flag is True |
| 21 |
|
| 22 |
|
| 23 |
def test_invocation_from_argv(): |
| 24 |
@autoparse |
| 25 |
def func(arg1, arg2: int): |
| 26 |
return arg1, arg2 |
| 27 |
|
| 28 |
with patch('sys.argv', ['command', '1', '2']): |
| 29 |
arg1, arg2 = func() |
| 30 |
assert arg1 == "1" |
| 31 |
assert arg2 == 2 |
| 32 |
|
| 33 |
|
| 34 |
def test_description_epilog_help(check_help_text): |
| 35 |
@autoparse( |
| 36 |
description='this is a description', |
| 37 |
epilog='this is an epilog') |
| 38 |
def func(arg: 'this is help text'): |
| 39 |
pass |
| 40 |
|
| 41 |
check_help_text( |
| 42 |
lambda: func(['-h']), |
| 43 |
'this is a description', |
| 44 |
'this is an epilog', |
| 45 |
'this is help text') |
| 46 |
|
| 47 |
|
| 48 |
def test_docstring_description(check_help_text): |
| 49 |
@autoparse |
| 50 |
def func(arg): |
| 51 |
'''This is a docstring description''' |
| 52 |
pass |
| 53 |
|
| 54 |
check_help_text( |
| 55 |
lambda: func(['-h']), |
| 56 |
'This is a docstring description') |
| 57 |
|
| 58 |
|
| 59 |
def test_docstring_description_epilog(check_help_text): |
| 60 |
@autoparse |
| 61 |
def func(arg): |
| 62 |
''' |
| 63 |
This is the description |
| 64 |
------- |
| 65 |
This is the epilog |
| 66 |
''' |
| 67 |
pass |
| 68 |
|
| 69 |
created_parser = func.parser |
| 70 |
assert 'This is the description' in created_parser.description |
| 71 |
assert 'This is the epilog' in created_parser.epilog |
| 72 |
|
| 73 |
check_help_text( |
| 74 |
lambda: func(['-h']), |
| 75 |
'This is the description', |
| 76 |
'This is the epilog', |
| 77 |
reject='-------') |
| 78 |
|
| 79 |
|
| 80 |
def test_bad_docstring(): |
| 81 |
with pytest.raises(TooManySplitsError): |
| 82 |
@autoparse |
| 83 |
def func(arg): |
| 84 |
''' |
| 85 |
Part 1 |
| 86 |
------- |
| 87 |
Part 2 |
| 88 |
------- |
| 89 |
Part 3 |
| 90 |
''' |
| 91 |
pass |
| 92 |
|
| 93 |
|
| 94 |
def test_custom_parser(): |
| 95 |
parser = ArgumentParser() |
| 96 |
|
| 97 |
parser.add_argument('arg', nargs='?') |
| 98 |
group = parser.add_mutually_exclusive_group() |
| 99 |
group.add_argument('-v', '--verbose', action='store_true') |
| 100 |
group.add_argument('-q', '--quiet', action='store_true') |
| 101 |
|
| 102 |
@autoparse(parser=parser) |
| 103 |
def main(arg, verbose, quiet): |
| 104 |
return arg, verbose, quiet |
| 105 |
|
| 106 |
assert main([]) == (None, False, False) |
| 107 |
assert main(['thing']) == ('thing', False, False) |
| 108 |
assert main(['-v']) == (None, True, False) |
| 109 |
assert main(['-q']) == (None, False, True) |
| 110 |
assert main(['-v', 'thing']) == ('thing', True, False) |
| 111 |
|
| 112 |
with pytest.raises(SystemExit): |
| 113 |
main(['-v', '-q']) |