mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 02:52:20 +00:00
more tests
This commit is contained in:
@@ -59,6 +59,20 @@ describe("tests related to positional arguments", function()
|
|||||||
args = parser:parse({"bar", "baz", "qu"})
|
args = parser:parse({"bar", "baz", "qu"})
|
||||||
assert.same(args, {foo1 = {"bar", "baz"}, foo2 = {"qu"}})
|
assert.same(args, {foo1 = {"bar", "baz"}, foo2 = {"qu"}})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("handles hyphen correctly", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:argument "foo"
|
||||||
|
local args = parser:parse({"-"})
|
||||||
|
assert.same(args, {foo = "-"})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles double hyphen correctly", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:argument "foo"
|
||||||
|
local args = parser:parse({"--", "-q"})
|
||||||
|
assert.same(args, {foo = "-q"})
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("passing incorrect arguments", function()
|
describe("passing incorrect arguments", function()
|
||||||
@@ -161,4 +175,4 @@ describe("tests related to positional arguments", function()
|
|||||||
assert.has_error(curry(parser.parse, parser, {}), "too few arguments")
|
assert.has_error(curry(parser.parse, parser, {}), "too few arguments")
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
@@ -28,6 +28,24 @@ describe("tests related to options", function()
|
|||||||
assert.same(args, {server = "foo"})
|
assert.same(args, {server = "foo"})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("handles GNU-style long options even when it could take more arguments", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-s", "--server", {
|
||||||
|
args = "*"
|
||||||
|
})
|
||||||
|
local args = parser:parse({"--server=foo"})
|
||||||
|
assert.same(args, {server = {"foo"}})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles GNU-style long options for multi-argument options", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-s", "--server", {
|
||||||
|
args = "1-2"
|
||||||
|
})
|
||||||
|
local args = parser:parse({"--server=foo", "bar"})
|
||||||
|
assert.same(args, {server = {"foo", "bar"}})
|
||||||
|
end)
|
||||||
|
|
||||||
it("handles short option correclty", function()
|
it("handles short option correclty", function()
|
||||||
local parser = largparse.parser()
|
local parser = largparse.parser()
|
||||||
parser:option("-s", "--server")
|
parser:option("-s", "--server")
|
||||||
@@ -151,6 +169,15 @@ describe("tests related to options", function()
|
|||||||
assert.same(args, {quiet = 3})
|
assert.same(args, {quiet = 3})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("overwrites old invocations", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-u", "--user", {
|
||||||
|
count = "0-2"
|
||||||
|
})
|
||||||
|
local args = parser:parse({"-uAlice", "--user=Bob", "--user", "John"})
|
||||||
|
assert.same(args, {user = {"Bob", "John"}})
|
||||||
|
end)
|
||||||
|
|
||||||
it("handles not used multi-count flag correctly", function()
|
it("handles not used multi-count flag correctly", function()
|
||||||
local parser = largparse.parser()
|
local parser = largparse.parser()
|
||||||
parser:flag("-q", "--quiet", {
|
parser:flag("-q", "--quiet", {
|
||||||
@@ -184,7 +211,19 @@ describe("tests related to options", function()
|
|||||||
assert.has_error(curry(parser.parse, parser, {"-sfoo", "bar"}), "too many arguments")
|
assert.has_error(curry(parser.parse, parser, {"-sfoo", "bar"}), "too many arguments")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- TODO: add more
|
it("doesn't accept GNU-like long options when it doesn't need arguments", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:flag("-q", "--quiet")
|
||||||
|
assert.has_error(curry(parser.parse, parser, {"--quiet=very_quiet"}), "option --quiet doesn't take arguments")
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles too many invocations correctly", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:flag("-q", "--quiet", {
|
||||||
|
count = 1,
|
||||||
|
no_overwrite = true
|
||||||
|
})
|
||||||
|
assert.has_error(curry(parser.parse, parser, {"-qq"}), "option -q must be used at most 1 times")
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
@@ -36,4 +36,4 @@ describe("tests related to utils.parse_boundaries", function()
|
|||||||
assert.equal(min, 42)
|
assert.equal(min, 42)
|
||||||
assert.equal(max, 96)
|
assert.equal(max, 96)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
Reference in New Issue
Block a user