add tests

This commit is contained in:
Emmanuel Garette 2019-05-09 20:12:23 +02:00 committed by Emmanuel Garette
parent 1cb8a2cc7b
commit 490879a2a9
2 changed files with 131 additions and 0 deletions

View file

@ -573,6 +573,17 @@ def test_readme_str_int_tree_flatten(json):
assert config.value.dict() == output assert config.value.dict() == output
def test_readme_list_single(json):
output = {'cmd': 'list',
'list': ['a'],
'verbosity': False,
'v': False}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['list', '--list', 'a'])
assert config.value.dict() == output
def test_readme_list(json): def test_readme_list(json):
output = {'cmd': 'list', output = {'cmd': 'list',
'list': ['a', 'b', 'c'], 'list': ['a', 'b', 'c'],

120
test/test_shortarg.py Normal file
View file

@ -0,0 +1,120 @@
from io import StringIO
import pytest
from contextlib import redirect_stderr
from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
SymLinkOption, OptionDescription, Config
from tiramisu_json_api import Config as JsonConfig
@pytest.fixture(params=['tiramisu', 'tiramisu-json'])
def json(request):
return request.param
def test_short_multi(json):
def get_config():
list_ = StrOption('list',
'list string option',
multi=True)
slist_ = SymLinkOption('l', list_)
root = OptionDescription('root',
'root',
[list_,
slist_,
])
config = Config(root)
config.property.read_write()
if json != 'tiramisu':
config = JsonConfig(config.option.dict())
return config
#
output = {'list': [], 'l': []}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args([])
assert config.value.dict() == output
#
output = {'list': ['a'], 'l': ['a']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--list', 'a'])
assert config.value.dict() == output
#
output = {'list': ['a', 'b'], 'l': ['a', 'b']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--list', 'a', 'b'])
assert config.value.dict() == output
#
output = {'list': ['a'], 'l': ['a']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['-l', 'a'])
assert config.value.dict() == output
#
output = {'list': ['a', 'b'], 'l': ['a', 'b']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['-l', 'a', 'b'])
assert config.value.dict() == output
def test_short_multi_mandatory(json):
def get_config():
list_ = StrOption('list',
'list string option',
multi=True,
properties=('mandatory',))
slist_ = SymLinkOption('l', list_)
root = OptionDescription('root',
'root',
[list_,
slist_,
])
config = Config(root)
config.property.read_write()
if json != 'tiramisu':
config = JsonConfig(config.option.dict())
return config
#
output = """usage: prog.py [-h] -l LIST [LIST ...]
prog.py: error: the following arguments are required: --list
"""
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args([])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
#
output = {'list': ['a'], 'l': ['a']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--list', 'a'])
assert config.value.dict() == output
#
output = {'list': ['a', 'b'], 'l': ['a', 'b']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--list', 'a', 'b'])
assert config.value.dict() == output
#
output = {'list': ['a'], 'l': ['a']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['-l', 'a'])
assert config.value.dict() == output
#
output = {'list': ['a', 'b'], 'l': ['a', 'b']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['-l', 'a', 'b'])
assert config.value.dict() == output