From 8412efda977c1c76885eae1d0b4a721cf71162f2 Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Sat, 28 Feb 2026 14:08:49 -0800 Subject: Upgrading JDK and adding Observable interface --- .../src/test/java/coffee/liz/ecs/DAGWorldTest.java | 20 ++++++++++++--- .../test/java/coffee/liz/ecs/model/EntityTest.java | 30 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) (limited to 'core/src/test/java/coffee/liz') diff --git a/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java b/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java index cf6cdad..4825e36 100644 --- a/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java +++ b/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java @@ -68,19 +68,31 @@ public class DAGWorldTest { } @Test - public void updateRefreshesComponentCacheAfterEntityMutations() { + public void cacheTracksComponentMutationsViaEntityEvents() { final DAGWorld world = new DAGWorld<>(); final Entity subject = world.createEntity(); - world.update("state", 0); assertTrue(world.resolve(Query.allOf(PositionComponent.class)).isEmpty()); subject.add(new PositionComponent()); - world.update("state", 0); assertEquals(1, world.resolve(Query.allOf(PositionComponent.class)).size()); subject.remove(PositionComponent.class); - world.update("state", 0); + assertTrue(world.resolve(Query.allOf(PositionComponent.class)).isEmpty()); + } + + @Test + public void removedEntityNoLongerMutatesWorldCache() { + final DAGWorld world = new DAGWorld<>(); + final Entity subject = world.createEntity(); + + subject.add(new PositionComponent()); + assertEquals(Set.of(subject), world.resolve(Query.allOf(PositionComponent.class))); + + world.removeEntity(subject); + subject.remove(PositionComponent.class); + subject.add(new PositionComponent()); + assertTrue(world.resolve(Query.allOf(PositionComponent.class)).isEmpty()); } diff --git a/core/src/test/java/coffee/liz/ecs/model/EntityTest.java b/core/src/test/java/coffee/liz/ecs/model/EntityTest.java index a8fd1e3..bb17296 100644 --- a/core/src/test/java/coffee/liz/ecs/model/EntityTest.java +++ b/core/src/test/java/coffee/liz/ecs/model/EntityTest.java @@ -6,6 +6,11 @@ import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import coffee.liz.ecs.events.ComponentAdded; +import coffee.liz.ecs.events.ComponentRemoved; +import coffee.liz.ecs.events.EntityEvent; +import coffee.liz.ecs.events.Hook; + import lombok.RequiredArgsConstructor; import org.junit.jupiter.api.Test; @@ -15,6 +20,7 @@ import org.junit.jupiter.params.provider.MethodSource; import java.util.Collection; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Stream; final class EntityTest { @@ -75,6 +81,30 @@ final class EntityTest { assertTrue(entity.componentTypes().isEmpty()); } + @Test + public void subscribeReceivesEmittedEvents() { + final Entity entity = Entity.builder().id(51).build(); + final AtomicInteger addedCount = new AtomicInteger(0); + final AtomicInteger removedCount = new AtomicInteger(0); + + final Hook hook = entity.subscribe(event -> { + if (event instanceof ComponentAdded) { + addedCount.incrementAndGet(); + } + if (event instanceof ComponentRemoved) { + removedCount.incrementAndGet(); + } + }); + + entity.add(new AlphaComponent("a")); + entity.remove(AlphaComponent.class); + + assertEquals(1, addedCount.get()); + assertEquals(1, removedCount.get()); + + entity.unsubscribe(hook); + } + private record AlphaComponent(String name) implements Component { } -- cgit v1.2.3-70-g09d2