added support for calltables. fixes #1

This commit is contained in:
Enrique García Cota 2011-05-01 23:46:48 +02:00
parent f93cbfb6d8
commit a0e6c86a7b
2 changed files with 17 additions and 2 deletions

View File

@ -6,9 +6,19 @@
-----------------------------------------------------------------------------------------------------------------------
local function isCallable(callback)
local tc = type(callback)
if tc == 'function' then return true end
if tc == 'table' then
local mt = getmetatable(callback)
return type(mt) == 'table' and type(mt.__call) == 'function'
end
return false
end
local function checkTimeAndCallback(time, callback)
assert(type(time) == "number" and time > 0, "time must be a positive number")
assert(type(callback) == "function", "callback must be a function")
assert(isCallable(callback), "callback must be a function")
end
local entries = {}

View File

@ -7,6 +7,7 @@ context( 'cron', function()
amount = amount or 1
counter = counter + amount
end
local countable = setmetatable({}, {__call = count})
before(function()
counter = 0
@ -44,11 +45,13 @@ context( 'cron', function()
end)
context( 'after', function()
test( 'Should throw error if time is not a positive number, or callback is not function', function()
test( 'Should throw error if time is not a positive number, or callback is not callable', function()
assert_error(function() cron.after('error', count) end)
assert_error(function() cron.after(2, 'error') end)
assert_error(function() cron.after(-2, count) end)
assert_error(function() cron.after(2, {}) end)
assert_not_error(function() cron.after(2, count) end)
assert_not_error(function() cron.after(2, countable) end)
end)
test( 'Should execute timed actions are executed only once, at the right time', function()
@ -76,7 +79,9 @@ context( 'cron', function()
assert_error(function() cron.every('error', count) end)
assert_error(function() cron.every(2, 'error') end)
assert_error(function() cron.every(-2, count) end)
assert_error(function() cron.every(-2, {}) end)
assert_not_error(function() cron.every(2, count) end)
assert_not_error(function() cron.every(2, countable) end)
end)
test( 'Should execute periodical actions periodically', function()