From 6f5e8386ebd5d8084ea847000cec91af33896b7e Mon Sep 17 00:00:00 2001 From: Rahix Date: Fri, 23 Dec 2022 01:13:00 +0100 Subject: [PATCH] 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. --- tests/run.lua | 1 + tests/tests/misc.lua | 59 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tests/tests/misc.lua diff --git a/tests/run.lua b/tests/run.lua index 7bc6d8cbeea1..2b184726e94e 100755 --- a/tests/run.lua +++ b/tests/run.lua @@ -13,6 +13,7 @@ test_sources = { "tests/local.lua", "tests/network.lua", "tests/timeout.lua", + "tests/misc.lua", } for _, source in ipairs(test_sources) do loadfile("./tests/"..source)() diff --git a/tests/tests/misc.lua b/tests/tests/misc.lua new file mode 100644 index 000000000000..9ba5e31e6683 --- /dev/null +++ b/tests/tests/misc.lua @@ -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