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 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 query = description.query
|
||||
local callback = description.callback
|
||||
local terms = query.filter_with
|
||||
local terms = query.filter_with :: { jecs.Id }
|
||||
if not terms then
|
||||
local ids = query.ids
|
||||
query.filter_with = ids
|
||||
terms = ids
|
||||
end
|
||||
|
||||
local entity_index = world.entity_index
|
||||
local function emplaced(entity)
|
||||
local entity_index = world.entity_index :: any
|
||||
local function emplaced(entity: jecs.Entity)
|
||||
local r = jecs.entity_index_try_get_fast(
|
||||
entity_index, entity)
|
||||
entity_index, entity :: any)
|
||||
|
||||
if not r then
|
||||
return
|
||||
|
@ -33,18 +45,20 @@ local function observers_new(world, description)
|
|||
end
|
||||
end
|
||||
|
||||
local function world_track(world, ...)
|
||||
local entity_index = world.entity_index
|
||||
local terms = { ... }
|
||||
local q_shim = { filter_with = terms }
|
||||
local function monitors_new(world, description)
|
||||
local query = description.query
|
||||
local callback = description.callback
|
||||
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 dense_array = {}
|
||||
local sparse_array = {}
|
||||
|
||||
local function emplaced(entity)
|
||||
local entity_index = world.entity_index :: any
|
||||
local function emplaced(entity: jecs.Entity)
|
||||
local r = jecs.entity_index_try_get_fast(
|
||||
entity_index, entity)
|
||||
entity_index, entity :: any)
|
||||
|
||||
if not r then
|
||||
return
|
||||
|
@ -52,55 +66,39 @@ local function world_track(world, ...)
|
|||
|
||||
local archetype = r.archetype
|
||||
|
||||
if jecs.query_match(q_shim :: any, archetype) then
|
||||
n += 1
|
||||
dense_array[n] = entity
|
||||
sparse_array[entity] = n
|
||||
if jecs.query_match(query, archetype) then
|
||||
callback(entity, jecs.OnAdd)
|
||||
end
|
||||
end
|
||||
|
||||
local function removed(entity)
|
||||
local i = sparse_array[entity]
|
||||
if i ~= n then
|
||||
dense_array[i] = dense_array[n]
|
||||
local function removed(entity: jecs.Entity, component: jecs.Id)
|
||||
local r = jecs.entity_index_try_get_fast(
|
||||
entity_index, entity :: any)
|
||||
|
||||
if not r then
|
||||
return
|
||||
end
|
||||
|
||||
dense_array[n] = nil
|
||||
local archetype = r.archetype
|
||||
|
||||
if jecs.query_match(query, archetype) then
|
||||
callback(entity, jecs.OnRemove)
|
||||
end
|
||||
end
|
||||
|
||||
for _, term in terms do
|
||||
world:added(term, emplaced)
|
||||
world:changed(term, emplaced)
|
||||
world:removed(term, removed)
|
||||
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
|
||||
|
||||
local function observers_add(world)
|
||||
local function observers_add(world: jecs.World & { [string]: any }): PatchedWorld
|
||||
local signals = {
|
||||
added = {},
|
||||
emplaced = {},
|
||||
removed = {}
|
||||
}
|
||||
|
||||
world.added = function(_, component, fn)
|
||||
local listeners = signals.added[component]
|
||||
if not listeners then
|
||||
|
@ -109,7 +107,7 @@ local function observers_add(world)
|
|||
local idr = jecs.id_record_ensure(world, component)
|
||||
idr.hooks.on_add = function(entity)
|
||||
for _, listener in listeners do
|
||||
listener(entity)
|
||||
listener(entity, component)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -124,7 +122,7 @@ local function observers_add(world)
|
|||
local idr = jecs.id_record_ensure(world, component)
|
||||
idr.hooks.on_change = function(entity, value)
|
||||
for _, listener in listeners do
|
||||
listener(entity, value)
|
||||
listener(entity, component, value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -139,7 +137,7 @@ local function observers_add(world)
|
|||
local idr = jecs.id_record_ensure(world, component)
|
||||
idr.hooks.on_remove = function(entity)
|
||||
for _, listener in listeners do
|
||||
listener(entity)
|
||||
listener(entity, component)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -148,10 +146,24 @@ local function observers_add(world)
|
|||
|
||||
world.signals = signals
|
||||
|
||||
world.track = world_track
|
||||
|
||||
world.observer = observers_new
|
||||
|
||||
world.monitor = monitors_new
|
||||
|
||||
return world
|
||||
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,
|
||||
|
||||
IS_PAIR = ECS_IS_PAIR,
|
||||
pair_first = ecs_pair_first,
|
||||
pair_second = ecs_pair_second,
|
||||
IS_PAIR = (ECS_IS_PAIR :: any) :: <P, O>(pair: Pair<P, O>) -> boolean,
|
||||
pair_first = (ecs_pair_first :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<P>,
|
||||
pair_second = (ecs_pair_second :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<O>,
|
||||
entity_index_get_alive = entity_index_get_alive,
|
||||
|
||||
archetype_append_to_records = archetype_append_to_records,
|
||||
|
|
|
@ -1,46 +1,89 @@
|
|||
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()
|
||||
local B = world:component()
|
||||
local C = world:component()
|
||||
TEST("addons/observers", function()
|
||||
local world = observers_add(jecs.world())
|
||||
|
||||
world:observer({
|
||||
query = world:query(),
|
||||
callback = function(entity)
|
||||
buf ..= debug.info(2, "sl")
|
||||
|
||||
do CASE "Ensure ordering between signals and observers"
|
||||
local A = world:component()
|
||||
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
|
||||
})
|
||||
|
||||
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)
|
||||
do CASE "Rematch entities in observers"
|
||||
local A = world:component()
|
||||
|
||||
world:removed(A, function(entity)
|
||||
assert(false)
|
||||
end)
|
||||
local count = 0
|
||||
local function counter()
|
||||
count += 1
|
||||
end
|
||||
world:observer({
|
||||
query = world:query(A),
|
||||
callback = counter
|
||||
})
|
||||
|
||||
local observer = world:observer({
|
||||
query = world:query(A, B),
|
||||
callback = function(entity)
|
||||
assert(i == 2)
|
||||
i += 1
|
||||
local e = world:entity()
|
||||
world:set(e, A, true)
|
||||
CHECK(count == 1)
|
||||
world:remove(e, A)
|
||||
CHECK(count == 1)
|
||||
world:set(e, A, true)
|
||||
CHECK(count == 2)
|
||||
world:set(e, A, true)
|
||||
CHECK(count == 3)
|
||||
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:add(e, A)
|
||||
assert(i == 2)
|
||||
world:monitor({
|
||||
query = world:query(A),
|
||||
callback = counter
|
||||
})
|
||||
|
||||
world:add(e, B)
|
||||
assert(i == 3)
|
||||
local e = world:entity()
|
||||
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
|
||||
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 {
|
||||
components = components,
|
||||
prettify = pe,
|
||||
stringify = stringify
|
||||
}
|
||||
|
|
|
@ -3,6 +3,10 @@
|
|||
-- v0.7.3
|
||||
-- MIT License
|
||||
-- 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
|
||||
|
@ -248,7 +252,7 @@ local function FOCUS()
|
|||
end
|
||||
end
|
||||
|
||||
local function FINISH(): boolean
|
||||
local function FINISH(): number
|
||||
local success = true
|
||||
local total_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"}`))
|
||||
|
||||
check_for_focused = false
|
||||
return success, table.clear(tests)
|
||||
table.clear(tests)
|
||||
return math.clamp(fails, 0, 1)
|
||||
end
|
||||
|
||||
local function SKIP()
|
||||
|
|
Loading…
Reference in a new issue