Mercurial > prosody-hg
annotate spec/util_argparse_spec.lua @ 14217:7eb0e47418ab 13.0
util.human.io: Add simple version comparison
This is a small helper function that compares version string (it can be passed
to table.sort() as a comparator).
Full version sorts (e.g. semver, and natural/alphanumeric sort in general) are
more complex, and I would like to eventually support those. But this is a
stable branch and we don't need it for anything just yet.
Regarding justification for the stable branch:
The function is being committed with extensive tests, and isn't currently used
anywhere. The plan is to use it in an upcoming update of how TLS profiles are
configured, but the inputs in that case will be fixed (meaning that the risk
of breakage is lower than if we were running it against arbitrary data).
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 12 Jun 2026 11:38:39 +0100 |
| parents | 81856814d74f |
| children | aac30c5ee334 |
| rev | line source |
|---|---|
| 11843 | 1 describe("parse", function() |
| 2 local parse | |
| 3 setup(function() parse = require"util.argparse".parse; end); | |
| 4 | |
| 5 it("works", function() | |
| 6 -- basic smoke test | |
| 7 local opts = parse({ "--help" }); | |
| 8 assert.same({ help = true }, opts); | |
| 9 end); | |
| 10 | |
| 11 it("returns if no args", function() assert.same({}, parse({})); end); | |
| 12 | |
| 13 it("supports boolean flags", function() | |
| 14 local opts, err = parse({ "--foo"; "--no-bar" }); | |
| 15 assert.falsy(err); | |
| 16 assert.same({ foo = true; bar = false }, opts); | |
| 17 end); | |
| 18 | |
| 19 it("consumes input until the first argument", function() | |
| 20 local arg = { "--foo"; "bar"; "--baz" }; | |
| 21 local opts, err = parse(arg); | |
| 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 | 24 assert.same({ "bar"; "--baz" }, arg); |
| 25 end); | |
| 26 | |
|
13733
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
27 it("allows continuation beyond first positional argument", function() |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
28 local arg = { "--foo"; "bar"; "--baz" }; |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
29 local opts, err = parse(arg, { stop_on_positional = false }); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
30 assert.falsy(err); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
31 assert.same({ foo = true, baz = true, "bar" }, opts); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
32 -- All input should have been consumed: |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
33 assert.same({ }, arg); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
34 end); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
35 |
| 11843 | 36 it("expands short options", function() |
|
13733
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
37 do |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
38 local opts, err = parse({ "--foo"; "-b" }, { short_params = { b = "bar" } }); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
39 assert.falsy(err); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
40 assert.same({ foo = true; bar = true }, opts); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
41 end |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
42 |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
43 do |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
44 -- Same test with strict mode enabled and all parameters declared |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
45 local opts, err = parse({ "--foo"; "-b" }, { kv_params = { foo = true, bar = true }; short_params = { b = "bar" }, strict = true }); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
46 assert.falsy(err); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
47 assert.same({ foo = true; bar = true }, opts); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
48 end |
| 11843 | 49 end); |
| 50 | |
| 51 it("supports value arguments", function() | |
| 52 local opts, err = parse({ "--foo"; "bar"; "--baz=moo" }, { value_params = { foo = true; bar = true } }); | |
| 53 assert.falsy(err); | |
| 54 assert.same({ foo = "bar"; baz = "moo" }, opts); | |
| 55 end); | |
| 56 | |
|
13762
81856814d74f
util.argparse: Fix bug (regression?) in argument parsing with --foo=bar
Matthew Wild <mwild1@gmail.com>
parents:
13733
diff
changeset
|
57 it("supports value arguments in strict mode", function() |
|
81856814d74f
util.argparse: Fix bug (regression?) in argument parsing with --foo=bar
Matthew Wild <mwild1@gmail.com>
parents:
13733
diff
changeset
|
58 local opts, err = parse({ "--foo"; "bar"; "--baz=moo" }, { strict = true, value_params = { foo = true; baz = true } }); |
|
81856814d74f
util.argparse: Fix bug (regression?) in argument parsing with --foo=bar
Matthew Wild <mwild1@gmail.com>
parents:
13733
diff
changeset
|
59 assert.falsy(err); |
|
81856814d74f
util.argparse: Fix bug (regression?) in argument parsing with --foo=bar
Matthew Wild <mwild1@gmail.com>
parents:
13733
diff
changeset
|
60 assert.same({ foo = "bar"; baz = "moo" }, opts); |
|
81856814d74f
util.argparse: Fix bug (regression?) in argument parsing with --foo=bar
Matthew Wild <mwild1@gmail.com>
parents:
13733
diff
changeset
|
61 end); |
|
81856814d74f
util.argparse: Fix bug (regression?) in argument parsing with --foo=bar
Matthew Wild <mwild1@gmail.com>
parents:
13733
diff
changeset
|
62 |
| 11843 | 63 it("demands values for value params", function() |
| 64 local opts, err, where = parse({ "--foo" }, { value_params = { foo = true } }); | |
| 65 assert.falsy(opts); | |
| 66 assert.equal("missing-value", err); | |
| 67 assert.equal("--foo", where); | |
| 68 end); | |
| 69 | |
|
11845
97c1399720c2
util.argparse: Add test for #1691
Kim Alvefur <zash@zash.se>
parents:
11843
diff
changeset
|
70 it("reports where the problem is", function() |
|
97c1399720c2
util.argparse: Add test for #1691
Kim Alvefur <zash@zash.se>
parents:
11843
diff
changeset
|
71 local opts, err, where = parse({ "-h" }); |
|
97c1399720c2
util.argparse: Add test for #1691
Kim Alvefur <zash@zash.se>
parents:
11843
diff
changeset
|
72 assert.falsy(opts); |
|
97c1399720c2
util.argparse: Add test for #1691
Kim Alvefur <zash@zash.se>
parents:
11843
diff
changeset
|
73 assert.equal("param-not-found", err); |
|
97c1399720c2
util.argparse: Add test for #1691
Kim Alvefur <zash@zash.se>
parents:
11843
diff
changeset
|
74 assert.equal("-h", where, "returned where"); |
|
97c1399720c2
util.argparse: Add test for #1691
Kim Alvefur <zash@zash.se>
parents:
11843
diff
changeset
|
75 end); |
|
97c1399720c2
util.argparse: Add test for #1691
Kim Alvefur <zash@zash.se>
parents:
11843
diff
changeset
|
76 |
|
13160
4ee9a912ceea
util.argparse: Add support for repeatable parameters
Kim Alvefur <zash@zash.se>
parents:
12477
diff
changeset
|
77 it("supports array arguments", function () |
|
13733
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
78 do |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
79 local opts, err = parse({ "--item"; "foo"; "--item"; "bar" }, { array_params = { item = true } }); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
80 assert.falsy(err); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
81 assert.same({"foo","bar"}, opts.item); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
82 end |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
83 |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
84 do |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
85 -- Same test with strict mode enabled |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
86 local opts, err = parse({ "--item"; "foo"; "--item"; "bar" }, { array_params = { item = true }, strict = true }); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
87 assert.falsy(err); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
88 assert.same({"foo","bar"}, opts.item); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
89 end |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
90 end) |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
91 |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
92 it("rejects unknown parameters in strict mode", function () |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
93 local opts, err, err2 = parse({ "--item"; "foo"; "--item"; "bar", "--foobar" }, { array_params = { item = true }, strict = true }); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
94 assert.falsy(opts); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
95 assert.same("param-not-found", err); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
96 assert.same("--foobar", err2); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
97 end); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
98 |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
99 it("accepts known kv parameters in strict mode", function () |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
100 local opts, err = parse({ "--item=foo" }, { kv_params = { item = true }, strict = true }); |
|
13160
4ee9a912ceea
util.argparse: Add support for repeatable parameters
Kim Alvefur <zash@zash.se>
parents:
12477
diff
changeset
|
101 assert.falsy(err); |
|
13733
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
102 assert.same("foo", opts.item); |
|
48c056c10e5a
util.argparse: Add strict mode + tests
Matthew Wild <mwild1@gmail.com>
parents:
13160
diff
changeset
|
103 end); |
| 11843 | 104 end); |
