Compare commits

..

2 commits

Author SHA1 Message Date
6f5e8386eb Add tests for local uuid and os.clock
To get execution coverage of bc.lua to 100%, add two more tests:

1. The first one ensures that os.clock is used properly when
   computer.uptime() is not available.
2. The second one tests the fallback implementation of uuid generation
   when uuid.next() is not available.
2022-12-23 15:40:23 +01:00
c4e0d9dfe7 Improve test coverage
Add missing tests to fully cover the BaseControl codebase (execution
coverage).  The only missing pieces are now uuid and os.clock coverage.
Importantly, these changes do not guarantee full coverage of failure
modes - but at least more possible failures are covered now.
2022-12-23 15:29:01 +01:00
6 changed files with 110 additions and 4 deletions

View file

@ -13,6 +13,7 @@ test_sources = {
"tests/local.lua", "tests/local.lua",
"tests/network.lua", "tests/network.lua",
"tests/timeout.lua", "tests/timeout.lua",
"tests/misc.lua",
} }
for _, source in ipairs(test_sources) do for _, source in ipairs(test_sources) do
loadfile("./tests/"..source)() loadfile("./tests/"..source)()

View file

@ -68,6 +68,32 @@ function test.clean_network2()
end, "unknown") end, "unknown")
end end
function test.clean_network_listener()
local bc1 = BaseControl:new()
local addr1 = network.get_scene()
bc1:register("clnlin1", 1234)
bc1:finalize()
local bc2 = BaseControl:new()
local addr2 = network.get_scene()
local tmp
bc2:listen("clnlin1", bc1.Query.Change, function(new)
test.equal(tmp, new, "value did not propagate")
end)
network.set_scene(addr1)
tmp = 13
bc1:set("clnlin1", 13)
network.set_scene(addr2)
bc2:close()
network.set_scene(addr1)
-- if the listener wasn't cleaned properly, this will fail
bc1:set("clnlin1", 16)
end
function test.unclean_deregister() function test.unclean_deregister()
local bc = BaseControl:new() local bc = BaseControl:new()
local addr = network.get_scene() local addr = network.get_scene()

View file

@ -77,6 +77,7 @@ function test.finalize_robust()
serialization.serialize({ serialization.serialize({
ty=BaseControl.Message.Register, ty=BaseControl.Message.Register,
nouns={"finaln2"}, nouns={"finaln2"},
verbs={"finalv2", "finalv3"},
}) })
) )
@ -96,11 +97,14 @@ function test.finalize_robust()
BaseControl.Network.default_port, BaseControl.Network.default_port,
serialization.serialize({ serialization.serialize({
ty=BaseControl.Message.Register, ty=BaseControl.Message.Register,
nouns={"finaln3"}, nouns={"finaln3", "finaln4"},
}) })
) )
bc2:finalize{"finaln1", "finaln2", "finaln3"} bc2:finalize{
"finaln1", "finaln2", "finaln3",
"finaln4", "finalv2", "finalv3",
}
test.is_true(bc2:has_noun("finaln1"), "noun missing") test.is_true(bc2:has_noun("finaln1"), "noun missing")
test.is_true(bc2:has_noun("finaln2"), "noun missing") test.is_true(bc2:has_noun("finaln2"), "noun missing")

View file

@ -47,6 +47,11 @@ function test.register()
test.error_raised(function() test.error_raised(function()
bc:register("register3", 12) bc:register("register3", 12)
end, "already registered") end, "already registered")
bc:register("register4", function() end)
test.error_raised(function()
bc:register("register4", function() end)
end, "already registered")
end end
function test.register_batch() function test.register_batch()
@ -143,13 +148,18 @@ function test.call_verb()
end) end)
bc:finalize() bc:finalize()
bc:call("call1", 1, 2) local ret = bc:call("call1", 1, 2)
test.equal(3, flag1, "call failed") test.equal(3, flag1, "call failed")
test.equal(1, flag2, "call failed") test.equal(1, flag2, "call failed")
test.equal(true, ret, "call not attempted")
bc:call("call1", 10, 10) local ret = bc:call("call1", 10, 10)
test.equal(20, flag1, "call failed") test.equal(20, flag1, "call failed")
test.equal(2, flag2, "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 end
function test.has_iter_verbs() function test.has_iter_verbs()

59
tests/tests/misc.lua Normal file
View file

@ -0,0 +1,59 @@
local test = require("u-test")
local serialization = require("serialization")
local computer = require("computer")
local uuid = require("uuid")
local computer_uptime_saved
function test.os_clock.start_up()
computer_uptime_saved = computer.uptime
computer.uptime = nil
end
function test.os_clock.tear_down()
computer.uptime = computer_uptime_saved
-- Force reload of the module
package.loaded.bc = nil
local BaseControl = require("bc")
end
function test.os_clock.test_monkeypatched()
-- Force reload of the module
package.loaded.bc = nil
local BaseControl = require("bc")
-- Test a timeout to prove it works
local bc1 = BaseControl:new()
test.error_raised(function()
bc1:finalize({"to_final1"}, 0.1)
end, "timeout")
end
local uuid_next_saved
function test.uuid.start_up()
uuid_next_saved = uuid.next
uuid.next = nil
end
function test.uuid.tear_down()
uuid.next = uuid_next_saved
-- Force reload of the module
package.loaded.bc = nil
local BaseControl = require("bc")
end
function test.uuid.test_monkeypatched()
-- Force reload of the module
package.loaded.bc = nil
local BaseControl = require("bc")
-- Test a listener as this will use a uuid
local bc = BaseControl:new()
bc:register("lisuuid1", 1234)
bc:finalize()
local id1 = bc:listen(
"lisuuid1", bc.Query.Change, function(new) end)
local id2 = bc:listen(
"lisuuid1", bc.Query.Change, function(new) end)
test.not_equal(id1, id2, "uuid collision")
end

View file

@ -64,6 +64,9 @@ function test.call_sync()
test.equal(40, ret, "local sync not correct") test.equal(40, ret, "local sync not correct")
test.equal("hello", val, "local sync not multiple") test.equal("hello", val, "local sync not multiple")
test.equal(2, n, "wrong invokation number") test.equal(2, n, "wrong invokation number")
test.error_raised(function()
bc1:call_sync("sync404", 1, 2, 3)
end, "unknown verb")
-- Test remote -- Test remote
local bc2 = BaseControl:new() local bc2 = BaseControl:new()
@ -72,4 +75,7 @@ function test.call_sync()
test.equal(40, ret, "local sync not correct") test.equal(40, ret, "local sync not correct")
test.equal("hello", val, "local sync not multiple") test.equal("hello", val, "local sync not multiple")
test.equal(4, n, "wrong invokation number") test.equal(4, n, "wrong invokation number")
test.error_raised(function()
bc2:call_sync("sync404", 1, 2, 3)
end, "unknown verb")
end end