mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 17:10:03 +00:00
Add tests for addons
This commit is contained in:
parent
9c68218d5d
commit
2b90fabec5
8 changed files with 110 additions and 105 deletions
1
.luaurc
1
.luaurc
|
@ -4,6 +4,7 @@
|
||||||
"testkit": "tools/testkit",
|
"testkit": "tools/testkit",
|
||||||
"mirror": "mirror",
|
"mirror": "mirror",
|
||||||
"tools": "tools",
|
"tools": "tools",
|
||||||
|
"addons": "addons",
|
||||||
},
|
},
|
||||||
"languageMode": "strict"
|
"languageMode": "strict"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,40 @@
|
||||||
local jecs = require("@jecs")
|
local jecs = require("@jecs")
|
||||||
local ECS_GENERATION = jecs.ECS_GENERATION
|
local ECS_GENERATION = jecs.ECS_GENERATION
|
||||||
local ECS_ID = jecs.ECS_ID
|
local ECS_ID = jecs.ECS_ID
|
||||||
local ansi = require("@tools/ansi")
|
|
||||||
|
local ansi = {
|
||||||
|
white_underline = function(s: any)
|
||||||
|
return `\27[1;4m{s}\27[0m`
|
||||||
|
end,
|
||||||
|
|
||||||
|
white = function(s: any)
|
||||||
|
return `\27[37;1m{s}\27[0m`
|
||||||
|
end,
|
||||||
|
|
||||||
|
green = function(s: any)
|
||||||
|
return `\27[32;1m{s}\27[0m`
|
||||||
|
end,
|
||||||
|
|
||||||
|
red = function(s: any)
|
||||||
|
return `\27[31;1m{s}\27[0m`
|
||||||
|
end,
|
||||||
|
|
||||||
|
yellow = function(s: any)
|
||||||
|
return `\27[33;1m{s}\27[0m`
|
||||||
|
end,
|
||||||
|
|
||||||
|
red_highlight = function(s: any)
|
||||||
|
return `\27[41;1;30m{s}\27[0m`
|
||||||
|
end,
|
||||||
|
|
||||||
|
green_highlight = function(s: any)
|
||||||
|
return `\27[42;1;30m{s}\27[0m`
|
||||||
|
end,
|
||||||
|
|
||||||
|
gray = function(s: any)
|
||||||
|
return `\27[30;1m{s}\27[0m`
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
local function pe(e: any)
|
local function pe(e: any)
|
||||||
local gen = ECS_GENERATION(e)
|
local gen = ECS_GENERATION(e)
|
|
@ -4,10 +4,9 @@ local ECS_ID = jecs.ECS_ID
|
||||||
local __ = jecs.Wildcard
|
local __ = jecs.Wildcard
|
||||||
local pair = jecs.pair
|
local pair = jecs.pair
|
||||||
|
|
||||||
local prettify = require("@tools/entity_visualiser").prettify
|
local prettify = require("@addons/entity_visualiser").prettify
|
||||||
|
|
||||||
local pe = prettify
|
local pe = prettify
|
||||||
local ansi = require("@tools/ansi")
|
|
||||||
|
|
||||||
function print_centered_entity(entity, width: number)
|
function print_centered_entity(entity, width: number)
|
||||||
local entity_str = tostring(entity)
|
local entity_str = tostring(entity)
|
||||||
|
@ -40,12 +39,10 @@ local function lifetime_tracker_add(world: jecs.World, opt)
|
||||||
|
|
||||||
local ENTITY_RANGE = (jecs.Rest :: any) + 1
|
local ENTITY_RANGE = (jecs.Rest :: any) + 1
|
||||||
|
|
||||||
local w = setmetatable({}, { __index = world })
|
|
||||||
|
|
||||||
padding_enabled = opt.padding_enabled
|
padding_enabled = opt.padding_enabled
|
||||||
|
|
||||||
local world_entity = world.entity
|
local world_entity = world.entity
|
||||||
w.entity = function(self, entity)
|
world.entity = function(_, entity)
|
||||||
if entity then
|
if entity then
|
||||||
return world_entity(world, entity)
|
return world_entity(world, entity)
|
||||||
end
|
end
|
||||||
|
@ -59,7 +56,7 @@ local function lifetime_tracker_add(world: jecs.World, opt)
|
||||||
pad()
|
pad()
|
||||||
return e
|
return e
|
||||||
end
|
end
|
||||||
w.print_entity_index = function(self)
|
world.print_entity_index = function()
|
||||||
local max_id = entity_index.max_id
|
local max_id = entity_index.max_id
|
||||||
local alive_count = entity_index.alive_count
|
local alive_count = entity_index.alive_count
|
||||||
local alive = table.move(dense_array, 1 + jecs.Rest :: any, alive_count, 1, {})
|
local alive = table.move(dense_array, 1 + jecs.Rest :: any, alive_count, 1, {})
|
||||||
|
@ -86,7 +83,7 @@ local function lifetime_tracker_add(world: jecs.World, opt)
|
||||||
pad()
|
pad()
|
||||||
end
|
end
|
||||||
local timelines = {}
|
local timelines = {}
|
||||||
w.print_snapshot = function(self)
|
world.print_snapshot = function(_)
|
||||||
local timeline = #timelines + 1
|
local timeline = #timelines + 1
|
||||||
local entity_column_width = 10
|
local entity_column_width = 10
|
||||||
local status_column_width = 8
|
local status_column_width = 8
|
||||||
|
@ -161,7 +158,7 @@ local function lifetime_tracker_add(world: jecs.World, opt)
|
||||||
end
|
end
|
||||||
local world_add = world.add
|
local world_add = world.add
|
||||||
local relations = {}
|
local relations = {}
|
||||||
w.add = function(self, entity: any, component: any)
|
world.add = function(_, entity: any, component: any)
|
||||||
world_add(world, entity, component)
|
world_add(world, entity, component)
|
||||||
if jecs.IS_PAIR(component) then
|
if jecs.IS_PAIR(component) then
|
||||||
local relation = jecs.pair_first(world, component)
|
local relation = jecs.pair_first(world, component)
|
||||||
|
@ -172,7 +169,7 @@ local function lifetime_tracker_add(world: jecs.World, opt)
|
||||||
end
|
end
|
||||||
|
|
||||||
local world_delete = world.delete
|
local world_delete = world.delete
|
||||||
w.delete = function(self, e)
|
world.delete = function(world, e)
|
||||||
world_delete(world, e)
|
world_delete(world, e)
|
||||||
|
|
||||||
local idr_t = component_index[pair(__, e)]
|
local idr_t = component_index[pair(__, e)]
|
||||||
|
@ -210,7 +207,7 @@ local function lifetime_tracker_add(world: jecs.World, opt)
|
||||||
print(`*deleted {pe(e)}`)
|
print(`*deleted {pe(e)}`)
|
||||||
pad()
|
pad()
|
||||||
end
|
end
|
||||||
return w
|
return world
|
||||||
end
|
end
|
||||||
|
|
||||||
return lifetime_tracker_add
|
return lifetime_tracker_add
|
|
@ -1,11 +1,9 @@
|
||||||
local jecs = require("@jecs")
|
local jecs = require("@jecs")
|
||||||
local testkit = require("@testkit")
|
local testkit = require("@testkit")
|
||||||
|
|
||||||
local function observers_new(description)
|
local function observers_new(world, description)
|
||||||
local event = description.event
|
|
||||||
local query = description.query
|
local query = description.query
|
||||||
local callback = description.callback
|
local callback = description.callback
|
||||||
local world = query.world
|
|
||||||
local terms = query.filter_with
|
local terms = query.filter_with
|
||||||
if not terms then
|
if not terms then
|
||||||
local ids = query.ids
|
local ids = query.ids
|
||||||
|
@ -26,11 +24,8 @@ local function observers_new(description)
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, term in terms do
|
for _, term in terms do
|
||||||
if event == jecs.OnAdd then
|
world:added(term, emplaced)
|
||||||
world:added(term, emplaced)
|
world:changed(term, emplaced)
|
||||||
elseif event == jecs.OnSet then
|
|
||||||
world:emplaced(term, emplaced)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -67,12 +62,11 @@ local function world_track(world, ...)
|
||||||
|
|
||||||
for _, term in terms do
|
for _, term in terms do
|
||||||
world:added(term, emplaced)
|
world:added(term, emplaced)
|
||||||
world:emplaced(term, emplaced)
|
world:changed(term, emplaced)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function iter()
|
local function iter()
|
||||||
local i = n
|
local i = n
|
||||||
print(i, "i")
|
|
||||||
return function()
|
return function()
|
||||||
local row = i
|
local row = i
|
||||||
if row == 0 then
|
if row == 0 then
|
||||||
|
@ -84,12 +78,16 @@ local function world_track(world, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
local it = {
|
local it = {
|
||||||
__iter = iter
|
__iter = iter,
|
||||||
|
without = function(self, ...)
|
||||||
|
q_shim.filter_without = { ... }
|
||||||
|
return self
|
||||||
|
end
|
||||||
}
|
}
|
||||||
return setmetatable(it, it)
|
return setmetatable(it, it)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function observers_init(world)
|
local function observers_add(world)
|
||||||
local signals = {
|
local signals = {
|
||||||
added = {},
|
added = {},
|
||||||
emplaced = {},
|
emplaced = {},
|
||||||
|
@ -110,13 +108,13 @@ local function observers_init(world)
|
||||||
table.insert(listeners, fn)
|
table.insert(listeners, fn)
|
||||||
end
|
end
|
||||||
|
|
||||||
world.emplaced = function(_, component, fn)
|
world.changed = function(_, component, fn)
|
||||||
local listeners = signals.emplaced[component]
|
local listeners = signals.emplaced[component]
|
||||||
if not listeners then
|
if not listeners then
|
||||||
listeners = {}
|
listeners = {}
|
||||||
signals.emplaced[component] = listeners
|
signals.emplaced[component] = listeners
|
||||||
local idr = jecs.id_record_ensure(world, component)
|
local idr = jecs.id_record_ensure(world, component)
|
||||||
idr.hooks.on_set = function(entity, value)
|
idr.hooks.on_change = function(entity, value)
|
||||||
for _, listener in listeners do
|
for _, listener in listeners do
|
||||||
listener(entity, value)
|
listener(entity, value)
|
||||||
end
|
end
|
||||||
|
@ -143,46 +141,9 @@ local function observers_init(world)
|
||||||
world.signals = signals
|
world.signals = signals
|
||||||
|
|
||||||
world.track = world_track
|
world.track = world_track
|
||||||
return
|
|
||||||
|
world.observer = observers_new
|
||||||
|
return world
|
||||||
end
|
end
|
||||||
|
|
||||||
local world = jecs.world()
|
return observers_add
|
||||||
observers_init(world)
|
|
||||||
|
|
||||||
local A = world:component()
|
|
||||||
local B = world:component()
|
|
||||||
|
|
||||||
world:added(A, print)
|
|
||||||
world:added(A, function(entity)
|
|
||||||
print(entity, 2)
|
|
||||||
end)
|
|
||||||
|
|
||||||
local observer = observers_new({
|
|
||||||
event = jecs.OnAdd,
|
|
||||||
query = world:query(A, B),
|
|
||||||
callback = function(entity)
|
|
||||||
print(entity, 3)
|
|
||||||
end
|
|
||||||
})
|
|
||||||
|
|
||||||
local e = world:entity()
|
|
||||||
local Test = world:track(A, B)
|
|
||||||
for a in Test do
|
|
||||||
print(a)
|
|
||||||
assert(false)
|
|
||||||
end
|
|
||||||
world:add(e, A)
|
|
||||||
|
|
||||||
-- Output:
|
|
||||||
-- 271
|
|
||||||
-- 271, 2
|
|
||||||
for _ in Test do
|
|
||||||
assert(false)
|
|
||||||
end
|
|
||||||
world:add(e, B)
|
|
||||||
for a in Test do
|
|
||||||
print("lol")
|
|
||||||
assert(true)
|
|
||||||
end
|
|
||||||
-- Output:
|
|
||||||
-- 271, 3
|
|
|
@ -1,8 +1,7 @@
|
||||||
local jecs = require("@jecs")
|
local jecs = require("@jecs")
|
||||||
local pair = jecs.pair
|
local pair = jecs.pair
|
||||||
local ChildOf = jecs.ChildOf
|
local ChildOf = jecs.ChildOf
|
||||||
local lifetime_tracker_add = require("@tools/lifetime_tracker")
|
local lifetime_tracker_add = require("@addons/lifetime_tracker")
|
||||||
local pe = require("@tools/entity_visualiser").prettify
|
|
||||||
local world = lifetime_tracker_add(jecs.world(), {padding_enabled=false})
|
local world = lifetime_tracker_add(jecs.world(), {padding_enabled=false})
|
||||||
local FriendsWith = world:component()
|
local FriendsWith = world:component()
|
||||||
world:print_snapshot()
|
world:print_snapshot()
|
46
test/addons/observers.luau
Normal file
46
test/addons/observers.luau
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
local jecs = require("@jecs")
|
||||||
|
local observers_add = require("@addons/observers")
|
||||||
|
|
||||||
|
local world = jecs.world()
|
||||||
|
observers_add(world)
|
||||||
|
|
||||||
|
local A = world:component()
|
||||||
|
local B = world:component()
|
||||||
|
local C = world:component()
|
||||||
|
|
||||||
|
world:observer({
|
||||||
|
query = world:query(),
|
||||||
|
callback = function(entity)
|
||||||
|
buf ..= debug.info(2, "sl")
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
local i = 0
|
||||||
|
world:added(A, function(entity)
|
||||||
|
assert(i == 0)
|
||||||
|
i += 1
|
||||||
|
end)
|
||||||
|
world:added(A, function(entity)
|
||||||
|
assert(i == 1)
|
||||||
|
i += 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
world:removed(A, function(entity)
|
||||||
|
assert(false)
|
||||||
|
end)
|
||||||
|
|
||||||
|
local observer = world:observer({
|
||||||
|
query = world:query(A, B),
|
||||||
|
callback = function(entity)
|
||||||
|
assert(i == 2)
|
||||||
|
i += 1
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
local e = world:entity()
|
||||||
|
world:add(e, A)
|
||||||
|
assert(i == 2)
|
||||||
|
|
||||||
|
world:add(e, B)
|
||||||
|
assert(i == 3)
|
|
@ -133,6 +133,8 @@ TEST("#adding a recycled target", function()
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
local entity_visualizer = require("@addons/entity_visualiser")
|
||||||
|
|
||||||
TEST("#repro2", function()
|
TEST("#repro2", function()
|
||||||
local world = world_new()
|
local world = world_new()
|
||||||
local Lifetime = world:component() :: jecs.Id<number>
|
local Lifetime = world:component() :: jecs.Id<number>
|
||||||
|
@ -144,7 +146,6 @@ TEST("#repro2", function()
|
||||||
world:set(entity, pair(Lifetime, Beam), 2)
|
world:set(entity, pair(Lifetime, Beam), 2)
|
||||||
world:set(entity, pair(4, 5), 6) -- noise
|
world:set(entity, pair(4, 5), 6) -- noise
|
||||||
|
|
||||||
local entity_visualizer = require("@tools/entity_visualiser")
|
|
||||||
-- entity_visualizer.components(world, entity)
|
-- entity_visualizer.components(world, entity)
|
||||||
|
|
||||||
for e in world:each(pair(Lifetime, __)) do
|
for e in world:each(pair(Lifetime, __)) do
|
||||||
|
@ -169,7 +170,7 @@ TEST("#repro2", function()
|
||||||
CHECK(world:get(entity, pair(Lifetime, Beam)) == 1)
|
CHECK(world:get(entity, pair(Lifetime, Beam)) == 1)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local lifetime_tracker_add = require("@tools/lifetime_tracker")
|
local lifetime_tracker_add = require("@addons/lifetime_tracker")
|
||||||
|
|
||||||
TEST("another", function()
|
TEST("another", function()
|
||||||
local world = world_new()
|
local world = world_new()
|
||||||
|
@ -306,8 +307,8 @@ TEST("world:cleanup()", function()
|
||||||
CHECK(#archetype_index["1_2_3"].entities == 1)
|
CHECK(#archetype_index["1_2_3"].entities == 1)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local pe = require("@tools/entity_visualiser").prettify
|
local pe = require("@addons/entity_visualiser").prettify
|
||||||
local lifetime_tracker_add = require("@tools/lifetime_tracker")
|
local lifetime_tracker_add = require("@addons/lifetime_tracker")
|
||||||
|
|
||||||
TEST("world:entity()", function()
|
TEST("world:entity()", function()
|
||||||
do
|
do
|
||||||
|
|
|
@ -1,33 +0,0 @@
|
||||||
return {
|
|
||||||
white_underline = function(s: any)
|
|
||||||
return `\27[1;4m{s}\27[0m`
|
|
||||||
end,
|
|
||||||
|
|
||||||
white = function(s: any)
|
|
||||||
return `\27[37;1m{s}\27[0m`
|
|
||||||
end,
|
|
||||||
|
|
||||||
green = function(s: any)
|
|
||||||
return `\27[32;1m{s}\27[0m`
|
|
||||||
end,
|
|
||||||
|
|
||||||
red = function(s: any)
|
|
||||||
return `\27[31;1m{s}\27[0m`
|
|
||||||
end,
|
|
||||||
|
|
||||||
yellow = function(s: any)
|
|
||||||
return `\27[33;1m{s}\27[0m`
|
|
||||||
end,
|
|
||||||
|
|
||||||
red_highlight = function(s: any)
|
|
||||||
return `\27[41;1;30m{s}\27[0m`
|
|
||||||
end,
|
|
||||||
|
|
||||||
green_highlight = function(s: any)
|
|
||||||
return `\27[42;1;30m{s}\27[0m`
|
|
||||||
end,
|
|
||||||
|
|
||||||
gray = function(s: any)
|
|
||||||
return `\27[30;1m{s}\27[0m`
|
|
||||||
end,
|
|
||||||
}
|
|
Loading…
Reference in a new issue