Annotate

spec/util_argparse_spec.lua @ 11977:9f7a6f7d13de

mod_smacks: Factor out formatting of 'h' value
author Kim Alvefur <zash@zash.se>
date Wed, 01 Dec 2021 16:20:40 +0100
parent 11845:97c1399720c2
child 12477:cc84682b8429
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);
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 assert.same({ foo = true }, opts);
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
11843
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end);