| 1 |
import pytest |
| 2 |
|
| 3 |
|
| 4 |
def test_int_positional(check_parse): |
| 5 |
def func(arg: int): pass |
| 6 |
|
| 7 |
check_parse( |
| 8 |
func, |
| 9 |
"1", |
| 10 |
arg=1) |
| 11 |
|
| 12 |
with pytest.raises(SystemExit): |
| 13 |
check_parse( |
| 14 |
func, |
| 15 |
'hello') |
| 16 |
|
| 17 |
|
| 18 |
def test_int_default(check_parse): |
| 19 |
def func(arg=10): pass |
| 20 |
|
| 21 |
check_parse(func, "-a1", arg=1) |
| 22 |
check_parse(func, arg=10) |
| 23 |
with pytest.raises(SystemExit): |
| 24 |
check_parse(func, "-aHello") |
| 25 |
|
| 26 |
|
| 27 |
def test_int_none_default(check_parse): |
| 28 |
def func(arg: int =None): pass |
| 29 |
|
| 30 |
check_parse(func, '-a1', arg=1) |
| 31 |
check_parse(func, arg=None) |
| 32 |
|
| 33 |
with pytest.raises(SystemExit): |
| 34 |
check_parse(func, '-aHello') |
| 35 |
|
| 36 |
|
| 37 |
# A note on bool types: when the type is EXPLICITLY bool, then the parameter is |
| 38 |
# flag no matter what. If the flag is present on the CLI, the default is NOT |
| 39 |
# used; this is the most internally consistent behavior (no flag -> default, |
| 40 |
# flag -> nondefault). The "truthiness" of the default is used to determine |
| 41 |
# the nondefault value- falsy values like `None`, `0`, and `[]` result in True |
| 42 |
# being nondefault, while "truthy" values like `1`, `[1]`, and `'hello'` result |
| 43 |
# in False being nondefault. |
| 44 |
def make_bool_test_params(): |
| 45 |
def bool1(flag=False): pass |
| 46 |
|
| 47 |
def bool2(flag=True): pass |
| 48 |
|
| 49 |
def bool3(flag: bool): pass |
| 50 |
|
| 51 |
def bool4(flag: bool =None): pass |
| 52 |
|
| 53 |
def bool5(flag: bool =0): pass |
| 54 |
|
| 55 |
def bool6(flag: bool ='noflag'): pass |
| 56 |
return [ |
| 57 |
(bool1, True, False), |
| 58 |
(bool2, False, True), |
| 59 |
(bool3, True, False), |
| 60 |
(bool4, True, None), |
| 61 |
(bool5, True, 0), |
| 62 |
(bool6, False, 'noflag')] |
| 63 |
|
| 64 |
|
| 65 |
@pytest.mark.parametrize( |
| 66 |
'function, with_flag, without_flag', |
| 67 |
make_bool_test_params() |
| 68 |
) |
| 69 |
def test_bool(check_parse, function, with_flag, without_flag): |
| 70 |
check_parse(function, '-f', flag=with_flag) |
| 71 |
check_parse(function, '--flag', flag=with_flag) |
| 72 |
check_parse(function, flag=without_flag) |
| 73 |
|
| 74 |
|
| 75 |
def test_file(check_parse, tmpdir): |
| 76 |
filepath = tmpdir.join('test_file.txt') |
| 77 |
filepath.ensure(file=True) |
| 78 |
with filepath.open() as file: |
| 79 |
def func(input_file=file): pass |
| 80 |
|
| 81 |
check_parse(func, input_file=file) |
| 82 |
check_parse(func, "-i", "path/to/file", input_file="path/to/file") |