Minetest-WorldEditAdditions/worldeditadditions/lib/normalize_test.lua

80 lines
2.8 KiB
Lua
Raw Normal View History

2024-10-13 20:53:24 +00:00
-- ████████ ███████ ███████ ████████
-- ██ ██ ██ ██
-- ██ █████ ███████ ██
-- ██ ██ ██ ██
-- ██ ███████ ███████ ██
-- ██ ██ █████ ███ ██ ██████ ██ ███████ ██████
-- ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██
-- ███████ ███████ ██ ██ ██ ██ ██ ██ █████ ██████
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ██ ██ ██ ██ ████ ██████ ███████ ███████ ██ ██
-- Metatable for tests
local metatable = {
__call = function(self,name, ...)
2024-10-14 17:32:11 +00:00
return xpcall(self.func, debug.traceback, name, ...)
2024-10-13 20:53:24 +00:00
end,
help = function(self)
return "Params: " .. self.params .. "\n" .. self.description
end
}
metatable.__index = metatable
2024-10-16 00:32:14 +00:00
local registered_tests = {}
2024-10-17 00:04:37 +00:00
--- Register a test
-- @param string test_name The name of the test
-- @param table def The definition of the test
-- @return nil No return. Adds the test to local registry
-- accessable via get_registered_tests method
2024-10-16 00:32:14 +00:00
local register_test = function(test_name, def)
2024-10-13 20:53:24 +00:00
---
-- 1: Validation
---
if type(test_name) ~= "string" then
error("The test name is not a string.")
end
if type(def) ~= "table" then
error("The test definition is not a table.")
end
if type(def.description) ~= "string" then
error("The test description is not a string.")
end
if type(def.params) ~= "string" or #def.params == 0 then
error("The test params param is not valid.")
end
if type(def.func) ~= "function" then
error("The test function is not a function.")
end
---
-- 2: Normalisation
---
setmetatable(def, metatable)
2024-10-16 00:32:14 +00:00
registered_tests[test_name] = def
end
local normalize_test = {}
normalize_test.__index = normalize_test
2024-10-17 00:04:37 +00:00
--- Register a test
-- @param string test_name The name of the test
-- @param table def The definition of the test
-- @return nil No return. Adds the test to local registry
-- accessable via get_registered_tests method
normalize_test.__call = function(_self, test_name, def)
2024-10-16 00:32:14 +00:00
register_test(test_name, def)
end
2024-10-17 00:04:37 +00:00
--- Get all registered tests
-- @return table All registered tests
2024-10-16 00:32:14 +00:00
normalize_test.get_registered_tests = function()
local ret = {}
for k, v in pairs(registered_tests) do
ret[k] = v
end
return ret
2024-10-13 20:53:24 +00:00
end
2024-10-16 00:32:14 +00:00
return setmetatable({}, normalize_test)