nodemcu-firmware/tests/NTest_tmr.lua

100 lines
2.2 KiB
Lua
Raw Permalink Normal View History

local N = ...
N = (N or require "NTest")("tmr")
Create NodeMCU test system based on gambiarra (#2984) * Create mispec_file.lua * Initial commit of gambiarra * Adapt gambiarra to NodeMCU * adapt to NodeMCU spacing and add nok functionality * Some refactoring to make it easier to add new functionality * Add methode `fail` to check failing code and pass error messages to output - fail can be called with a function that should fail and a string which should be contained in the errormessage. - Pass failed check reasons to output. * Create gambiarra_file.lua * Add reporting of tests that failed with Lua error * ok, nok and fail will terminate the running test * Add capability to run sync and async tests in mixed order and have a task.post inbetween them * fix gambiarra self test to also run on device (not only host) Use less ram in checking tests directly after they ran. Use nateie task.post to tame watchdog. * Update file tests + add async tmr tests * Another fix in executing async test * Catch errors in callbacks using node.setonerror * change interface to return an object with several test methods * Update README.md * Change interface of Gambiarra + add reason for failed eq * Update gambiarra documentation * Add coroutine testcases to gambiarra * Delete mispec_file.lua as it is superseeded by gambiarra_file.lua * improve regexp for stack frame extraction * Use Lua 53 debug capabilities * move actual tests upfront * remove debug code + optimization * Show errors immediately instead of at the end of the test, freeing memory earlier * Split tests to be run in 2 tranches * rename to NTest and move to new location * Add tests to checking mechanisms * Add luacheck to tests * Some pushing around of files * more (last) fixes and file juggling * Minor tweaks and forgotten checkin * Add NTest selftest to travis * Trying how to master travis * another try * restrict NTest selftest to linux
2020-11-08 15:31:11 +01:00
N.testasync('SINGLE alarm', function(next)
local t = tmr.create();
local count = 0
t:alarm(200, tmr.ALARM_SINGLE,
function()
count = count + 1
ok(count <= 1, "only 1 invocation")
next()
end)
ok(true, "sync end")
end)
N.testasync('SEMI alarm', function(next)
local t = tmr.create();
local count = 0
t:alarm(200, tmr.ALARM_SEMI,
function(tp)
count = count + 1
if count <= 1 then
tp:start()
return
end
ok(eq(count, 2), "only 2 invocations")
next()
end)
ok(true, "sync end")
end)
N.testasync('AUTO alarm', function(next)
local t = tmr.create();
local count = 0
t:alarm(200, tmr.ALARM_AUTO,
function(tp)
count = count + 1
if count == 2 then
tp:stop()
return next()
end
ok(count < 2, "only 2 invocations")
end)
ok(true, "sync end")
end)
N.testco('SINGLE alarm coroutine', function(getCB, waitCB)
local t = tmr.create();
t:alarm(200, tmr.ALARM_SINGLE, getCB("timer"))
local name, timer = waitCB()
ok(eq("timer", name), "CB name matches")
ok(eq(t, timer), "CB tmr instance matches")
ok(true, "coroutine end")
end)
N.testco('SEMI alarm coroutine', function(getCB, waitCB)
local t = tmr.create();
t:alarm(200, tmr.ALARM_SEMI, getCB("timer"))
local name, timer = waitCB()
ok(eq("timer", name), "CB name matches")
ok(eq(t, timer), "CB tmr instance matches")
timer:start()
name, timer = waitCB()
ok(eq("timer", name), "CB name matches again")
ok(eq(t, timer), "CB tmr instance matches again")
ok(true, "coroutine end")
end)
N.testco('AUTO alarm coroutine', function(getCB, waitCB)
local t = tmr.create();
t:alarm(200, tmr.ALARM_AUTO, getCB("timer"))
local name, timer = waitCB()
ok(eq("timer", name), "CB name matches")
ok(eq(t, timer), "CB tmr instance matches")
name, timer = waitCB()
ok(eq("timer", name), "CB name matches again")
ok(eq(t, timer), "CB tmr instance matches again")
timer:stop()
ok(true, "coroutine end")
end)
N.test('softwd set positive and negative values', function()
tmr.softwd(22)
tmr.softwd(0)
tmr.softwd(-1) -- disable it again
tmr.softwd(-22) -- disable it again
end)