first commit
This commit is contained in:
1
test/data/compare/10_positional/argparse.py
Normal file
1
test/data/compare/10_positional/argparse.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_argument("echo", help="echo the string you use here")
|
1
test/data/compare/10_positional/tiramisu.py
Normal file
1
test/data/compare/10_positional/tiramisu.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_arguments(StrOption('echo', 'echo the string you use here', properties=('mandatory', 'positional')))
|
1
test/data/compare/10_positional_default/argparse.py
Normal file
1
test/data/compare/10_positional_default/argparse.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_argument("echo", help="echo the string you use here", default='blah', nargs='?')
|
1
test/data/compare/10_positional_default/tiramisu.py
Normal file
1
test/data/compare/10_positional_default/tiramisu.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_arguments(StrOption('echo', 'echo the string you use here', properties=('mandatory', 'positional'), default='blah'))
|
2
test/data/compare/10_positional_int/argparse.py
Normal file
2
test/data/compare/10_positional_int/argparse.py
Normal file
@ -0,0 +1,2 @@
|
||||
parser.add_argument("square", help="display a square of a given number",
|
||||
type=int)
|
1
test/data/compare/10_positional_int/tiramisu.py
Normal file
1
test/data/compare/10_positional_int/tiramisu.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_arguments(IntOption('square', 'display a square of a given number', properties=('mandatory', 'positional')))
|
1
test/data/compare/10_positional_list/argparse.py
Normal file
1
test/data/compare/10_positional_list/argparse.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_argument("echo", help="echo the string you use here", nargs='+')
|
1
test/data/compare/10_positional_list/tiramisu.py
Normal file
1
test/data/compare/10_positional_list/tiramisu.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_arguments(StrOption('echo', 'echo the string you use here', properties=('mandatory', 'positional'), multi=True))
|
1
test/data/compare/20_bool/argparse.py
Normal file
1
test/data/compare/20_bool/argparse.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_argument('--verbosity', help='increase output verbosity', action='store_true')
|
1
test/data/compare/20_bool/tiramisu.py
Normal file
1
test/data/compare/20_bool/tiramisu.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_arguments(BoolOption('verbosity', 'increase output verbosity', default=False))
|
1
test/data/compare/20_bool_true/argparse.py
Normal file
1
test/data/compare/20_bool_true/argparse.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_argument('--verbosity', help='increase output verbosity', action='store_false')
|
1
test/data/compare/20_bool_true/tiramisu.py
Normal file
1
test/data/compare/20_bool_true/tiramisu.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_arguments(BoolOption('verbosity', 'increase output verbosity', default=True))
|
2
test/data/compare/20_choice/argparse.py
Normal file
2
test/data/compare/20_choice/argparse.py
Normal file
@ -0,0 +1,2 @@
|
||||
parser.add_argument('--door', help='Door numbers', choices=['1', '2', '3'])
|
||||
|
1
test/data/compare/20_choice/tiramisu.py
Normal file
1
test/data/compare/20_choice/tiramisu.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_arguments(ChoiceOption('door', 'Door numbers', ('1', '2', '3')))
|
2
test/data/compare/20_int/argparse.py
Normal file
2
test/data/compare/20_int/argparse.py
Normal file
@ -0,0 +1,2 @@
|
||||
parser.add_argument('--int', help='integer', type=int)
|
||||
|
1
test/data/compare/20_int/tiramisu.py
Normal file
1
test/data/compare/20_int/tiramisu.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_arguments(IntOption('int', 'integer'))
|
2
test/data/compare/20_string/argparse.py
Normal file
2
test/data/compare/20_string/argparse.py
Normal file
@ -0,0 +1,2 @@
|
||||
parser.add_argument('--foo', help='foo help')
|
||||
|
2
test/data/compare/20_string/tiramisu.py
Normal file
2
test/data/compare/20_string/tiramisu.py
Normal file
@ -0,0 +1,2 @@
|
||||
parser.add_arguments(StrOption('foo', 'foo help'))
|
||||
|
2
test/data/compare/20_string_list/argparse.py
Normal file
2
test/data/compare/20_string_list/argparse.py
Normal file
@ -0,0 +1,2 @@
|
||||
parser.add_argument('--foo', help='foo help', nargs='*')
|
||||
|
2
test/data/compare/20_string_list/tiramisu.py
Normal file
2
test/data/compare/20_string_list/tiramisu.py
Normal file
@ -0,0 +1,2 @@
|
||||
parser.add_arguments(StrOption('foo', 'foo help', multi=True))
|
||||
|
2
test/data/compare/30_choice_int/argparse.py
Normal file
2
test/data/compare/30_choice_int/argparse.py
Normal file
@ -0,0 +1,2 @@
|
||||
parser.add_argument('--door', help='Door numbers', choices=[1, 2, 3])
|
||||
|
1
test/data/compare/30_choice_int/tiramisu.py
Normal file
1
test/data/compare/30_choice_int/tiramisu.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_arguments(ChoiceOption('door', 'Door numbers', (1, 2, 3)))
|
2
test/data/compare/30_string_default/argparse.py
Normal file
2
test/data/compare/30_string_default/argparse.py
Normal file
@ -0,0 +1,2 @@
|
||||
parser.add_argument('--foo', help='foo help', default='default', nargs='?')
|
||||
|
2
test/data/compare/30_string_default/tiramisu.py
Normal file
2
test/data/compare/30_string_default/tiramisu.py
Normal file
@ -0,0 +1,2 @@
|
||||
parser.add_arguments(StrOption('foo', 'foo help', 'default'))
|
||||
|
1
test/data/compare/30_string_short/argparse.py
Normal file
1
test/data/compare/30_string_short/argparse.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_argument('-f', '--foo', help='foo help')
|
3
test/data/compare/30_string_short/tiramisu.py
Normal file
3
test/data/compare/30_string_short/tiramisu.py
Normal file
@ -0,0 +1,3 @@
|
||||
str_long = StrOption('foo', 'foo help')
|
||||
str_short = SymLinkOption('f', str_long)
|
||||
parser.add_arguments([str_long, str_short])
|
2
test/data/compare/40_multi_bool/argparse.py
Normal file
2
test/data/compare/40_multi_bool/argparse.py
Normal file
@ -0,0 +1,2 @@
|
||||
parser.add_argument('-v', help='increase output verbosity', action='store_true')
|
||||
parser.add_argument('-s', help='second argument', action='store_true')
|
1
test/data/compare/40_multi_bool/tiramisu.py
Normal file
1
test/data/compare/40_multi_bool/tiramisu.py
Normal file
@ -0,0 +1 @@
|
||||
parser.add_arguments([BoolOption('v', 'increase output verbosity', default=False), BoolOption('s', 'second argument', default=False)])
|
3
test/data/compare/40_positional_optional/argparse.py
Normal file
3
test/data/compare/40_positional_optional/argparse.py
Normal file
@ -0,0 +1,3 @@
|
||||
parser.add_argument("echo", help="echo the string you use here")
|
||||
parser.add_argument('--verbosity', help='increase output verbosity', action='store_true')
|
||||
|
3
test/data/compare/40_positional_optional/tiramisu.py
Normal file
3
test/data/compare/40_positional_optional/tiramisu.py
Normal file
@ -0,0 +1,3 @@
|
||||
parser.add_arguments([StrOption('echo', 'echo the string you use here', properties=('mandatory', 'positional')),
|
||||
BoolOption('verbosity', 'increase output verbosity', default=False)])
|
||||
|
2
test/data/compare/40_short_long/argparse.py
Normal file
2
test/data/compare/40_short_long/argparse.py
Normal file
@ -0,0 +1,2 @@
|
||||
parser.add_argument('-v', help='increase output verbosity', action='store_true')
|
||||
parser.add_argument('-i', '--int', help='integer', type=int)
|
4
test/data/compare/40_short_long/tiramisu.py
Normal file
4
test/data/compare/40_short_long/tiramisu.py
Normal file
@ -0,0 +1,4 @@
|
||||
int_long = IntOption('int', 'integer')
|
||||
parser.add_arguments([BoolOption('v', 'increase output verbosity', default=False),
|
||||
int_long,
|
||||
SymLinkOption('i', int_long)])
|
145
test/test_simple.py
Normal file
145
test/test_simple.py
Normal file
@ -0,0 +1,145 @@
|
||||
from tiramisu import StrOption, BoolOption, IntOption, ChoiceOption, OptionDescription, SymLinkOption
|
||||
from py.test import raises, fixture
|
||||
from io import StringIO
|
||||
import sys
|
||||
from os import listdir
|
||||
from os.path import join, isdir
|
||||
from contextlib import redirect_stdout, redirect_stderr
|
||||
from argparse import ArgumentParser
|
||||
|
||||
#from pouet import TiramisuParser
|
||||
from tiramisu_parser import TiramisuParser
|
||||
|
||||
|
||||
DATA_DIR = 'test/data/compare'
|
||||
TEST_DIRS = []
|
||||
|
||||
|
||||
for test in listdir(DATA_DIR):
|
||||
test_file = join(DATA_DIR, test)
|
||||
if isdir(test_file):
|
||||
TEST_DIRS.append(test_file)
|
||||
|
||||
TEST_DIRS.sort()
|
||||
# TEST_DIRS.remove('test/data/compare/10_positional_list')
|
||||
# TEST_DIRS = ['test/data/compare/50_conditional_disable']
|
||||
|
||||
|
||||
@fixture(scope="module", params=TEST_DIRS)
|
||||
def test_list(request):
|
||||
return request.param
|
||||
|
||||
|
||||
def import_subfile_and_test(filename, parser, arg):
|
||||
parser_dict = []
|
||||
parser_system_err = []
|
||||
f = StringIO()
|
||||
with redirect_stderr(f):
|
||||
exec(open(filename).read())
|
||||
# print('arg', arg)
|
||||
try:
|
||||
parser_dict.append(parser.parse_args(arg).__dict__)
|
||||
except SystemExit as err:
|
||||
parser_system_err.append(str(err))
|
||||
else:
|
||||
parser_system_err.append(None)
|
||||
parser_error = f.getvalue()
|
||||
f = StringIO()
|
||||
with redirect_stdout(f):
|
||||
parser.print_help()
|
||||
parser_help = f.getvalue()
|
||||
return parser_dict, parser_system_err, parser_error, parser_help
|
||||
|
||||
|
||||
def test_files(test_list):
|
||||
args = [[],
|
||||
# 10_positional
|
||||
['bar'], ['foo', 'bar'],
|
||||
# 10_positional_int
|
||||
['4'],
|
||||
# 20_bool
|
||||
['--verbosity'], ['--verbosity', 'arg'],
|
||||
# 20_string
|
||||
['--foo'], ['--foo', '--bar'], ['--foo', 'a'],
|
||||
['--foo', 'a', '--foo', 'b'],
|
||||
# 20_int
|
||||
['--int', '3'], ['--int', 'a'],
|
||||
# 20 choice
|
||||
['--door', 'a'], ['--door', '1'],
|
||||
# 30_string_short
|
||||
['-f', 'b'], ['--foo', 'c', '-f', 'b'],
|
||||
# 40 multi_bool
|
||||
['-v'], ['-v', '-s'], ['-vs'],
|
||||
# 40_short_long
|
||||
['-v', '--foo', '1'], ['-vf', '2'], ['-vf'], ['-vf', '-v'],
|
||||
# 40_positional_optional
|
||||
['bar', '--verbosity'], ['--verbosity', 'bar'],
|
||||
]
|
||||
for arg in args:
|
||||
tiramparser = TiramisuParser('prog.py')
|
||||
tiramparser_dict, tiramparser_system_err, tiramparser_error, tiramparser_help = import_subfile_and_test(test_list + '/tiramisu.py',
|
||||
tiramparser, arg)
|
||||
#
|
||||
argparser = ArgumentParser('prog.py')
|
||||
argparser_dict, argparser_system_err, argparser_error, argparser_help = import_subfile_and_test(test_list + '/argparse.py',
|
||||
argparser, arg)
|
||||
#print(tiramparser_dict)
|
||||
#print(tiramparser_system_err)
|
||||
#print(tiramparser_error)
|
||||
#print(tiramparser_help)
|
||||
#print('-----')
|
||||
#print(argparser_dict)
|
||||
#print(argparser_system_err)
|
||||
#print(argparser_error)
|
||||
#print(argparser_help)
|
||||
assert tiramparser_dict == argparser_dict
|
||||
assert tiramparser_error == argparser_error
|
||||
assert tiramparser_help == argparser_help
|
||||
assert tiramparser_system_err == argparser_system_err
|
||||
|
||||
|
||||
#FIXME --verbose sans --quiet
|
||||
#parser = argparse.ArgumentParser(description="calculate X to the power of Y")
|
||||
#group = parser.add_mutually_exclusive_group()
|
||||
#group.add_argument("-v", "--verbose", action="store_true")
|
||||
#group.add_argument("-q", "--quiet", action="store_true")
|
||||
#parser.add_argument("x", type=int, help="the base")
|
||||
#parser.add_argument("y", type=int, help="the exponent")
|
||||
#args = parser.parse_args()
|
||||
#answer = args.x**args.y
|
||||
|
||||
|
||||
#FIXME --sum ?
|
||||
#parser = argparse.ArgumentParser(description='Process some integers.')
|
||||
#parser.add_argument('integers', metavar='N', type=int, nargs='+',
|
||||
# help='an integer for the accumulator')
|
||||
#parser.add_argument('--sum', dest='accumulate', action='store_const',
|
||||
# const=sum, default=max,
|
||||
# help='sum the integers (default: find the max)')
|
||||
#args = parser.parse_args()
|
||||
#print(args.accumulate(args.integers))
|
||||
|
||||
|
||||
# +++++++++++++++++++++++++++++ nargs
|
||||
#FIXME longueur fixe
|
||||
#>>> parser = argparse.ArgumentParser()
|
||||
#>>> parser.add_argument('--foo', nargs=2)
|
||||
#>>> parser.add_argument('bar', nargs=1)
|
||||
#>>> parser.parse_args('c --foo a b'.split())
|
||||
#Namespace(bar=['c'], foo=['a', 'b'])
|
||||
|
||||
|
||||
#FIXME const
|
||||
#>>> parser = argparse.ArgumentParser()
|
||||
#>>> parser.add_argument('--foo', nargs='?', const='c', default='d')
|
||||
#>>> parser.add_argument('bar', nargs='?', default='d')
|
||||
#>>> parser.parse_args(['XX', '--foo', 'YY'])
|
||||
#Namespace(bar='XX', foo='YY')
|
||||
#>>> parser.parse_args(['XX', '--foo'])
|
||||
#Namespace(bar='XX', foo='c')
|
||||
#>>> parser.parse_args([])
|
||||
#Namespace(bar='d', foo='d')
|
||||
|
||||
#FIXME ? | * | +
|
||||
# * == list
|
||||
# + == list + mandatory
|
Reference in New Issue
Block a user