def test_init_arg(self): def func(x, y): return x * y parser = lp.Lazyparser(func, {}, {}) dic = { "help": lp.Argument("help", "help", str), "x": lp.Argument("x", inspect._empty, inspect._empty), "y": lp.Argument("y", inspect._empty, inspect._empty) } order = ["help", "x", "y"] assert parser.init_args() == (dic, order) lp.help_arg = False parser = lp.Lazyparser(func, {}, {}) dic = { "x": lp.Argument("x", inspect._empty, inspect._empty), "y": lp.Argument("y", inspect._empty, inspect._empty) } order = ["x", "y"] assert parser.init_args() == (dic, order) def func(help, y): return help * y parser.func = func self.assertRaises(SystemExit, parser.init_args)
def test_argparse_narg(self): arg = lp.Argument("lol", 7, float) assert arg.argparse_narg() is None arg = lp.Argument("lol", 7, List(vtype=float)) assert arg.argparse_narg() == "+" arg = lp.Argument("lol", 7, List(5, float)) assert arg.argparse_narg() == 5
def test_argparse_metavar(self): arg = lp.Argument("lol", 7, List(vtype=int)) assert arg.argparse_metavar() == "List[int]" arg = lp.Argument("lol", 7, List(size=5, vtype=int)) assert arg.argparse_metavar() == "List[5,int]" arg = lp.Argument("lol", 7, float) assert arg.argparse_metavar() == "float"
def test_test_type(self): def func(x, y): return x * y parser = lp.Lazyparser(func, {}, {}) parser = lp.init_parser(parser) def foo(x): return x * 2 arg = lp.Argument("lol", foo, List(vtype=int)) arg.value = 77 assert lp.test_type(arg, parser) arg.value = [17, 12] assert lp.test_type(arg, parser) arg = lp.Argument("lol", True, bool) arg.value = True assert lp.test_type(arg, parser) arg.value = "foo" self.assertRaises(SystemExit, lp.test_type, arg, parser) arg = lp.Argument("lol", inspect._empty, List(vtype=bool)) arg.value = [True, False, True, "False", "True", "foo"] self.assertRaises(SystemExit, lp.test_type, arg, parser) arg.value = [True, False, True, "False", "True"] assert lp.test_type(arg, parser)
def test_get_parser_group(self): arg = lp.Argument("lol", inspect._empty, int) assert arg.get_parser_group() == ["__rarg__", lp.required_title] arg = lp.Argument("lol", 6, int) assert arg.get_parser_group() == ["__parser__", lp.optionals_title] lp.groups = {"foo": ["lol", "help"]} lp.lpg_name = {"foo": "bar"} assert arg.get_parser_group() == ["__parser__", "foo"] lp.groups = {"foo": ["lol"]} assert arg.get_parser_group() == ["bar", "foo"]
def test_set_type(self): class Lol: pass arg = lp.Argument("lol", 7, int) assert arg.set_type(int) == int assert arg.set_type(inspect._empty) == inspect._empty self.assertRaises(SystemExit, arg.set_type, "bloup") self.assertRaises(SystemExit, arg.set_type, Lol) self.assertRaises(SystemExit, arg.set_type, List(vtype=Lol))
def test_message(self): arg = lp.Argument("t", 2, int) msg = "argument --t: Hello world" n = os.path.basename(sys.argv[0]) assert msg == lp.message("Hello world", arg, type_m=None) assert "%s: warning: " % n + msg == lp.message("Hello world", arg, type_m="w") assert "%s: error: " % n + msg == lp.message("Hello world", arg, type_m="e")
def test_handle_list_typing_type(self): arg = lp.Argument("lol", 7, int) assert int == lp.handle_list_typing_type(int) a = lp.handle_list_typing_type(List(vtype=int)) assert isinstance(a, List) and a.type == int and a.size == "+" b = lp.handle_list_typing_type(List(vtype=str, size=6)) assert isinstance(b, List) and b.type == str and b.size == 6 c = lp.handle_list_typing_type(typing.List[str]) assert isinstance(c, List) and c.type == str and c.size == "+" d = lp.handle_list_typing_type(typing.List[int]) assert isinstance(d, List) and d.type == int and d.size == "+"
def test_tests_function(self): def func(x, y): return x * y parser = lp.Lazyparser(func, {}, {}) parser = lp.init_parser(parser) arg = lp.Argument("lol", 7, int) arg.short_name = "l" arg.value = 5 arg.choice = " l > 10 " self.assertRaises(SystemExit, lp.tests_function, arg, parser) arg.choice = " lol bloup 10 " assert lp.tests_function(arg, parser) is None arg.choice = " bloup 10 " assert lp.tests_function(arg, parser) is None arg = lp.Argument("lol", [1, 2, 3], List(vtype=int)) arg.short_name = "l" arg.value = [1, 2, 3] arg.choice = " l > 10 " self.assertRaises(SystemExit, lp.tests_function, arg, parser) arg.choice = " lol < 10 " assert lp.tests_function(arg, parser) is None arg.choice = " lol fgh 10 " assert lp.tests_function(arg, parser) is None arg.choice = " fgh 10 " assert lp.tests_function(arg, parser) is None arg = lp.Argument("lol", "lul.txt", str) arg.short_name = "l" arg.value = 7 arg.choice = "file" self.assertRaises(SystemExit, lp.tests_function, arg, parser) arg.value = "foo.txt" self.assertRaises(SystemExit, lp.tests_function, arg, parser) arg = lp.Argument("lol", ["lul.txt"], List(vtype=str)) arg.short_name = "l" arg.value = ["boo.txt", "foo.txt", "bar.txt"] arg.choice = "file" self.assertRaises(SystemExit, lp.tests_function, arg, parser)
def test_get_type(self): argument = lp.Argument("t", 2, int) type_n = ["(int)", "(float)", "(str)", "(string)"] type_r = [int, float, str, str] for i in range(len(type_n)): res = lp.get_type(type_n[i], argument) assert isinstance(type_r[i], type(res[0])) assert type_n[i] == res[1] assert isinstance(lp.get_type("(List(vtype=int))", argument)[0], List) assert isinstance(lp.get_type("(List[int])", argument)[0], List) assert isinstance(lp.get_type("(List)", argument)[0], List) self.assertRaises(SystemExit, lp.get_type, "(gloubimou)", argument) self.assertRaises(SystemExit, lp.get_type, "(Function)", argument) self.assertRaises(SystemExit, lp.get_type, """(List(vtype="xd"))""", argument)
def test_argparse_choice(self): def foo(x): return x * 2 arg = lp.Argument("lol", 7, List(vtype=str)) assert arg.argparse_choice() is None arg.choice = "test" assert arg.argparse_choice() is None arg.choice = True assert arg.argparse_choice() == "True" arg.choice = [1, "foo", True] assert arg.argparse_choice() == [1, "foo", 'True'] arg.choice = [1, "foo", foo] self.assertRaises(SystemExit, arg.argparse_choice) arg.choice = foo self.assertRaises(SystemExit, arg.argparse_choice) arg.choice = 5 assert arg.argparse_choice() == 5
def test_argparse_type(self): arg = lp.Argument("lol", 7, bool) assert arg.argparse_type() == str arg = lp.Argument("lol", 7, List(vtype=int)) assert arg.argparse_type() == int arg = lp.Argument("lol", 7, List(vtype=bool)) assert arg.argparse_type() == str arg = lp.Argument("lol", 7, List(vtype=float)) assert arg.argparse_type() == float arg = lp.Argument("lol", 7, float) assert arg.argparse_type() == float arg = lp.Argument("lol", 7, int) assert arg.argparse_type() == int
def test_gfn(self): arg = lp.Argument("lol", 7, int) assert arg.gfn() == "--lol" arg.short_name = "l" assert arg.gfn() == "-l/--lol"
def test_get_type(self): arg = lp.Argument("lol", 7, int) assert arg.get_type() == int arg = lp.Argument("lol", 7, List(vtype=int)) assert arg.get_type() == List