200 lines
6.5 KiB
Python
200 lines
6.5 KiB
Python
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(json):
|
|
def get_config():
|
|
list_ = StrOption('list',
|
|
'list string option')
|
|
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': None, 'l': None}
|
|
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', 'l': 'a'}
|
|
config = get_config()
|
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
|
parser.parse_args(['-l', 'a'])
|
|
assert config.value.dict() == output
|
|
|
|
|
|
def test_short_mandatory(json):
|
|
def get_config():
|
|
list_ = StrOption('list',
|
|
'list string option',
|
|
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
|
|
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', 'l': 'a'}
|
|
config = get_config()
|
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
|
parser.parse_args(['-l', 'a'])
|
|
assert config.value.dict() == output
|
|
|
|
|
|
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
|