hammer/test/tests.luau
Mark Marks cab13e4213 chore: Make jecs a dependency, bump jecs to v0.3.0
+ Make jecs a wally dependency instead of a file in lib. This wasn't done earlier due to wally packages not being able to be used outside of Roblox without dynamically requiring them. Now, a script is used to download the latest source of jecsfrom Github associated with the version in the manifest.

+ Bump jecs-utils to v0.1.3 and release
2024-09-24 15:35:21 +02:00

237 lines
5.9 KiB
Text

--!strict
-- stylua: ignore start
local jecs = require("../jecs")
local jecs_utils = require("@jecs_utils")
local testkit = require("@testkit")
local collect = jecs_utils.collect
local handle = jecs_utils.handle
local replicator = jecs_utils.replicator
local ref = jecs_utils.ref
local command_buffer = jecs_utils.command_buffer
local signal = require("./signal")
local BENCH, START = testkit.benchmark()
local TEST, CASE, CHECK, FINISH, SKIP, FOCUS = testkit.test()
TEST("jecs_utils.collect()", function()
do CASE "collects"
local sig: signal.signal<number> = signal()
local flush = collect(sig)
local should = {}
for idx = 100, 1, -1 do
local n = math.random()
should[idx] = n
sig:fire(n)
end
for idx, n in flush do
CHECK(should[idx] == n)
end
end
end)
TEST("jecs_utils.handle()", function()
do CASE "has"
local world = jecs.World.new()
jecs_utils.initialize(world)
local entity = world:entity()
local tag = world:entity()
world:add(entity, tag)
CHECK(handle(entity):has(tag))
end
do CASE "get"
local world = jecs.World.new()
jecs_utils.initialize(world)
local entity = world:entity()
local component = world:component()
world:set(entity, component, 50)
CHECK(handle(entity):get(component) == 50)
end
do CASE "add"
local world = jecs.World.new()
jecs_utils.initialize(world)
local entity = world:entity()
local tag = world:entity()
handle(entity):add(tag)
CHECK(world:has(entity, tag))
end
do CASE "set"
local world = jecs.World.new()
jecs_utils.initialize(world)
local entity = world:entity()
local component = world:component()
handle(entity):set(component, 50)
CHECK(world:get(entity, component) == 50)
end
do CASE "remove"
local world = jecs.World.new()
jecs_utils.initialize(world)
local entity = world:entity()
local component = world:component()
handle(entity):set(component, 50)
CHECK(world:get(entity, component) == 50)
end
do CASE "delete"
local world = jecs.World.new()
jecs_utils.initialize(world)
local entity = world:entity()
handle(entity):delete()
CHECK(not world:contains(entity))
end
end)
TEST("jecs_utils.ref()", function()
do CASE "ref(abc) == ref(abc)"
local world = jecs.World.new()
jecs_utils.initialize(world)
local a: number = ref(1234):id()
local b: number = ref(1234):id()
CHECK(a == b)
end
end)
TEST("jecs_utils.replicator()", function()
do CASE "propagates difference"
local world = jecs.World.new()
local tag = world:entity()
local component: jecs.Entity<number> = world:component()
local entity1 = world:entity()
local entity2 = world:entity()
jecs_utils.initialize(world)
local rep = replicator(component, tag)
world:add(entity1, tag)
world:set(entity2, component, 50)
local difference: jecs_utils.changes = rep.calculate_difference() :: any
CHECK(difference ~= nil)
local world2 = jecs.World.new()
local component2: jecs.Entity<number> = world2:component()
local tag2 = world2:entity()
jecs_utils.initialize(world2)
local rep2 = replicator(component2, tag2)
rep2.apply_difference(difference)
CHECK(ref(`replicated-{entity1}`):has(tag2))
CHECK(ref(`replicated-{entity2}`):get(component2) == 50)
end
do CASE "propagates full data"
local world = jecs.World.new()
local tag = world:entity()
local component: jecs.Entity<number> = world:component()
local entity1 = world:entity()
local entity2 = world:entity()
jecs_utils.initialize(world)
local rep = replicator(component, tag)
world:add(entity1, tag)
world:set(entity2, component, 50)
local full_data = rep.get_full_data()
CHECK(full_data ~= nil)
local world2 = jecs.World.new()
local component2: jecs.Entity<number> = world2:component()
local tag2 = world2:entity()
jecs_utils.initialize(world2)
local rep2 = replicator(component2, tag2)
rep2.apply_difference(full_data)
CHECK(ref(`replicated-{entity1}`):has(tag2))
CHECK(ref(`replicated-{entity2}`):get(component2) == 50)
end
end)
TEST("jecs_utils.command_buffer", function()
do CASE "add"
local world = jecs.World.new()
jecs_utils.initialize(world)
local tag = world:entity()
local entity = world:entity()
command_buffer.add(entity, tag)
CHECK(not world:has(entity, tag))
command_buffer.flush()
CHECK(world:has(entity, tag))
end
do CASE "set"
local world = jecs.World.new()
jecs_utils.initialize(world)
local component = world:component()
local entity = world:entity()
command_buffer.set(entity, component, 50)
CHECK(not world:has(entity, component))
command_buffer.flush()
CHECK(world:get(entity, component) == 50)
end
do CASE "remove"
local world = jecs.World.new()
jecs_utils.initialize(world)
local component = world:component()
local entity = world:entity()
world:set(entity, component, 50)
command_buffer.remove(entity, component)
CHECK(world:has(entity, component))
command_buffer.flush()
CHECK(not world:has(entity, component))
end
do CASE "delete"
local world = jecs.World.new()
jecs_utils.initialize(world)
local entity = world:entity()
command_buffer.delete(entity)
command_buffer.flush()
CHECK(not world:contains(entity))
end
end)
FINISH()
-- stylua: ignore end