tiramisu-cmdline-parser/tests/test_help.py

96 lines
2.5 KiB
Python
Raw Permalink Normal View History

2019-07-26 16:39:01 +02:00
from io import StringIO
from contextlib import redirect_stdout, redirect_stderr
import pytest
from argparse import RawDescriptionHelpFormatter
2019-07-26 16:39:01 +02:00
from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
2024-07-30 11:29:10 +02:00
OptionDescription, Config
2019-08-23 16:22:34 +02:00
try:
from tiramisu_api import Config as JsonConfig
2024-07-30 11:29:10 +02:00
#params = ['tiramisu', 'tiramisu-json']
params = ['tiramisu']
2019-08-23 16:22:34 +02:00
except:
params = ['tiramisu']
2024-07-30 11:29:10 +02:00
from .utils import TestHelpFormatter
2019-07-26 16:39:01 +02:00
def get_config(json):
choiceoption = ChoiceOption('cmd',
'choice the sub argument',
('str', 'list', 'int', 'none'),
properties=('mandatory',
'positional'))
od = OptionDescription('od',
'od',
[choiceoption])
root = OptionDescription('root',
'root',
[od])
config = Config(root)
config.property.read_write()
if json == 'tiramisu':
return config
jconfig = JsonConfig(config.option.dict())
return jconfig
2019-08-23 16:22:34 +02:00
@pytest.fixture(params=params)
2019-07-26 16:39:01 +02:00
def json(request):
return request.param
def test_help(json):
output = """usage: prog.py [-h] {str,list,int,none}
2024-07-30 11:29:10 +02:00
options:
2019-07-26 16:39:01 +02:00
-h, --help show this help message and exit
od:
{str,list,int,none} choice the sub argument
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter)
2019-07-26 16:39:01 +02:00
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
def test_help_epilog(json):
output = """usage: prog.py [-h] {str,list,int,none}
2024-07-30 11:29:10 +02:00
options:
2019-07-26 16:39:01 +02:00
-h, --help show this help message and exit
od:
{str,list,int,none} choice the sub argument
two line
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', epilog="\ntwo\nline", formatter_class=TestHelpFormatter)
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
def test_help_epilog_raw(json):
output = """usage: prog.py [-h] {str,list,int,none}
2024-07-30 11:29:10 +02:00
options:
-h, --help show this help message and exit
od:
{str,list,int,none} choice the sub argument
2019-07-26 16:39:01 +02:00
two
line
"""
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', epilog="\ntwo\nline", formatter_class=RawDescriptionHelpFormatter)
2019-07-26 16:39:01 +02:00
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output