added a few tests for :pparse()

This commit is contained in:
mpeterv
2014-02-18 15:27:58 +04:00
parent a7c1ec8634
commit a0fe631b08
2 changed files with 20 additions and 1 deletions

19
spec/pparse_spec.lua Normal file
View File

@@ -0,0 +1,19 @@
local Parser = require "argparse"
describe("tests related to :pparse()", function()
it("returns true and result on success", function()
local parser = Parser()
parser:option "-s" "--server"
local ok, args = parser:pparse{"--server", "foo"}
assert.is_true(ok)
assert.same({server = "foo"}, args)
end)
it("returns false and bare error message on failure", function()
local parser = Parser()
parser:argument "foo"
local ok, errmsg = parser:pparse{}
assert.is_false(ok)
assert.equal("too few arguments", errmsg)
end)
end)

View File

@@ -769,7 +769,7 @@ end
function Parser:pparse(args) function Parser:pparse(args)
local errmsg local errmsg
local ok, result = pcall(function() local ok, result = pcall(function()
return self:_parse(args, function(err) return self:_parse(args, function(parser, err)
errmsg = err errmsg = err
return error() return error()
end) end)