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_equal(self): def func(x, y): return x * y parser = lp.Lazyparser(func, {}, {}) parser2 = lp.Lazyparser(func, {}, {}) assert parser == parser2 def func2(x, z): return x * z parser2 = lp.Lazyparser(func2, {}, {}) assert parser != parser2
def test_get_order(self): lp.set_groups() lp.set_env() def func(v, w, x, y, z): return v + w + x + y + z parser = lp.Lazyparser(func, {}, {}) assert parser.get_order() == parser.order lp.grp_order = ["Foo", "Optional arguments"] self.assertRaises(SystemExit, parser.get_order) lp.groups = {"Foo": ["v", "w", "x"]} lp.lpg_name = {"Foo": "Foo"} parser = lp.Lazyparser(func, {}, {}) assert parser.get_order() == ["v", "w", "x", "help", "y", "z"]
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_update_type(self): def func(x, y): return x * y parser = lp.Lazyparser(func, {}, {}) assert parser.args["x"].type == inspect._empty parser.update_type("x", ["qsfuhg", "srighdo", "()"]) assert parser.args["x"].type == inspect._empty parser.update_type("x", ["(int)"]) print(parser.args["x"].type) assert parser.args["x"].type == int parser.update_type("x", ["qsfuhg", "srighdo", "(str)", "(float)"]) assert parser.args["x"].type == str
def test_set_constrain(self): lp.set_env() def func(x): return x * 2 parser = lp.Lazyparser(func, {}, {}) assert parser.args["x"].choice is None parser.set_constrain({"x": "file"}) assert parser.args["x"].choice == "file" parser.set_constrain({"x": "x > 5"}) assert parser.args["x"].choice == " x > 5 " parser.set_constrain({"x": 5}) assert parser.args["x"].choice == 5
def test_update_param(self): lp.set_env(tb=12) def func(x, y): return x * y parser = lp.Lazyparser(func, {}, {}) parser.func.__doc__ = """Multiply x by y Take two number and multiply them. :param x: (int) a number x :param y: (int) a number y""" assert parser.args["x"].help == "param x" assert parser.args["y"].type == inspect._empty parser.update_param() assert parser.args["x"].help == "a number x" assert parser.args["y"].help == "a number y" assert parser.args["x"].type == int assert parser.args["y"].type == int lp.set_env(delim1="", tb=12) def func(x, y): return x * y parser = lp.Lazyparser(func, {}, {}) parser.func.__doc__ = """Multiply x by y Take two number and multiply them. x: (int) a number : x y: (int) a number : y""" parser.update_param() assert parser.args["x"].help == "a number : x" assert parser.args["y"].help == "a number : y" assert parser.args["x"].type == int assert parser.args["y"].type == int
def test_description(self): lp.set_env(delim1=":param", delim2=":", hd="", tb=12) def func(x, y): return x * y parser = lp.Lazyparser(func, {}, {}) assert parser.description() == "" parser.func.__doc__ = """Multiply x by y Take two number and multiply them. @Keyword :param x: (int) a number x :param y: (int) a number y""" desc = 'Multiply x by yTake two number and multiply them.@Keyword' assert parser.description().replace("\n", "") == desc lp.set_env(delim1="", delim2=":", hd="@Keyword", tb=12) desc = """Multiply x by yTake two number and multiply them.""" assert parser.description().replace("\n", "") == desc
def test_init_parser(self): lp.set_env(tb=17) def func(x, y, z=5, w=7): return x * y + z doc = """Multiply x by y and add z Take two number and multiply them. :param x: (int) a number x :param y: (int) a number y :param z: (int) a number z""" func.__doc__ = doc myparser = lp.Lazyparser(func, {"z": 10}, {}) parser = lp.init_parser(myparser) assert isinstance(parser, ArgumentParser) assert parser.description.replace("\n", "") == \ """Multiply x by y and add zTake two number and multiply them."""
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_set_filled(self): lp.set_env() def func(x): return x * 2 parser = lp.Lazyparser(func, {}, {}) parser.set_filled(const="lolipop") assert parser.args["x"].const == "$$void$$" parser.args["x"].type = int self.assertRaises(SystemExit, parser.set_filled, const={"x": 7}) parser.args["x"].default = 3 parser.set_filled(const={"x": 7}) assert parser.args["x"].const == 7 parser.args["x"].type = float self.assertRaises(SystemExit, parser.set_filled, const={"x": "b"}) parser.args["x"].default = "lul" self.assertRaises(SystemExit, parser.set_filled, const={"x": 7}) parser.args["x"].default = 6 parser.args["x"].type = List self.assertRaises(SystemExit, parser.set_filled, const={"x": "bloup"}) self.assertRaises(SystemExit, parser.set_filled, const={"x": (1, 2, 3)}) parser.args["x"].type = FileType self.assertRaises(SystemExit, parser.set_filled, const={"x": "bb"}) class Lol: pass parser.args["x"].type = Lol self.assertRaises(SystemExit, parser.set_filled, const={"x": "bb"}) parser.args["x"].type = int self.assertRaises(SystemExit, parser.set_filled, const={"x": "foo"}) parser.args["x"].default = "bar" self.assertRaises(SystemExit, parser.set_filled, const={"x": 7})