def test_parse(self): lp.set_env(tb=12) @lp.parse(x="x < 10") def multiply(x, y): """ Multiply a by b. :param x: (int) a number x :param y: (int) a number y :return: x *y """ return x * y self.assertRaises(SystemExit, multiply) sys.argv = [sys.argv[0]] for word in "-x 7 -y 8".split(): sys.argv.append(word) res = multiply() assert res == 7 * 8 @lp.parse def multiply(x, y): """ Multiply a by b. :param x: (int) a number x :param y: (int) a number y :return: x *y """ return x * y assert multiply() == 7 * 8
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_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_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_flag(self): lp.set_env(tb=12) @lp.flag(y=1) @lp.parse(x="x < 10") def multiply(x, y=10): """ Multiply a by b. :param x: (int) a number x :param y: (int) a number y :return: x * y """ return x * y self.assertRaises(SystemExit, multiply) sys.argv = [sys.argv[0]] for word in "-x 7 -y".split(): sys.argv.append(word) assert multiply() == 7 sys.argv = [sys.argv[0]] for word in "-x 7".split(): sys.argv.append(word) assert multiply() == 70 @lp.flag @lp.parse(x="x < 10") def multiply(x, y=10): """ Multiply a by b. :param x: (int) a number x :param y: (int) a number y :return: x * y """ return x * y assert multiply() == 70
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_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})
def test_set_env(self): self.assertRaises(SystemExit, lp.set_env, ":param", "", "", 4) self.assertRaises(SystemExit, lp.set_env, "", "", "", "4") self.assertRaises(SystemExit, lp.set_env, 5, "", "", "4") assert lp.set_env("", ":", "", 4) is None assert lp.set_env("param", ":", "Keyword", 4) is None