216 lines
6.3 KiB
Python
216 lines
6.3 KiB
Python
from cutekit import cli2, utils
|
|
from asserts import (
|
|
assert_is,
|
|
assert_equal,
|
|
assert_is_instance,
|
|
)
|
|
|
|
# --- Parse Values ----------------------------------------------------------- #
|
|
|
|
|
|
def test_parse_int_val():
|
|
assert_equal(cli2.parseValue("1"), 1)
|
|
assert_equal(cli2.parseValue("2"), 2)
|
|
assert_equal(cli2.parseValue("+2"), +2)
|
|
assert_equal(cli2.parseValue("-2"), -2)
|
|
|
|
|
|
def test_parse_true_val():
|
|
assert_equal(cli2.parseValue("true"), True)
|
|
assert_equal(cli2.parseValue("True"), True)
|
|
assert_equal(cli2.parseValue("y"), True)
|
|
assert_equal(cli2.parseValue("yes"), True)
|
|
assert_equal(cli2.parseValue("Y"), True)
|
|
assert_equal(cli2.parseValue("Yes"), True)
|
|
|
|
|
|
def test_parse_false_val():
|
|
assert_equal(cli2.parseValue("false"), False)
|
|
assert_equal(cli2.parseValue("False"), False)
|
|
assert_equal(cli2.parseValue("n"), False)
|
|
assert_equal(cli2.parseValue("no"), False)
|
|
assert_equal(cli2.parseValue("N"), False)
|
|
assert_equal(cli2.parseValue("No"), False)
|
|
|
|
|
|
def test_parse_str_val():
|
|
assert_equal(cli2.parseValue("foo"), "foo")
|
|
assert_equal(cli2.parseValue("'foo'"), "foo")
|
|
assert_equal(cli2.parseValue('"foo"'), "foo")
|
|
|
|
|
|
def test_parse_list_val():
|
|
assert_equal(cli2.parseValue("foo,bar"), ["foo", "bar"])
|
|
assert_equal(cli2.parseValue("'foo','bar'"), ["foo", "bar"])
|
|
assert_equal(cli2.parseValue('"foo","bar"'), ["foo", "bar"])
|
|
|
|
|
|
# --- Parse Args ------------------------------------------------------------- #
|
|
|
|
|
|
def test_parse_short_arg():
|
|
args = cli2.parseArg("-a")
|
|
assert_equal(len(args), 1)
|
|
arg = args[0]
|
|
assert_is_instance(arg, cli2.ArgumentToken)
|
|
assert_equal(arg.key, "a")
|
|
assert_equal(arg.value, True)
|
|
|
|
|
|
def test_parse_short_args():
|
|
args = cli2.parseArg("-abc")
|
|
assert_equal(len(args), 3)
|
|
arg = args[0]
|
|
assert_is_instance(arg, cli2.ArgumentToken)
|
|
assert_equal(arg.key, "a")
|
|
assert_equal(arg.value, True)
|
|
|
|
arg = args[1]
|
|
assert_is_instance(arg, cli2.ArgumentToken)
|
|
assert_equal(arg.key, "b")
|
|
assert_equal(arg.value, True)
|
|
|
|
arg = args[2]
|
|
assert_is_instance(arg, cli2.ArgumentToken)
|
|
assert_equal(arg.key, "c")
|
|
assert_equal(arg.value, True)
|
|
|
|
|
|
def test_parse_long_arg():
|
|
args = cli2.parseArg("--foo")
|
|
assert_equal(len(args), 1)
|
|
arg = args[0]
|
|
assert_is_instance(arg, cli2.ArgumentToken)
|
|
assert_equal(arg.key, "foo")
|
|
assert_equal(arg.value, True)
|
|
|
|
|
|
def test_parse_long_arg_with_value():
|
|
args = cli2.parseArg("--foo=bar")
|
|
assert_equal(len(args), 1)
|
|
arg = args[0]
|
|
assert_is_instance(arg, cli2.ArgumentToken)
|
|
assert_equal(arg.key, "foo")
|
|
assert_equal(arg.value, "bar")
|
|
|
|
|
|
def test_parse_long_arg_with_value_list():
|
|
args = cli2.parseArg("--foo=bar,baz")
|
|
assert_equal(len(args), 1)
|
|
arg = args[0]
|
|
assert_is_instance(arg, cli2.ArgumentToken)
|
|
assert_equal(arg.key, "foo")
|
|
assert_equal(arg.value, ["bar", "baz"])
|
|
|
|
|
|
def test_parse_key_subkey_arg():
|
|
args = cli2.parseArg("--foo:bar")
|
|
assert_equal(len(args), 1)
|
|
arg = args[0]
|
|
assert_is_instance(arg, cli2.ArgumentToken)
|
|
assert_equal(arg.key, "foo")
|
|
assert_equal(arg.subkey, "bar")
|
|
assert_equal(arg.value, True)
|
|
|
|
|
|
# --- Extract Args ----------------------------------------------------------- #
|
|
|
|
|
|
def extractParse(type: type[utils.T], args: list[str]) -> utils.T:
|
|
schema = cli2.Schema.extract(type)
|
|
return schema.parse(args)
|
|
|
|
|
|
def test_cli_arg_int():
|
|
class IntArg:
|
|
value: int = cli2.arg(None, "value")
|
|
|
|
assert_equal(extractParse(IntArg, ["--value=-1"]).value, -1)
|
|
assert_equal(extractParse(IntArg, ["--value=0"]).value, 0)
|
|
assert_equal(extractParse(IntArg, ["--value=1"]).value, 1)
|
|
|
|
|
|
"""
|
|
class StrArg:
|
|
value: str = cli2.arg(None, "value")
|
|
|
|
|
|
def test_cli_arg_str1():
|
|
v = extractParse(StrArg, ["--value=foo"])
|
|
assert v.value == "foo"
|
|
|
|
|
|
def test_cli_arg_str2():
|
|
v = extractParse(StrArg, ["--value='foo, bar'"])
|
|
assert v.value == "foo, bar"
|
|
|
|
|
|
class BoolArg:
|
|
value: bool = cli2.arg(None, "value")
|
|
|
|
|
|
def test_cli_arg_bool():
|
|
assert_is(extractParse(BoolArg, ["--value"]).value, True)
|
|
assert_is(extractParse(BoolArg, ["--value=true"]).value, True)
|
|
assert_is(extractParse(BoolArg, ["--value=True"]).value, True)
|
|
assert_is(extractParse(BoolArg, ["--value=y"]).value, True)
|
|
assert_is(extractParse(BoolArg, ["--value=yes"]).value, True)
|
|
assert_is(extractParse(BoolArg, ["--value=Y"]).value, True)
|
|
assert_is(extractParse(BoolArg, ["--value=Yes"]).value, True)
|
|
assert_is(extractParse(BoolArg, ["--value=1"]).value, True)
|
|
|
|
assert extractParse(BoolArg, ["--value=false"]).value is False
|
|
assert extractParse(BoolArg, ["--value=False"]).value is False
|
|
assert extractParse(BoolArg, ["--value=n"]).value is False
|
|
assert extractParse(BoolArg, ["--value=no"]).value is False
|
|
assert extractParse(BoolArg, ["--value=N"]).value is False
|
|
assert extractParse(BoolArg, ["--value=No"]).value is False
|
|
print(extractParse(BoolArg, ["--value=0"]).value)
|
|
assert extractParse(BoolArg, ["--value=0"]).value is False
|
|
|
|
|
|
class IntListArg:
|
|
value: list[int] = cli2.arg(None, "value")
|
|
|
|
|
|
def test_cli_arg_list_int1():
|
|
assert_equal(extractParse(IntListArg, []).value, [])
|
|
assert_equal(extractParse(IntListArg, ["--value=1", "--value=2"]).value, [1, 2])
|
|
assert_equal(extractParse(IntListArg, ["--value=1,2"]).value, [1, 2])
|
|
|
|
|
|
class StrListArg:
|
|
value: list[str] = cli2.arg(None, "value")
|
|
|
|
|
|
def test_cli_arg_list_str():
|
|
assert_equal(
|
|
extractParse(StrListArg, ["--value=foo", "--value=bar"]).value,
|
|
[
|
|
"foo",
|
|
"bar",
|
|
],
|
|
)
|
|
|
|
assert_equal(extractParse(StrListArg, ["--value=foo,bar"]).value, ["foo", "bar"])
|
|
assert_equal(extractParse(StrListArg, ["--value=foo,bar"]).value, ["foo", "bar"])
|
|
assert_equal(extractParse(StrListArg, ["--value='foo,bar'"]).value, ["foo,bar"])
|
|
assert_equal(extractParse(StrListArg, ["--value='foo, bar'"]).value, ["foo, bar"])
|
|
assert_equal(extractParse(StrListArg, ['--value="foo, bar"']).value, ["foo, bar"])
|
|
|
|
|
|
class StrDictArg:
|
|
value: dict[str, str] = cli2.arg(None, "value")
|
|
|
|
|
|
def test_cli_arg_dict_str():
|
|
assert_equal(extractParse(StrDictArg, ["--value:foo=bar"]).value, {"foo": "bar"})
|
|
assert_equal(
|
|
extractParse(StrDictArg, ["--value:foo=bar", "--value:baz=qux"]).value,
|
|
{
|
|
"foo": "bar",
|
|
"baz": "qux",
|
|
},
|
|
)
|
|
|
|
"""
|