Update init, add tests

This commit is contained in:
Lars Mueller 2020-09-22 19:18:47 +02:00
parent e1c44219d3
commit 934ab6b128
2 changed files with 20 additions and 7 deletions

@ -34,13 +34,17 @@ if _VERSION then
end
end
-- get modpath wrapper
-- TODO get rid of code duplication
local function get_resource(modname, resource)
if not resource then
resource = modname
modname = minetest.get_current_modname()
end
return minetest.get_modpath(modname) .. "/" .. resource
end
local function loadfile_exports(filename)
local env = setmetatable({}, {__index = _G, __call = _G})
local env = setmetatable({}, {__index = _G})
local file = assert(loadfile(filename))
setfenv(file, env)
file()
@ -60,13 +64,14 @@ local components = {
player = {},
table = {},
text = {string = "local"},
threading = {}
threading = {},
vector = {}
}
modlib = {}
for component, additional in pairs(components) do
local comp = loadfile_exports(get_resource("modlib", component .. ".lua"))
local comp = loadfile_exports(get_resource(component .. ".lua"))
modlib[component] = comp
for alias, scope in pairs(additional) do
if scope == "global" then
@ -81,7 +86,8 @@ modlib.conf.build_setting_tree()
modlib.mod.loadfile_exports = loadfile_exports
-- complete the string library (=metatable) with text helpers
modlib.table.complete(string, modlib.text)
_ml = modlib
_ml = modlib
--[[
--modlib.mod.include("test.lua")
]]

7
test.lua Normal file

@ -0,0 +1,7 @@
local t = {}
t[t] = t
local t2 = modlib.table.deepcopy(t)
assert(t2[t2] == t2)
assert(modlib.table.equals_noncircular({[{}]={}}, {[{}]={}}))