Add tests for hooks

This commit is contained in:
Ukendio 2024-10-12 22:01:08 +02:00
parent 76ab6838f2
commit d4be55ead0

View file

@ -1365,6 +1365,32 @@ TEST("changetracker:track()", function()
end) end)
local function create_cache(hook)
local columns = setmetatable({}, {
__index = function(self, component)
local column = {}
self[component] = column
return column
end
})
return function(world, component, fn)
local column = columns[component]
table.insert(column, fn)
world:set(component, hook, function(entity, value)
for _, callback in column do
callback(entity, value)
end
end)
end
end
local hooks = {
OnSet = create_cache(jecs.OnSet),
OnAdd = create_cache(jecs.OnAdd),
OnRemove = create_cache(jecs.OnRemove)
}
TEST("Hooks", function() TEST("Hooks", function()
do CASE "OnAdd" do CASE "OnAdd"
local world = jecs.World.new() local world = jecs.World.new()
@ -1380,8 +1406,15 @@ TEST("Hooks", function()
local world = jecs.World.new() local world = jecs.World.new()
local Number = world:component() local Number = world:component()
local e1 = world:entity() local e1 = world:entity()
world:set(Number, jecs.OnSet, function(entity, data)
CHECK(e1 == entity) hooks.OnSet(world, Number, function(entity, data)
CHECK(e1 == entity)
CHECK(data == world:get(entity, Number))
CHECK(data == 1)
end)
hooks.OnSet(world, Number, function(entity, data)
CHECK(e1 == entity)
CHECK(data == world:get(entity, Number))
CHECK(data == 1) CHECK(data == 1)
end) end)
world:set(e1, Number, 1) world:set(e1, Number, 1)