You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
4.9 KiB

local test = require("u-test")
local serialization = require("serialization")
local BaseControl = require("bc")
function test.construct()
local function is_a_bc(bc)
test.is_true(bc.local_nouns ~= nil)
test.is_true(bc.local_verbs ~= nil)
end
-- Short init
is_a_bc(BaseControl())
is_a_bc(BaseControl:new())
is_a_bc(BaseControl:finalize())
is_a_bc(BaseControl:finalize({}, 10))
is_a_bc(BaseControl:finalize({}))
-- Long init
local bc = BaseControl:new()
bc:finalize()
is_a_bc(bc)
local bc = BaseControl:new()
bc:finalize({}, 10)
is_a_bc(bc)
local bc = BaseControl:new()
bc:finalize({})
is_a_bc(bc)
end
function test.register()
local bc = BaseControl:new()
bc:register("register1", 1234)
bc:finalize()
-- Register after finalizing
local bc = BaseControl:finalize()
test.error_raised(function()
bc:register("register2", 4321)
end, "after finalizing")
-- Register exisiting
local bc = BaseControl:new()
bc:register("register3", 123)
test.error_raised(function()
bc:register("register3", 12)
end, "already registered")
bc:register("register4", function() end)
test.error_raised(function()
bc:register("register4", function() end)
end, "already registered")
end
function test.register_batch()
local bc = BaseControl:new()
bc:register{
rb1=1234,
rb2=4321,
rb3v=function() end,
}
bc:finalize()
test.is_true(bc:has_noun("rb1"))
test.is_true(bc:has_noun("rb2"))
test.is_true(bc:has_verb("rb3v"))
end
function test.available()
local bc = BaseControl:new()
bc:register("available1", 1234)
bc:register("available2v", function() end)
bc:finalize()
test.equal(1234, bc.local_nouns["available1"])
test.is_true(bc.local_verbs["available2v"] ~= nil)
end
function test.get()
local bc = BaseControl:new()
bc:register("get1", 1234)
bc:register("get2v", function() end)
bc:finalize()
test.equal(1234, bc:get("get1"), "wrong value for noun")
test.error_raised(function()
bc:get("get2")
end, "unknown")
test.error_raised(function()
bc:get("get2v")
end, "not a noun")
end
function test.set()
local bc = BaseControl:new()
bc:register("set1", 31415)
bc:register("set2v", function() end)
bc:finalize()
test.equal(31415, bc:get("set1"), "wrong value for noun")
bc:set("set1", 14142)
test.equal(14142, bc:get("set1"), "wrong value for noun")
test.error_raised(function()
bc:set("set2", 12345)
end, "not a local")
test.error_raised(function()
bc:set("set2v", 12345)
end, "can't be cast into a noun")
bc:set("set2v", function() end)
test.error_raised(function()
bc:set("set1", function() end)
end, "can't be cast into a verb")
end
function test.has_iter_nouns()
local bc = BaseControl:new(
BaseControl.Network(31213) -- Network isolation
)
bc:register("hin1", 123)
bc:register("hin2v", function() end)
bc:register("hin3", 321)
bc:finalize()
test.is_true(bc:has_noun("hin1"), "exisiting noun not found")
test.is_false(bc:has_noun("hin2"), "non-existing noun found")
test.is_false(bc:has_noun("hin2v"), "verb found as noun")
test.is_true(bc:has_noun("hin3"), "exisiting noun not found")
local nouns = bc:nouns()
table.sort(nouns)
test.equal(2, #nouns, "noun-list incorrect")
test.equal("hin1", nouns[1], "noun-list incorrect")
test.equal("hin3", nouns[2], "noun-list incorrect")
end
function test.call_verb()
local bc = BaseControl:new()
local flag1, flag2 = 0, 0
bc:register("call1", function(a, b)
flag1 = a + b
flag2 = flag2 + 1
end)
bc:finalize()
local ret = bc:call("call1", 1, 2)
test.equal(3, flag1, "call failed")
test.equal(1, flag2, "call failed")
test.equal(true, ret, "call not attempted")
local ret = bc:call("call1", 10, 10)
test.equal(20, flag1, "call failed")
test.equal(2, flag2, "call failed")
test.equal(true, ret, "call not attempted")
local ret = bc:call("call2", 1, 2, 3)
test.equal(false, ret, "call erroneously attempted")
end
function test.has_iter_verbs()
local bc = BaseControl:new(
BaseControl.Network(31214) -- Network isolation
)
bc:register("hiv1v", function() end)
bc:register("hiv2", 123)
bc:register("hiv3v", function() end)
bc:finalize()
test.is_true(bc:has_verb("hiv1v"), "exisiting verb not found")
test.is_false(bc:has_verb("hiv2v"), "non-existing verb found")
test.is_false(bc:has_verb("hiv2"), "noun found as verb")
test.is_true(bc:has_verb("hiv3v"), "exisiting verb not found")
local verbs = bc:verbs()
table.sort(verbs)
test.equal(2, #verbs, "verb-list incorrect")
test.equal("hiv1v", verbs[1], "verb-list incorrect")
test.equal("hiv3v", verbs[2], "verb-list incorrect")
end
function test.iter_empty()
local bc = BaseControl:new(
BaseControl.Network(31211) -- Network isolation
):finalize()
test.equal(31211, bc.network.port, "wrong port")
test.equal(0, #bc:nouns(), "nouns found in empty network")
test.equal(0, #bc:verbs(), "verbs found in empty network")
end