diff options
| author | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-02-28 14:08:49 -0800 |
|---|---|---|
| committer | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-02-28 14:08:49 -0800 |
| commit | 8412efda977c1c76885eae1d0b4a721cf71162f2 (patch) | |
| tree | 4ff20bc346fd24aeb5881ea06855d7bea5f5d162 /core/src/test/java/coffee | |
| parent | 87c8a1e15e399d29f42b41a4ccb66a84c5f6bb9a (diff) | |
| download | dyl-8412efda977c1c76885eae1d0b4a721cf71162f2.tar.gz dyl-8412efda977c1c76885eae1d0b4a721cf71162f2.zip | |
Upgrading JDK and adding Observable interface
Diffstat (limited to 'core/src/test/java/coffee')
| -rw-r--r-- | core/src/test/java/coffee/liz/ecs/DAGWorldTest.java | 20 | ||||
| -rw-r--r-- | core/src/test/java/coffee/liz/ecs/model/EntityTest.java | 30 |
2 files changed, 46 insertions, 4 deletions
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<String> 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<String> 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<EntityEvent> 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 { } |
