From 6953187e38de249a6581dcef50140ec0759c2f58 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sun, 13 May 2012 22:03:21 +0100 Subject: Add conditions to allow/deny in builtins --- lib/lace/builtin.lua | 20 +++++++++-- test/test-lace.builtin.lua | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 2 deletions(-) diff --git a/lib/lace/builtin.lua b/lib/lace/builtin.lua index fdfcefb..15b88af 100644 --- a/lib/lace/builtin.lua +++ b/lib/lace/builtin.lua @@ -32,8 +32,24 @@ local function get_set_last_result(newv) end local function _do_return(exec_context, result, reason, cond) - if #cond > 0 then - -- Run the conditions + for i = 1, #cond do + local name = cond[i] + local invert = false + if name:sub(1,1) == "!" then + invert = true + name = name:sub(2) + end + local res, msg = engine.test(exec_context, name) + if res == nil then + return nil, msg + end + if invert then + res = not res + end + if not res then + -- condition failed, return true to continue execution + return true + end end return result, reason end diff --git a/test/test-lace.builtin.lua b/test/test-lace.builtin.lua index b80df1c..a20f4cd 100644 --- a/test/test-lace.builtin.lua +++ b/test/test-lace.builtin.lua @@ -237,6 +237,96 @@ function suite.compile_builtin_define_ok() assert(ectx[".lace"].defs.fish.JEFF, "definition should have passed through") end +function suite.run_allow_deny_with_conditions_not_present() + local compctx = {[".lace"] = {}} + local cmdtab, msg = builtin.commands.allow(compctx, "allow", "because", "cheese") + assert(type(cmdtab) == "table", "Successful compilation returns tables") + assert(type(cmdtab.fn) == "function", "With functions") + local ectx = {} + local ok, msg = cmdtab.fn(ectx, unpack(cmdtab.args)) + assert(ok == nil, "Running a conditional allow where the conditions are not defined should fail") + assert(msg.msg:match("cheese"), "Resultant error should indicate missing variable name") +end + +function suite.run_allow_deny_with_condition_present_erroring() + local compctx = {[".lace"] = {}} + local cmdtab, msg = builtin.commands.allow(compctx, "allow", "because", "cheese") + assert(type(cmdtab) == "table", "Successful compilation returns tables") + assert(type(cmdtab.fn) == "function", "With functions") + local _cheesetab = { + fn = function() return nil, { msg = "CHEESE" } end, + args = {} + } + local ectx = {[".lace"] = {defs = { cheese = _cheesetab }}} + local ok, msg = cmdtab.fn(ectx, unpack(cmdtab.args)) + assert(ok == nil, "Running a conditional allow where the conditions error should fail") + assert(msg.msg:match("CHEESE"), "Resultant error should be passed") +end + +function suite.run_allow_deny_with_condition_present_failing() + local compctx = {[".lace"] = {}} + local cmdtab, msg = builtin.commands.allow(compctx, "allow", "because", "cheese") + assert(type(cmdtab) == "table", "Successful compilation returns tables") + assert(type(cmdtab.fn) == "function", "With functions") + local _cheesetab = { + fn = function() return false end, + args = {} + } + local ectx = {[".lace"] = {defs = { cheese = _cheesetab }}} + local ok, msg = cmdtab.fn(ectx, unpack(cmdtab.args)) + print(ok, msg) + assert(ok == true, "Running a conditional allow where the conditions fail should return continuation") +end + + +function suite.run_allow_deny_with_condition_present_passing() + local compctx = {[".lace"] = {}} + local cmdtab, msg = builtin.commands.allow(compctx, "allow", "because", "cheese") + assert(type(cmdtab) == "table", "Successful compilation returns tables") + assert(type(cmdtab.fn) == "function", "With functions") + local _cheesetab = { + fn = function() return true end, + args = {} + } + local ectx = {[".lace"] = {defs = { cheese = _cheesetab }}} + local ok, msg = cmdtab.fn(ectx, unpack(cmdtab.args)) + print(ok, msg) + assert(ok == "allow", "Running a conditional allow where the conditions pass should return allow") + assert(msg == "because", "Resulting in the reason given") +end + +function suite.run_allow_deny_with_inverted_condition_present_failing() + local compctx = {[".lace"] = {}} + local cmdtab, msg = builtin.commands.allow(compctx, "allow", "because", "!cheese") + assert(type(cmdtab) == "table", "Successful compilation returns tables") + assert(type(cmdtab.fn) == "function", "With functions") + local _cheesetab = { + fn = function() return true end, + args = {} + } + local ectx = {[".lace"] = {defs = { cheese = _cheesetab }}} + local ok, msg = cmdtab.fn(ectx, unpack(cmdtab.args)) + print(ok, msg) + assert(ok == true, "Running a conditional allow where the conditions fail should return continuation") +end + + +function suite.run_allow_deny_with_inverted_condition_present_passing() + local compctx = {[".lace"] = {}} + local cmdtab, msg = builtin.commands.allow(compctx, "allow", "because", "!cheese") + assert(type(cmdtab) == "table", "Successful compilation returns tables") + assert(type(cmdtab.fn) == "function", "With functions") + local _cheesetab = { + fn = function() return false end, + args = {} + } + local ectx = {[".lace"] = {defs = { cheese = _cheesetab }}} + local ok, msg = cmdtab.fn(ectx, unpack(cmdtab.args)) + print(ok, msg) + assert(ok == "allow", "Running a conditional allow where the conditions pass should return allow") + assert(msg == "because", "Resulting in the reason given") +end + local count_ok = 0 for _, testname in ipairs(testnames) do print("Run: " .. testname) -- cgit v1.2.1