aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_argparse_spec.lua
blob: 0f2430b7b1c7d3615e2cc904dd79b11e5844cb78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
describe("parse", function()
	local parse
	setup(function() parse = require"util.argparse".parse; end);

	it("works", function()
		-- basic smoke test
		local opts = parse({ "--help" });
		assert.same({ help = true }, opts);
	end);

	it("returns if no args", function() assert.same({}, parse({})); end);

	it("supports boolean flags", function()
		local opts, err = parse({ "--foo"; "--no-bar" });
		assert.falsy(err);
		assert.same({ foo = true; bar = false }, opts);
	end);

	it("consumes input until the first argument", function()
		local arg = { "--foo"; "bar"; "--baz" };
		local opts, err = parse(arg);
		assert.falsy(err);
		assert.same({ foo = true, "bar", "--baz" }, opts);
		assert.same({ "bar"; "--baz" }, arg);
	end);

	it("expands short options", function()
		local opts, err = parse({ "--foo"; "-b" }, { short_params = { b = "bar" } });
		assert.falsy(err);
		assert.same({ foo = true; bar = true }, opts);
	end);

	it("supports value arguments", function()
		local opts, err = parse({ "--foo"; "bar"; "--baz=moo" }, { value_params = { foo = true; bar = true } });
		assert.falsy(err);
		assert.same({ foo = "bar"; baz = "moo" }, opts);
	end);

	it("demands values for value params", function()
		local opts, err, where = parse({ "--foo" }, { value_params = { foo = true } });
		assert.falsy(opts);
		assert.equal("missing-value", err);
		assert.equal("--foo", where);
	end);

	it("reports where the problem is", function()
		local opts, err, where = parse({ "-h" });
		assert.falsy(opts);
		assert.equal("param-not-found", err);
		assert.equal("-h", where, "returned where");
	end);

end);