mirror of
https://git.minetest.land/MineClone2/MineClone2.git
synced 2025-03-13 17:42:29 +01:00
Rework vl_scheduler unit test to use tests/lib/mock.lua
This commit is contained in:
@ -1,64 +1,19 @@
|
||||
local posix = require 'posix'
|
||||
package.path = package.path .. ";../../../tests/lib/?.lua"
|
||||
local mock = require("mock").luanti(_G)
|
||||
mock.current_modname = "vl_scheduler"
|
||||
mock.modpaths["vl_scheduler"] = "./"
|
||||
|
||||
_G.core = {}
|
||||
_G.dump = dump
|
||||
dofile("../../../tests/lib/misc_helpers.lua")
|
||||
local mod_paths = {
|
||||
vl_scheduler = "./",
|
||||
}
|
||||
|
||||
local on_mods_loaded
|
||||
local globalsteps = 0
|
||||
local last_fake_globalstep_dtime
|
||||
local time_offset = 0
|
||||
|
||||
local function fastforward(amount)
|
||||
time_offset = time_offset + amount
|
||||
end
|
||||
|
||||
local function init_core()
|
||||
return {
|
||||
registered_globalsteps = {
|
||||
function(dtime)
|
||||
globalsteps = globalsteps + 1
|
||||
last_fake_globalstep_dtime = dtime
|
||||
end,
|
||||
},
|
||||
get_current_modname = function() return "vl_scheduler" end,
|
||||
get_modpath = function(modname) return mod_paths[modname] end,
|
||||
register_on_mods_loaded = function(func) on_mods_loaded = func end,
|
||||
get_us_time = function()
|
||||
local sec, nsec = posix.clock_gettime(0)
|
||||
return sec * 1e6 + nsec // 1000 + time_offset
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
local function call_globalstep(dtime)
|
||||
local callbacks = _G.core.registered_globalsteps
|
||||
for i =1,#callbacks do
|
||||
callbacks[i](0.0532432)
|
||||
end
|
||||
end
|
||||
local fastforward = mock.fastforward
|
||||
local call_globalstep = mock.call_globalsteps
|
||||
|
||||
describe('vl_scheduler',function()
|
||||
-- Setup a mock environment like Luanti has
|
||||
local core_mock = init_core()
|
||||
_G.minetest = core_mock
|
||||
_G.core = core_mock
|
||||
_G.bit = require('bitop.funcs')
|
||||
_G.loadstring = loadstring or load
|
||||
_G.unpack = table.unpack
|
||||
_G.dump = dump
|
||||
_G.math.round = function(x) return math.floor(x + 0.5) end
|
||||
|
||||
it('loads',function()
|
||||
local vl_scheduler = dofile("./init.lua")
|
||||
end)
|
||||
it('intercepts the globalstep handlers',function()
|
||||
on_mods_loaded()
|
||||
mock.on_mods_loaded()
|
||||
call_globalstep(0.0532432)
|
||||
assert.is_same(last_fake_globalstep_dtime, 0.0532432)
|
||||
assert.is_same(mock.last_fake_globalstep_dtime, 0.0532432)
|
||||
end)
|
||||
it('schedules tasks',function()
|
||||
local called = false
|
||||
@ -74,14 +29,14 @@ describe('vl_scheduler',function()
|
||||
|
||||
for i=40,50 do
|
||||
after((i-1)*0.05, function(expected_timestep)
|
||||
if expected_timestep ~= globalsteps then
|
||||
if expected_timestep ~= mock.globalsteps then
|
||||
failed = true
|
||||
print("expected="..expected_timestep..",actual="..globalsteps)
|
||||
print("expected="..expected_timestep..",actual="..mock.globalsteps)
|
||||
end
|
||||
end, i)
|
||||
end
|
||||
--_G.vl_scheduler.print_debug_dump()
|
||||
globalsteps = 0
|
||||
mock.globalsteps = 0
|
||||
for i=1,3005 do
|
||||
call_globalstep(0.05)
|
||||
end
|
||||
@ -93,13 +48,13 @@ describe('vl_scheduler',function()
|
||||
|
||||
for i = 5,3000,1 do
|
||||
after((i-1)*0.05, function(expected_timestep)
|
||||
if expected_timestep ~= globalsteps then
|
||||
if expected_timestep ~= mock.globalsteps then
|
||||
failed = true
|
||||
print("expected="..expected_timestep..",actual="..globalsteps)
|
||||
print("expected="..expected_timestep..",actual="..mock.globalsteps)
|
||||
end
|
||||
end, i)
|
||||
end
|
||||
globalsteps = 0
|
||||
mock.globalsteps = 0
|
||||
for i=1,3005 do
|
||||
call_globalstep(0.05)
|
||||
end
|
||||
|
@ -7,38 +7,50 @@ print("package.path="..package.path)
|
||||
|
||||
function mock.luanti(g)
|
||||
local mock
|
||||
local luanti_core
|
||||
mock = {
|
||||
on_mods_loaded = {},
|
||||
registered_on_mods_loaded = {},
|
||||
globalsteps = 0,
|
||||
last_fake_globalstep_dtime = 0,
|
||||
time_offset = 0,
|
||||
current_modname = nil,
|
||||
modpaths = {},
|
||||
settings = {},
|
||||
log = {},
|
||||
registered_globalsteps = {
|
||||
function(dtime)
|
||||
mock.globalsteps = mock.globalsteps + 1
|
||||
mock.last_fake_globaltime_dtime = dtime
|
||||
mock.last_fake_globalstep_dtime = dtime
|
||||
end,
|
||||
},
|
||||
fastforward = function(dtime)
|
||||
mock.time_offset = mock.time_offset + dtime
|
||||
end,
|
||||
on_mods_loaded = function()
|
||||
local callbacks = mock.registered_on_mods_loaded
|
||||
for i = 1,#callbacks do
|
||||
callbacks[i]()
|
||||
end
|
||||
end,
|
||||
call_globalsteps = function(dtime)
|
||||
local callbacks = mock.registered_globalsteps
|
||||
local callbacks = luanti_core.registered_globalsteps
|
||||
for i = 1,#callbacks do
|
||||
callbacks[i](dtime)
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
||||
function mock:fastforward(amount)
|
||||
self.time_offset = self.time_offset + amount
|
||||
function mock.fastforward(amount)
|
||||
mock.time_offset = mock.time_offset + amount
|
||||
end
|
||||
|
||||
local luanti_core = {
|
||||
luanti_core = {
|
||||
registered_globalsteps = mock.registered_globalsteps,
|
||||
registered_nodes = {},
|
||||
log = function(class, msg)
|
||||
table.insert(mock.log, {class,msg})
|
||||
print("["..class.."] "..msg)
|
||||
end,
|
||||
settings = {
|
||||
get_bool = function(key)
|
||||
return mock.settings[key] == "true"
|
||||
@ -60,7 +72,7 @@ function mock.luanti(g)
|
||||
return mock.modpaths[modname]
|
||||
end,
|
||||
register_on_mods_loaded = function(func)
|
||||
table.insert(mock.on_mods_loaded, func)
|
||||
table.insert(mock.registered_on_mods_loaded, func)
|
||||
end,
|
||||
register_globalstep = function(callback)
|
||||
table.insert(mock.registered_globalsteps, callback)
|
||||
|
Reference in New Issue
Block a user