From a0fe631b08bf98015c96bb2007e0954c9057d822 Mon Sep 17 00:00:00 2001 From: mpeterv Date: Tue, 18 Feb 2014 15:27:58 +0400 Subject: [PATCH] added a few tests for :pparse() --- spec/pparse_spec.lua | 19 +++++++++++++++++++ src/argparse.lua | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 spec/pparse_spec.lua diff --git a/spec/pparse_spec.lua b/spec/pparse_spec.lua new file mode 100644 index 0000000..e0516ac --- /dev/null +++ b/spec/pparse_spec.lua @@ -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) diff --git a/src/argparse.lua b/src/argparse.lua index bf6e692..93551f1 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -769,7 +769,7 @@ end function Parser:pparse(args) local errmsg local ok, result = pcall(function() - return self:_parse(args, function(err) + return self:_parse(args, function(parser, err) errmsg = err return error() end)