summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2012-05-13 20:36:18 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2012-05-13 20:36:18 +0100
commit83efa26e56e108f32da37c79bdeeb809e1dcae12 (patch)
tree7363c671c7cfa4c9f13ed7f2e7ab8911368b58ac
parent14fa86bb11d7564dc47185e30f45fb92c50ef16e (diff)
downloadlace-83efa26e56e108f32da37c79bdeeb809e1dcae12.tar.gz
Behaviour for 'default' in lace.builtin
-rw-r--r--lib/lace/builtin.lua31
-rw-r--r--test/test-lace.builtin.lua65
2 files changed, 96 insertions, 0 deletions
diff --git a/lib/lace/builtin.lua b/lib/lace/builtin.lua
index 209916d..a45051f 100644
--- a/lib/lace/builtin.lua
+++ b/lib/lace/builtin.lua
@@ -13,6 +13,8 @@ local function compiler()
return require "lace.compiler"
end
+--[ Allow and Deny ]------------------------------------------------
+
local function _do_return(exec_context, result, reason, cond)
if #cond > 0 then
-- Run the conditions
@@ -40,6 +42,35 @@ end
builtin.allow = _return
builtin.deny = _return
+--[ Default for Allow and Deny ]------------------------------------
+
+function builtin.default(compcontext, def, result, reason, unwanted)
+ assert(def == "default", "Somehow, builtin.default got something odd")
+ if type(result) ~= "string" then
+ return compiler().error("Expected result, got nothing")
+ end
+ if result ~= "allow" and result ~= "deny" then
+ return compiler().error("Result wasn't allow or deny", {2})
+ end
+ if type(reason) ~= "string" then
+ reason = "Default behaviour"
+ end
+ if unwanted ~= nil then
+ return compiler().error("Unexpected additional content", {4})
+ end
+
+ if compcontext[".lace"].default then
+ return compiler().error("Cannot change the default")
+ end
+
+ compcontext[".lace"].default = { result, reason }
+
+ return {
+ fn = function() return true end,
+ args = {}
+ }
+end
+
return {
commands = builtin,
}
diff --git a/test/test-lace.builtin.lua b/test/test-lace.builtin.lua
index 67f0166..10c5c8f 100644
--- a/test/test-lace.builtin.lua
+++ b/test/test-lace.builtin.lua
@@ -61,6 +61,71 @@ function suite.run_builtin_allow_deny_novariables()
assert(msg == "because", "Expected reason should be 'because'")
end
+function suite.compile_builtin_default_noresult()
+ local compctx = {[".lace"] = {}}
+ local cmdtab, msg = builtin.commands.default(compctx, "default")
+ assert(cmdtab == false, "Internal errors should return false")
+ assert(type(msg) == "table", "Internal errors should return tables")
+ assert(type(msg.msg) == "string", "Internal errors should have string messages")
+ assert(msg.msg:match("Expected result"), "Expected error should mention a lack of result")
+end
+
+function suite.compile_builtin_default_resultbad()
+ local compctx = {[".lace"] = {}}
+ local cmdtab, msg = builtin.commands.default(compctx, "default", "FISH")
+ assert(cmdtab == false, "Internal errors should return false")
+ assert(type(msg) == "table", "Internal errors should return tables")
+ assert(type(msg.msg) == "string", "Internal errors should have string messages")
+ assert(msg.msg:match("allow or deny"), "Expected error should mention a bad of result")
+end
+
+function suite.compile_builtin_default_extra_fluff()
+ local compctx = {[".lace"] = {}}
+ local cmdtab, msg = builtin.commands.default(compctx, "default", "allow", "", "unwanted")
+ assert(cmdtab == false, "Internal errors should return false")
+ assert(type(msg) == "table", "Internal errors should return tables")
+ assert(type(msg.msg) == "string", "Internal errors should have string messages")
+ assert(msg.msg:match("additional"), "Expected error should mention additional content")
+end
+
+function suite.compile_builtin_default_ok()
+ local compctx = {[".lace"] = {}}
+ local cmdtab, msg = builtin.commands.default(compctx, "default", "allow", "because")
+ assert(type(cmdtab) == "table", "Successful compilation should return a table")
+ assert(type(cmdtab.fn) == "function", "With a function")
+ assert(type(cmdtab.args) == "table", "And an arg table")
+ assert(cmdtab.fn() == true, "Default command should always return true")
+ assert(type(compctx[".lace"].default) == "table", "Default should always set up the context")
+ assert(#compctx[".lace"].default == 2, "Default table should consist of two entries")
+ assert(compctx[".lace"].default[1] == "allow", "First entry should be the result")
+ assert(compctx[".lace"].default[2] == "because", "Second entry should be the reason")
+end
+
+function suite.compile_builtin_default_twice()
+ local compctx = {[".lace"] = {}}
+ local cmdtab, msg = builtin.commands.default(compctx, "default", "allow", "")
+ assert(type(cmdtab) == "table", "Successful compilation should return a table")
+ local cmdtab, msg = builtin.commands.default(compctx, "default", "allow", "")
+ assert(cmdtab == false, "Internal errors should return false")
+ assert(type(msg) == "table", "Internal errors should return tables")
+ assert(type(msg.msg) == "string", "Internal errors should have string messages")
+ assert(msg.msg:match("change the"), "Expected error should mention changing the default")
+end
+
+function suite.compile_builtin_default_noreason()
+ local compctx = {[".lace"] = {}}
+ local cmdtab, msg = builtin.commands.default(compctx, "default", "allow")
+ assert(type(cmdtab) == "table", "Successful compilation should return a table")
+ assert(type(cmdtab.fn) == "function", "With a function")
+ assert(type(cmdtab.args) == "table", "And an arg table")
+ assert(cmdtab.fn() == true, "Default command should always return true")
+ assert(type(compctx[".lace"].default) == "table", "Default should always set up the context")
+ assert(#compctx[".lace"].default == 2, "Default table should consist of two entries")
+ assert(compctx[".lace"].default[1] == "allow", "First entry should be the result")
+ assert(compctx[".lace"].default[2] == "Default behaviour", "Second entry should be the reason")
+end
+
+
local count_ok = 0
for _, testname in ipairs(testnames) do
print("Run: " .. testname)