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_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_list(self): v = List() assert v.size == "+" assert v.type == str assert v.value is None v = List(4, int, [1, 2, 3]) assert v.size == 4 assert v.type == int assert v.value == [1, 2, 3] self.assertRaises(SystemExit, List, 4, int, 7) self.assertRaises(SystemExit, List, "22u", int, 7)
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_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_handled_type(self): for o in ["s", "m"]: assert lp.handled_type(str, o) assert lp.handled_type(int, o) assert lp.handled_type(bool, o) assert lp.handled_type(float, o) assert not lp.handled_type(FileType("w"), o) assert not lp.handled_type(854, o) assert not lp.handled_type("blip", o) assert not lp.handled_type(sum, o) assert lp.handled_type(List) assert lp.handled_type(List(size=5, vtype=int)) assert not lp.handled_type(List, "s") assert not lp.handled_type(List(size=5, vtype="int"), "s")
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_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_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_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