Mercurial > prosody-hg
comparison spec/util_argparse_spec.lua @ 13733:48c056c10e5a 13.0
util.argparse: Add strict mode + tests
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Mon, 17 Feb 2025 18:24:23 +0000 |
| parents | 4ee9a912ceea |
| children | 81856814d74f |
comparison
equal
deleted
inserted
replaced
| 13732:1465b1e305df | 13733:48c056c10e5a |
|---|---|
| 22 assert.falsy(err); | 22 assert.falsy(err); |
| 23 assert.same({ foo = true, "bar", "--baz" }, opts); | 23 assert.same({ foo = true, "bar", "--baz" }, opts); |
| 24 assert.same({ "bar"; "--baz" }, arg); | 24 assert.same({ "bar"; "--baz" }, arg); |
| 25 end); | 25 end); |
| 26 | 26 |
| 27 it("allows continuation beyond first positional argument", function() | |
| 28 local arg = { "--foo"; "bar"; "--baz" }; | |
| 29 local opts, err = parse(arg, { stop_on_positional = false }); | |
| 30 assert.falsy(err); | |
| 31 assert.same({ foo = true, baz = true, "bar" }, opts); | |
| 32 -- All input should have been consumed: | |
| 33 assert.same({ }, arg); | |
| 34 end); | |
| 35 | |
| 27 it("expands short options", function() | 36 it("expands short options", function() |
| 28 local opts, err = parse({ "--foo"; "-b" }, { short_params = { b = "bar" } }); | 37 do |
| 29 assert.falsy(err); | 38 local opts, err = parse({ "--foo"; "-b" }, { short_params = { b = "bar" } }); |
| 30 assert.same({ foo = true; bar = true }, opts); | 39 assert.falsy(err); |
| 40 assert.same({ foo = true; bar = true }, opts); | |
| 41 end | |
| 42 | |
| 43 do | |
| 44 -- Same test with strict mode enabled and all parameters declared | |
| 45 local opts, err = parse({ "--foo"; "-b" }, { kv_params = { foo = true, bar = true }; short_params = { b = "bar" }, strict = true }); | |
| 46 assert.falsy(err); | |
| 47 assert.same({ foo = true; bar = true }, opts); | |
| 48 end | |
| 31 end); | 49 end); |
| 32 | 50 |
| 33 it("supports value arguments", function() | 51 it("supports value arguments", function() |
| 34 local opts, err = parse({ "--foo"; "bar"; "--baz=moo" }, { value_params = { foo = true; bar = true } }); | 52 local opts, err = parse({ "--foo"; "bar"; "--baz=moo" }, { value_params = { foo = true; bar = true } }); |
| 35 assert.falsy(err); | 53 assert.falsy(err); |
| 49 assert.equal("param-not-found", err); | 67 assert.equal("param-not-found", err); |
| 50 assert.equal("-h", where, "returned where"); | 68 assert.equal("-h", where, "returned where"); |
| 51 end); | 69 end); |
| 52 | 70 |
| 53 it("supports array arguments", function () | 71 it("supports array arguments", function () |
| 54 local opts, err = parse({ "--item"; "foo"; "--item"; "bar" }, { array_params = { item = true } }); | 72 do |
| 73 local opts, err = parse({ "--item"; "foo"; "--item"; "bar" }, { array_params = { item = true } }); | |
| 74 assert.falsy(err); | |
| 75 assert.same({"foo","bar"}, opts.item); | |
| 76 end | |
| 77 | |
| 78 do | |
| 79 -- Same test with strict mode enabled | |
| 80 local opts, err = parse({ "--item"; "foo"; "--item"; "bar" }, { array_params = { item = true }, strict = true }); | |
| 81 assert.falsy(err); | |
| 82 assert.same({"foo","bar"}, opts.item); | |
| 83 end | |
| 84 end) | |
| 85 | |
| 86 it("rejects unknown parameters in strict mode", function () | |
| 87 local opts, err, err2 = parse({ "--item"; "foo"; "--item"; "bar", "--foobar" }, { array_params = { item = true }, strict = true }); | |
| 88 assert.falsy(opts); | |
| 89 assert.same("param-not-found", err); | |
| 90 assert.same("--foobar", err2); | |
| 91 end); | |
| 92 | |
| 93 it("accepts known kv parameters in strict mode", function () | |
| 94 local opts, err = parse({ "--item=foo" }, { kv_params = { item = true }, strict = true }); | |
| 55 assert.falsy(err); | 95 assert.falsy(err); |
| 56 assert.same({"foo","bar"}, opts.item); | 96 assert.same("foo", opts.item); |
| 57 end) | 97 end); |
| 58 end); | 98 end); |
