Annotate

spec/util_argparse_spec.lua @ 13270:14bbfb2cc8dd default tip

lint: Teach luacheck about module:once Silence warning for using this introduced in 9c62ffbdf2ae
author Kim Alvefur <zash@zash.se>
date Sun, 15 Oct 2023 16:43:14 +0200
parent 13160:4ee9a912ceea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11843
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 describe("parse", function()
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local parse
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 setup(function() parse = require"util.argparse".parse; end);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 it("works", function()
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 -- basic smoke test
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local opts = parse({ "--help" });
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 assert.same({ help = true }, opts);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 end);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 it("returns if no args", function() assert.same({}, parse({})); end);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 it("supports boolean flags", function()
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local opts, err = parse({ "--foo"; "--no-bar" });
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 assert.falsy(err);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 assert.same({ foo = true; bar = false }, opts);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 end);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 it("consumes input until the first argument", function()
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local arg = { "--foo"; "bar"; "--baz" };
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local opts, err = parse(arg);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 assert.falsy(err);
12477
cc84682b8429 util.argparse: Revise 553c6204fe5b with a different approach
Matthew Wild <mwild1@gmail.com>
parents: 11845
diff changeset
23 assert.same({ foo = true, "bar", "--baz" }, opts);
11843
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 assert.same({ "bar"; "--baz" }, arg);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 end);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 it("expands short options", function()
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local opts, err = parse({ "--foo"; "-b" }, { short_params = { b = "bar" } });
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 assert.falsy(err);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 assert.same({ foo = true; bar = true }, opts);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 end);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 it("supports value arguments", function()
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local opts, err = parse({ "--foo"; "bar"; "--baz=moo" }, { value_params = { foo = true; bar = true } });
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 assert.falsy(err);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 assert.same({ foo = "bar"; baz = "moo" }, opts);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 end);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 it("demands values for value params", function()
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 local opts, err, where = parse({ "--foo" }, { value_params = { foo = true } });
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 assert.falsy(opts);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 assert.equal("missing-value", err);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 assert.equal("--foo", where);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 end);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
11845
97c1399720c2 util.argparse: Add test for #1691
Kim Alvefur <zash@zash.se>
parents: 11843
diff changeset
46 it("reports where the problem is", function()
97c1399720c2 util.argparse: Add test for #1691
Kim Alvefur <zash@zash.se>
parents: 11843
diff changeset
47 local opts, err, where = parse({ "-h" });
97c1399720c2 util.argparse: Add test for #1691
Kim Alvefur <zash@zash.se>
parents: 11843
diff changeset
48 assert.falsy(opts);
97c1399720c2 util.argparse: Add test for #1691
Kim Alvefur <zash@zash.se>
parents: 11843
diff changeset
49 assert.equal("param-not-found", err);
97c1399720c2 util.argparse: Add test for #1691
Kim Alvefur <zash@zash.se>
parents: 11843
diff changeset
50 assert.equal("-h", where, "returned where");
97c1399720c2 util.argparse: Add test for #1691
Kim Alvefur <zash@zash.se>
parents: 11843
diff changeset
51 end);
97c1399720c2 util.argparse: Add test for #1691
Kim Alvefur <zash@zash.se>
parents: 11843
diff changeset
52
13160
4ee9a912ceea util.argparse: Add support for repeatable parameters
Kim Alvefur <zash@zash.se>
parents: 12477
diff changeset
53 it("supports array arguments", function ()
4ee9a912ceea util.argparse: Add support for repeatable parameters
Kim Alvefur <zash@zash.se>
parents: 12477
diff changeset
54 local opts, err = parse({ "--item"; "foo"; "--item"; "bar" }, { array_params = { item = true } });
4ee9a912ceea util.argparse: Add support for repeatable parameters
Kim Alvefur <zash@zash.se>
parents: 12477
diff changeset
55 assert.falsy(err);
4ee9a912ceea util.argparse: Add support for repeatable parameters
Kim Alvefur <zash@zash.se>
parents: 12477
diff changeset
56 assert.same({"foo","bar"}, opts.item);
4ee9a912ceea util.argparse: Add support for repeatable parameters
Kim Alvefur <zash@zash.se>
parents: 12477
diff changeset
57 end)
11843
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end);