1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/utils/test/builder.lua

155 lines
3.6 KiB
Lua
Raw Normal View History

2020-09-20 20:13:07 +02:00
local ModuleStore = require 'utils.test.module_store'
2020-09-28 21:40:47 +02:00
local Context = require 'utils.test.context'
2020-09-20 20:13:07 +02:00
local Public = {}
local is_init = false
2020-09-28 21:40:47 +02:00
local id_count = 0
local function get_id()
id_count = id_count + 1
return id_count
end
2020-09-20 20:13:07 +02:00
local function init_inner(module, depth)
2020-09-28 21:40:47 +02:00
module.id = get_id()
2020-09-20 20:13:07 +02:00
module.depth = depth
local count = 0
local tests = {}
for name, func in pairs(module.test_funcs) do
count = count + 1
tests[#tests + 1] = {
2020-09-28 21:40:47 +02:00
id = get_id(),
2020-09-20 20:13:07 +02:00
name = name,
2020-09-25 21:33:46 +02:00
module = module,
2020-09-20 20:13:07 +02:00
func = func,
2020-09-28 21:40:47 +02:00
context = nil,
2020-09-20 20:13:07 +02:00
current_step = nil,
passed = nil,
error = nil
}
end
module.tests = tests
for _, child in pairs(module.children) do
count = count + init_inner(child, depth + 1)
end
module.count = count
return count
end
function Public.init()
if is_init then
return
end
is_init = true
init_inner(ModuleStore.root_module, 0)
end
function Public.get_root_modules()
Public.init()
return ModuleStore.root_module
end
2020-09-28 21:40:47 +02:00
local function prepare_pre_module_hooks(module, runnables, player)
2020-09-25 21:33:46 +02:00
local startup_func = module.startup_func
if startup_func then
runnables[#runnables + 1] = {
is_hook = true,
name = 'startup',
module = module,
func = startup_func,
2020-09-28 21:40:47 +02:00
context = Context.new(player),
2020-09-25 21:33:46 +02:00
current_step = 0,
error = nil
}
end
end
2020-09-28 21:40:47 +02:00
local function build_pre_module_hooks(module, runnables, player)
2020-09-25 21:33:46 +02:00
if module == nil then
return
end
2020-09-28 21:40:47 +02:00
build_pre_module_hooks(module.parent, runnables, player)
prepare_pre_module_hooks(module, runnables, player)
2020-09-25 21:33:46 +02:00
end
2020-09-28 21:40:47 +02:00
local function prepare_post_module_hooks(module, runnables, player)
2020-09-25 21:33:46 +02:00
local teardown_func = module.teardown_func
if teardown_func then
runnables[#runnables + 1] = {
is_hook = true,
name = 'teardown',
module = module,
func = teardown_func,
2020-09-28 21:40:47 +02:00
context = Context.new(player),
2020-09-25 21:33:46 +02:00
current_step = 0,
error = nil
}
end
end
2020-09-28 21:40:47 +02:00
local function build_post_module_hooks(module, runnables, player)
2020-09-25 21:33:46 +02:00
if module == nil then
return
end
2020-09-28 21:40:47 +02:00
prepare_post_module_hooks(module, runnables, player)
build_post_module_hooks(module.parent, runnables, player)
2020-09-25 21:33:46 +02:00
end
2020-09-28 21:40:47 +02:00
local function prepare_test(test, player)
test.context = Context.new(player)
2020-09-20 20:13:07 +02:00
test.current_step = 0
test.passed = nil
test.error = nil
2020-09-25 21:33:46 +02:00
return test
2020-09-20 20:13:07 +02:00
end
2020-09-28 21:40:47 +02:00
local function prepare_module(module, runnables, player)
2020-09-20 20:13:07 +02:00
module.passed = nil
2020-09-28 21:40:47 +02:00
prepare_pre_module_hooks(module, runnables, player)
2020-09-20 20:13:07 +02:00
for _, test in pairs(module.tests) do
2020-09-28 21:40:47 +02:00
prepare_test(test, player)
2020-09-25 21:33:46 +02:00
runnables[#runnables + 1] = test
2020-09-20 20:13:07 +02:00
end
for _, child in pairs(module.children) do
2020-09-28 21:40:47 +02:00
prepare_module(child, runnables, player)
2020-09-20 20:13:07 +02:00
end
2020-09-25 21:33:46 +02:00
2020-09-28 21:40:47 +02:00
prepare_post_module_hooks(module, runnables, player)
2020-09-20 20:13:07 +02:00
end
2020-09-28 21:40:47 +02:00
function Public.build_test_for_run(test, player)
2020-09-20 20:13:07 +02:00
Public.init()
2020-09-25 21:33:46 +02:00
local runnables = {}
2020-09-28 21:40:47 +02:00
build_pre_module_hooks(test.module, runnables, player)
runnables[#runnables + 1] = prepare_test(test, player)
build_post_module_hooks(test.module, runnables, player)
2020-09-25 21:33:46 +02:00
return runnables
2020-09-20 20:13:07 +02:00
end
2020-09-28 21:40:47 +02:00
function Public.build_module_for_run(module, player)
2020-09-20 20:13:07 +02:00
Public.init()
2020-09-25 21:33:46 +02:00
local runnables = {}
2020-09-28 21:40:47 +02:00
build_pre_module_hooks(module.parent, runnables, player)
prepare_module(module, runnables, player)
build_post_module_hooks(module.parent, runnables, player)
2020-09-25 21:33:46 +02:00
return runnables
2020-09-20 20:13:07 +02:00
end
return Public