mirror of
https://github.com/Refactorio/RedMew.git
synced 2024-12-04 09:42:30 +02:00
Add module names and better error handling.
This commit is contained in:
parent
980ca7672b
commit
bb96ccf15f
@ -170,7 +170,7 @@ if _DUMP_ENV then
|
||||
end
|
||||
|
||||
if _DEBUG then
|
||||
require 'utils.test.main'
|
||||
require('utils.test.main')
|
||||
end
|
||||
|
||||
-- Needs to be at bottom so tokens are registered last.
|
||||
|
@ -4,7 +4,7 @@ local Gui = require 'utils.gui'
|
||||
local Assert = require 'utils.test.assert'
|
||||
|
||||
Declare.module(
|
||||
'Gui',
|
||||
{'utils', 'Gui'},
|
||||
function()
|
||||
Declare.module(
|
||||
'can toggle top buttons',
|
||||
|
@ -1,12 +1,5 @@
|
||||
local require = require
|
||||
local pcall = pcall
|
||||
local include = require 'utils.test.include'
|
||||
|
||||
function find_all_tests()
|
||||
local loaded = _G.package.loaded
|
||||
|
||||
for name in pairs(loaded) do
|
||||
pcall(require, name .. '_tests')
|
||||
end
|
||||
for name in pairs(_G.package.loaded) do
|
||||
include(name .. '_tests')
|
||||
end
|
||||
|
||||
find_all_tests()
|
||||
|
9
utils/test/include.lua
Normal file
9
utils/test/include.lua
Normal file
@ -0,0 +1,9 @@
|
||||
local require = require
|
||||
local pcall = pcall
|
||||
|
||||
return function(name)
|
||||
local s, e = pcall(require, name)
|
||||
if not s and not string.find(e, 'no such file') then
|
||||
error(e, 2)
|
||||
end
|
||||
end
|
@ -1,4 +1,5 @@
|
||||
require 'utils.test.runner'
|
||||
require 'utils.test.viewer'
|
||||
require 'utils.test.command'
|
||||
require 'utils.test.discovery'
|
||||
local include = require 'utils.test.include'
|
||||
include 'utils.test.runner'
|
||||
include 'utils.test.viewer'
|
||||
include 'utils.test.command'
|
||||
include 'utils.test.discovery'
|
||||
|
@ -16,7 +16,7 @@ local function new_module(module_name)
|
||||
teardown_error = nil,
|
||||
test_funcs = {},
|
||||
tests = nil,
|
||||
is_open = true,
|
||||
is_open = false,
|
||||
depth = nil,
|
||||
count = nil,
|
||||
passed = nil
|
||||
@ -24,6 +24,7 @@ local function new_module(module_name)
|
||||
end
|
||||
|
||||
local root_module = new_module(nil)
|
||||
root_module.is_open = true
|
||||
Public.root_module = root_module
|
||||
|
||||
local parent_module = nil
|
||||
@ -42,34 +43,56 @@ local function add_module(module_name, module_func, parent)
|
||||
module_func()
|
||||
end
|
||||
|
||||
local function no_op()
|
||||
end
|
||||
|
||||
local function add_module_range(modules_names, module_func, parent)
|
||||
for i = 1, #modules_names - 1 do
|
||||
local name = modules_names[i]
|
||||
add_module(name, no_op, parent)
|
||||
parent = parent_module
|
||||
end
|
||||
|
||||
add_module(modules_names[#modules_names], module_func, parent)
|
||||
end
|
||||
|
||||
function Public.module(module_name, module_func)
|
||||
if type(module_name) ~= 'string' then
|
||||
error('module_name must be of type string.')
|
||||
local module_name_type = type(module_name)
|
||||
if module_name_type ~= 'string' and module_name_type ~= 'table' then
|
||||
error('module_name must be of type string or array of strings.', 2)
|
||||
end
|
||||
|
||||
if module_name_type == 'table' and #module_name == 0 then
|
||||
error('when module_name is array must be non empty.', 2)
|
||||
end
|
||||
|
||||
if type(module_func) ~= 'function' then
|
||||
error('module_func must be of type function.')
|
||||
error('module_func must be of type function.', 2)
|
||||
end
|
||||
|
||||
local old_parent = parent_module
|
||||
local parent = parent_module or root_module
|
||||
|
||||
add_module(module_name, module_func, parent)
|
||||
if module_name_type == 'string' then
|
||||
add_module(module_name, module_func, parent)
|
||||
else
|
||||
add_module_range(module_name, module_func, parent)
|
||||
end
|
||||
|
||||
parent_module = old_parent
|
||||
end
|
||||
|
||||
function Public.test(test_name, test_func)
|
||||
if not parent_module then
|
||||
error('test can not be declared outisde of a module.')
|
||||
error('test can not be declared outisde of a module.', 2)
|
||||
end
|
||||
|
||||
if type(test_name) ~= 'string' then
|
||||
error('test_name must be of type string.')
|
||||
error('test_name must be of type string.', 2)
|
||||
end
|
||||
|
||||
if type(test_func) ~= 'function' then
|
||||
error('test_func must be of type function.')
|
||||
error('test_func must be of type function.', 2)
|
||||
end
|
||||
|
||||
local test_funcs = parent_module.test_funcs
|
||||
@ -79,7 +102,8 @@ function Public.test(test_name, test_func)
|
||||
"test '",
|
||||
test_name,
|
||||
"' already exists, can not have duplicate test names in the same module."
|
||||
}
|
||||
},
|
||||
2
|
||||
)
|
||||
end
|
||||
|
||||
@ -88,15 +112,15 @@ end
|
||||
|
||||
function Public.module_startup(startup_func)
|
||||
if type(startup_func) ~= 'function' then
|
||||
error('startup_func must be of type function.')
|
||||
error('startup_func must be of type function.', 2)
|
||||
end
|
||||
|
||||
if parent_module == nil then
|
||||
error('root module can not have startup_func.')
|
||||
error('root module can not have startup_func.', 2)
|
||||
end
|
||||
|
||||
if parent_module.startup_func ~= nil then
|
||||
error('startup_func can not be declared twice for the same module.')
|
||||
error('startup_func can not be declared twice for the same module.', 2)
|
||||
end
|
||||
|
||||
parent_module.startup_func = startup_func
|
||||
@ -104,15 +128,15 @@ end
|
||||
|
||||
function Public.module_teardown(teardown_func)
|
||||
if type(teardown_func) ~= 'function' then
|
||||
error('teardown_func must be of type function.')
|
||||
error('teardown_func must be of type function.', 2)
|
||||
end
|
||||
|
||||
if parent_module == nil then
|
||||
error('root module can not have teardown_func.')
|
||||
error('root module can not have teardown_func.', 2)
|
||||
end
|
||||
|
||||
if parent_module.teardown_func ~= nil then
|
||||
error('teardown_func can not be declared twice for the same module.')
|
||||
error('teardown_func can not be declared twice for the same module.', 2)
|
||||
end
|
||||
|
||||
parent_module.teardown_func = teardown_func
|
||||
|
Loading…
Reference in New Issue
Block a user