2017-06-23 17:04:11 +00:00
|
|
|
local logger = require("logger")
|
|
|
|
|
2017-06-14 17:32:16 +00:00
|
|
|
local MockTime = {
|
|
|
|
original_os_time = os.time,
|
|
|
|
original_util_time = nil,
|
|
|
|
value = os.time(),
|
|
|
|
}
|
|
|
|
|
|
|
|
function MockTime:install()
|
|
|
|
assert(self ~= nil)
|
|
|
|
local util = require("ffi/util")
|
|
|
|
if self.original_util_time == nil then
|
|
|
|
self.original_util_time = util.gettime
|
|
|
|
assert(self.original_util_time ~= nil)
|
|
|
|
end
|
2017-08-08 20:35:40 +00:00
|
|
|
os.time = function() --luacheck: ignore
|
2017-06-23 17:04:11 +00:00
|
|
|
logger.dbg("MockTime:os.time: ", self.value)
|
|
|
|
return self.value
|
|
|
|
end
|
|
|
|
util.gettime = function()
|
|
|
|
logger.dbg("MockTime:util.gettime: ", self.value)
|
|
|
|
return self.value, 0
|
|
|
|
end
|
2017-06-14 17:32:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function MockTime:uninstall()
|
|
|
|
assert(self ~= nil)
|
|
|
|
local util = require("ffi/util")
|
2017-08-08 20:35:40 +00:00
|
|
|
os.time = self.original_os_time --luacheck: ignore
|
2017-06-14 17:32:16 +00:00
|
|
|
if self.original_util_time ~= nil then
|
|
|
|
util.gettime = self.original_util_time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function MockTime:set(value)
|
|
|
|
assert(self ~= nil)
|
|
|
|
if type(value) ~= "number" then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
self.value = math.floor(value)
|
2017-06-23 17:04:11 +00:00
|
|
|
logger.dbg("MockTime:set ", self.value)
|
2017-06-14 17:32:16 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function MockTime:increase(value)
|
|
|
|
assert(self ~= nil)
|
|
|
|
if type(value) ~= "number" then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
self.value = math.floor(self.value + value)
|
2017-06-23 17:04:11 +00:00
|
|
|
logger.dbg("MockTime:increase ", self.value)
|
2017-06-14 17:32:16 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return MockTime
|