From 490879a2a9fd70116d95757c1c3f9b2347b3d002 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Thu, 9 May 2019 20:12:23 +0200 Subject: [PATCH] add tests --- test/test_readme.py | 11 ++++ test/test_shortarg.py | 120 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 test/test_shortarg.py diff --git a/test/test_readme.py b/test/test_readme.py index aa8dc15..7baa13d 100644 --- a/test/test_readme.py +++ b/test/test_readme.py @@ -573,6 +573,17 @@ def test_readme_str_int_tree_flatten(json): 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): output = {'cmd': 'list', 'list': ['a', 'b', 'c'], diff --git a/test/test_shortarg.py b/test/test_shortarg.py new file mode 100644 index 0000000..8bd966e --- /dev/null +++ b/test/test_shortarg.py @@ -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