mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 17:10:03 +00:00
Cleanup testing slightly
This commit is contained in:
parent
5c051eb737
commit
ba31aa98ba
6 changed files with 1081 additions and 1307 deletions
|
@ -1,20 +1,32 @@
|
||||||
local jecs = require("@jecs")
|
local jecs = require("@jecs")
|
||||||
local testkit = require("@testkit")
|
|
||||||
|
type Observer<T...> = {
|
||||||
|
callback: (jecs.Entity) -> (),
|
||||||
|
query: jecs.Query<T...>,
|
||||||
|
}
|
||||||
|
|
||||||
|
export type PatchedWorld = jecs.World & {
|
||||||
|
added: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id, value: any) -> ()) -> (),
|
||||||
|
removed: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id) -> ()) -> (),
|
||||||
|
changed: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id) -> ()) -> (),
|
||||||
|
observer: (PatchedWorld, Observer<any>) -> (),
|
||||||
|
monitor: (PatchedWorld, Observer<any>) -> (),
|
||||||
|
}
|
||||||
|
|
||||||
local function observers_new(world, description)
|
local function observers_new(world, description)
|
||||||
local query = description.query
|
local query = description.query
|
||||||
local callback = description.callback
|
local callback = description.callback
|
||||||
local terms = query.filter_with
|
local terms = query.filter_with :: { jecs.Id }
|
||||||
if not terms then
|
if not terms then
|
||||||
local ids = query.ids
|
local ids = query.ids
|
||||||
query.filter_with = ids
|
query.filter_with = ids
|
||||||
terms = ids
|
terms = ids
|
||||||
end
|
end
|
||||||
|
|
||||||
local entity_index = world.entity_index
|
local entity_index = world.entity_index :: any
|
||||||
local function emplaced(entity)
|
local function emplaced(entity: jecs.Entity)
|
||||||
local r = jecs.entity_index_try_get_fast(
|
local r = jecs.entity_index_try_get_fast(
|
||||||
entity_index, entity)
|
entity_index, entity :: any)
|
||||||
|
|
||||||
if not r then
|
if not r then
|
||||||
return
|
return
|
||||||
|
@ -33,18 +45,20 @@ local function observers_new(world, description)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_track(world, ...)
|
local function monitors_new(world, description)
|
||||||
local entity_index = world.entity_index
|
local query = description.query
|
||||||
local terms = { ... }
|
local callback = description.callback
|
||||||
local q_shim = { filter_with = terms }
|
local terms = query.filter_with :: { jecs.Id }
|
||||||
|
if not terms then
|
||||||
|
local ids = query.ids
|
||||||
|
query.filter_with = ids
|
||||||
|
terms = ids
|
||||||
|
end
|
||||||
|
|
||||||
local n = 0
|
local entity_index = world.entity_index :: any
|
||||||
local dense_array = {}
|
local function emplaced(entity: jecs.Entity)
|
||||||
local sparse_array = {}
|
|
||||||
|
|
||||||
local function emplaced(entity)
|
|
||||||
local r = jecs.entity_index_try_get_fast(
|
local r = jecs.entity_index_try_get_fast(
|
||||||
entity_index, entity)
|
entity_index, entity :: any)
|
||||||
|
|
||||||
if not r then
|
if not r then
|
||||||
return
|
return
|
||||||
|
@ -52,55 +66,39 @@ local function world_track(world, ...)
|
||||||
|
|
||||||
local archetype = r.archetype
|
local archetype = r.archetype
|
||||||
|
|
||||||
if jecs.query_match(q_shim :: any, archetype) then
|
if jecs.query_match(query, archetype) then
|
||||||
n += 1
|
callback(entity, jecs.OnAdd)
|
||||||
dense_array[n] = entity
|
|
||||||
sparse_array[entity] = n
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function removed(entity)
|
local function removed(entity: jecs.Entity, component: jecs.Id)
|
||||||
local i = sparse_array[entity]
|
local r = jecs.entity_index_try_get_fast(
|
||||||
if i ~= n then
|
entity_index, entity :: any)
|
||||||
dense_array[i] = dense_array[n]
|
|
||||||
|
if not r then
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
dense_array[n] = nil
|
local archetype = r.archetype
|
||||||
|
|
||||||
|
if jecs.query_match(query, archetype) then
|
||||||
|
callback(entity, jecs.OnRemove)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, term in terms do
|
for _, term in terms do
|
||||||
world:added(term, emplaced)
|
world:added(term, emplaced)
|
||||||
world:changed(term, emplaced)
|
world:removed(term, removed)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function iter()
|
|
||||||
local i = n
|
|
||||||
return function()
|
|
||||||
local row = i
|
|
||||||
if row == 0 then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
i -= 1
|
|
||||||
return dense_array[row] :: any
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local it = {
|
|
||||||
__iter = iter,
|
|
||||||
without = function(self, ...)
|
|
||||||
q_shim.filter_without = { ... }
|
|
||||||
return self
|
|
||||||
end
|
|
||||||
}
|
|
||||||
return setmetatable(it, it)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function observers_add(world)
|
local function observers_add(world: jecs.World & { [string]: any }): PatchedWorld
|
||||||
local signals = {
|
local signals = {
|
||||||
added = {},
|
added = {},
|
||||||
emplaced = {},
|
emplaced = {},
|
||||||
removed = {}
|
removed = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
world.added = function(_, component, fn)
|
world.added = function(_, component, fn)
|
||||||
local listeners = signals.added[component]
|
local listeners = signals.added[component]
|
||||||
if not listeners then
|
if not listeners then
|
||||||
|
@ -109,7 +107,7 @@ local function observers_add(world)
|
||||||
local idr = jecs.id_record_ensure(world, component)
|
local idr = jecs.id_record_ensure(world, component)
|
||||||
idr.hooks.on_add = function(entity)
|
idr.hooks.on_add = function(entity)
|
||||||
for _, listener in listeners do
|
for _, listener in listeners do
|
||||||
listener(entity)
|
listener(entity, component)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -124,7 +122,7 @@ local function observers_add(world)
|
||||||
local idr = jecs.id_record_ensure(world, component)
|
local idr = jecs.id_record_ensure(world, component)
|
||||||
idr.hooks.on_change = 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, component, value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -139,7 +137,7 @@ local function observers_add(world)
|
||||||
local idr = jecs.id_record_ensure(world, component)
|
local idr = jecs.id_record_ensure(world, component)
|
||||||
idr.hooks.on_remove = function(entity)
|
idr.hooks.on_remove = function(entity)
|
||||||
for _, listener in listeners do
|
for _, listener in listeners do
|
||||||
listener(entity)
|
listener(entity, component)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -148,10 +146,24 @@ local function observers_add(world)
|
||||||
|
|
||||||
world.signals = signals
|
world.signals = signals
|
||||||
|
|
||||||
world.track = world_track
|
|
||||||
|
|
||||||
world.observer = observers_new
|
world.observer = observers_new
|
||||||
|
|
||||||
|
world.monitor = monitors_new
|
||||||
|
|
||||||
return world
|
return world
|
||||||
end
|
end
|
||||||
|
|
||||||
return observers_add
|
local world = jecs.world()
|
||||||
|
|
||||||
|
observers_add(world):observer({
|
||||||
|
callback = function() end,
|
||||||
|
query = world:query(1 :: any)
|
||||||
|
})
|
||||||
|
|
||||||
|
observers_add(world):added(1 :: any, function()
|
||||||
|
|
||||||
|
end)
|
||||||
|
|
||||||
|
return {
|
||||||
|
add = observers_add
|
||||||
|
}
|
||||||
|
|
|
@ -2487,9 +2487,9 @@ return {
|
||||||
|
|
||||||
ECS_ID_DELETE = ECS_ID_DELETE,
|
ECS_ID_DELETE = ECS_ID_DELETE,
|
||||||
|
|
||||||
IS_PAIR = ECS_IS_PAIR,
|
IS_PAIR = (ECS_IS_PAIR :: any) :: <P, O>(pair: Pair<P, O>) -> boolean,
|
||||||
pair_first = ecs_pair_first,
|
pair_first = (ecs_pair_first :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<P>,
|
||||||
pair_second = ecs_pair_second,
|
pair_second = (ecs_pair_second :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<O>,
|
||||||
entity_index_get_alive = entity_index_get_alive,
|
entity_index_get_alive = entity_index_get_alive,
|
||||||
|
|
||||||
archetype_append_to_records = archetype_append_to_records,
|
archetype_append_to_records = archetype_append_to_records,
|
||||||
|
|
|
@ -1,46 +1,89 @@
|
||||||
local jecs = require("@jecs")
|
local jecs = require("@jecs")
|
||||||
local observers_add = require("@addons/observers")
|
local testkit = require("@testkit")
|
||||||
|
local test = testkit.test()
|
||||||
|
local CASE, TEST, FINISH, CHECK = test.CASE, test.TEST, test.FINISH, test.CHECK
|
||||||
|
local observers = require("@addons/observers")
|
||||||
|
local observers_add = observers.add
|
||||||
|
|
||||||
local world = jecs.world()
|
|
||||||
observers_add(world)
|
|
||||||
|
|
||||||
local A = world:component()
|
TEST("addons/observers", function()
|
||||||
local B = world:component()
|
local world = observers_add(jecs.world())
|
||||||
local C = world:component()
|
|
||||||
|
|
||||||
world:observer({
|
|
||||||
query = world:query(),
|
do CASE "Ensure ordering between signals and observers"
|
||||||
callback = function(entity)
|
local A = world:component()
|
||||||
buf ..= debug.info(2, "sl")
|
local B = world:component()
|
||||||
|
|
||||||
|
local count = 0
|
||||||
|
local function counter()
|
||||||
|
count += 1
|
||||||
|
end
|
||||||
|
world:observer({
|
||||||
|
callback = counter,
|
||||||
|
query = world:query(A, B),
|
||||||
|
})
|
||||||
|
|
||||||
|
world:added(A, counter)
|
||||||
|
world:added(A, counter)
|
||||||
|
|
||||||
|
world:removed(A, counter)
|
||||||
|
|
||||||
|
local e = world:entity()
|
||||||
|
world:add(e, A)
|
||||||
|
CHECK(count == 2)
|
||||||
|
|
||||||
|
world:add(e, B)
|
||||||
|
CHECK(count == 3)
|
||||||
|
|
||||||
|
world:remove(e, A)
|
||||||
|
CHECK(count == 4)
|
||||||
end
|
end
|
||||||
})
|
|
||||||
|
|
||||||
local i = 0
|
do CASE "Rematch entities in observers"
|
||||||
world:added(A, function(entity)
|
local A = world:component()
|
||||||
assert(i == 0)
|
|
||||||
i += 1
|
|
||||||
end)
|
|
||||||
world:added(A, function(entity)
|
|
||||||
assert(i == 1)
|
|
||||||
i += 1
|
|
||||||
end)
|
|
||||||
|
|
||||||
world:removed(A, function(entity)
|
local count = 0
|
||||||
assert(false)
|
local function counter()
|
||||||
end)
|
count += 1
|
||||||
|
end
|
||||||
|
world:observer({
|
||||||
|
query = world:query(A),
|
||||||
|
callback = counter
|
||||||
|
})
|
||||||
|
|
||||||
local observer = world:observer({
|
local e = world:entity()
|
||||||
query = world:query(A, B),
|
world:set(e, A, true)
|
||||||
callback = function(entity)
|
CHECK(count == 1)
|
||||||
assert(i == 2)
|
world:remove(e, A)
|
||||||
i += 1
|
CHECK(count == 1)
|
||||||
|
world:set(e, A, true)
|
||||||
|
CHECK(count == 2)
|
||||||
|
world:set(e, A, true)
|
||||||
|
CHECK(count == 3)
|
||||||
end
|
end
|
||||||
})
|
|
||||||
|
|
||||||
|
do CASE "Don't report changed components in monitor"
|
||||||
|
local A = world:component()
|
||||||
|
local count = 0
|
||||||
|
local function counter()
|
||||||
|
count += 1
|
||||||
|
end
|
||||||
|
|
||||||
local e = world:entity()
|
world:monitor({
|
||||||
world:add(e, A)
|
query = world:query(A),
|
||||||
assert(i == 2)
|
callback = counter
|
||||||
|
})
|
||||||
|
|
||||||
world:add(e, B)
|
local e = world:entity()
|
||||||
assert(i == 3)
|
world:set(e, A, true)
|
||||||
|
CHECK(count == 1)
|
||||||
|
world:remove(e, A)
|
||||||
|
CHECK(count == 2)
|
||||||
|
world:set(e, A, true)
|
||||||
|
CHECK(count == 3)
|
||||||
|
world:set(e, A, true)
|
||||||
|
CHECK(count == 3)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
return FINISH()
|
||||||
|
|
2100
test/tests.luau
2100
test/tests.luau
File diff suppressed because it is too large
Load diff
|
@ -70,7 +70,51 @@ local function components(world: jecs.World, entity: any)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local entity_index_try_get_any = jecs.entity_index_try_get_any
|
||||||
|
|
||||||
|
local function stringify(world: jecs.World)
|
||||||
|
local function record(e: jecs.Entity): jecs.Record
|
||||||
|
return entity_index_try_get_any(world.entity_index :: any, e :: any) :: any
|
||||||
|
end
|
||||||
|
local function tbl(e: jecs.Entity)
|
||||||
|
return record(e).archetype
|
||||||
|
end
|
||||||
|
local function archetype(e: jecs.Entity)
|
||||||
|
return tbl(e).type
|
||||||
|
end
|
||||||
|
local function records(e: jecs.Entity)
|
||||||
|
return tbl(e).records
|
||||||
|
end
|
||||||
|
local function columns(e: jecs.Entity)
|
||||||
|
return tbl(e).columns
|
||||||
|
end
|
||||||
|
local function row(e: jecs.Entity)
|
||||||
|
return record(e).row
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Important to order them in the order of their columns
|
||||||
|
local function tuple(e, ...)
|
||||||
|
for i, column in columns(e) do
|
||||||
|
if select(i, ...) ~= column[row(e)] then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
record = record,
|
||||||
|
tbl = tbl,
|
||||||
|
archetype = archetype,
|
||||||
|
records = records,
|
||||||
|
row = row,
|
||||||
|
tuple = tuple,
|
||||||
|
columns = columns
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
components = components,
|
components = components,
|
||||||
prettify = pe,
|
prettify = pe,
|
||||||
|
stringify = stringify
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,10 @@
|
||||||
-- v0.7.3
|
-- v0.7.3
|
||||||
-- MIT License
|
-- MIT License
|
||||||
-- Copyright (c) 2022 centau
|
-- Copyright (c) 2022 centau
|
||||||
|
--
|
||||||
|
-- Some changes that I have made to this module is to evaluate the tests lazily,
|
||||||
|
-- this way only focused tests will actually be ran rather than just focusing their output.
|
||||||
|
--
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
local disable_ansi = false
|
local disable_ansi = false
|
||||||
|
@ -248,7 +252,7 @@ local function FOCUS()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function FINISH(): boolean
|
local function FINISH(): number
|
||||||
local success = true
|
local success = true
|
||||||
local total_cases = 0
|
local total_cases = 0
|
||||||
local passed_cases = 0
|
local passed_cases = 0
|
||||||
|
@ -311,7 +315,8 @@ local function FINISH(): boolean
|
||||||
print((fails > 0 and color.red or color.green)(`{fails} {fails == 1 and "fail" or "fails"}`))
|
print((fails > 0 and color.red or color.green)(`{fails} {fails == 1 and "fail" or "fails"}`))
|
||||||
|
|
||||||
check_for_focused = false
|
check_for_focused = false
|
||||||
return success, table.clear(tests)
|
table.clear(tests)
|
||||||
|
return math.clamp(fails, 0, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function SKIP()
|
local function SKIP()
|
||||||
|
|
Loading…
Reference in a new issue