aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-10-12 14:48:21 +0200
committerKim Alvefur <zash@zash.se>2021-10-12 14:48:21 +0200
commit0c99443297976ed3ff83ec07c0301da830d6eb24 (patch)
tree99910edd814aed564150294b6475b239c54b8bee
parent8660ec61ce506c687402c942972c5fa42a417b6d (diff)
downloadprosody-0c99443297976ed3ff83ec07c0301da830d6eb24.tar.gz
prosody-0c99443297976ed3ff83ec07c0301da830d6eb24.zip
util.argparse: Tests
-rw-r--r--spec/util_argparse_spec.lua46
1 files changed, 46 insertions, 0 deletions
diff --git a/spec/util_argparse_spec.lua b/spec/util_argparse_spec.lua
new file mode 100644
index 00000000..6b024249
--- /dev/null
+++ b/spec/util_argparse_spec.lua
@@ -0,0 +1,46 @@
+describe("parse", function()
+ local parse
+ setup(function() parse = require"util.argparse".parse; end);
+
+ it("works", function()
+ -- basic smoke test
+ local opts = parse({ "--help" });
+ assert.same({ help = true }, opts);
+ end);
+
+ it("returns if no args", function() assert.same({}, parse({})); end);
+
+ it("supports boolean flags", function()
+ local opts, err = parse({ "--foo"; "--no-bar" });
+ assert.falsy(err);
+ assert.same({ foo = true; bar = false }, opts);
+ end);
+
+ it("consumes input until the first argument", function()
+ local arg = { "--foo"; "bar"; "--baz" };
+ local opts, err = parse(arg);
+ assert.falsy(err);
+ assert.same({ foo = true }, opts);
+ assert.same({ "bar"; "--baz" }, arg);
+ end);
+
+ it("expands short options", function()
+ local opts, err = parse({ "--foo"; "-b" }, { short_params = { b = "bar" } });
+ assert.falsy(err);
+ assert.same({ foo = true; bar = true }, opts);
+ end);
+
+ it("supports value arguments", function()
+ local opts, err = parse({ "--foo"; "bar"; "--baz=moo" }, { value_params = { foo = true; bar = true } });
+ assert.falsy(err);
+ assert.same({ foo = "bar"; baz = "moo" }, opts);
+ end);
+
+ it("demands values for value params", function()
+ local opts, err, where = parse({ "--foo" }, { value_params = { foo = true } });
+ assert.falsy(opts);
+ assert.equal("missing-value", err);
+ assert.equal("--foo", where);
+ end);
+
+end);