Annotate

spec/util_argparse_spec.lua @ 11843:d2a939e21671

util.argparse: Tests
author Kim Alvefur <zash@zash.se>
date Tue, 12 Oct 2021 14:48:21 +0200
child 11845:97c1399720c2
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
d2a939e21671 util.argparse: Tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 end);