aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/java/coffee/liz/ecs
diff options
context:
space:
mode:
authorElizabeth Hunt <me@liz.coffee>2026-02-15 10:08:30 -0800
committerElizabeth Hunt <me@liz.coffee>2026-02-15 10:12:08 -0800
commitd6a294e14604aecd9046cbc19dc1837ca694fbb0 (patch)
tree616e87e59f9ee95253acce05aa26405523a9acd7 /core/src/test/java/coffee/liz/ecs
parent347564c592325007cb81dbdf1a07831c2ae003c3 (diff)
downloadthe-abstraction-engine-java-d6a294e14604aecd9046cbc19dc1837ca694fbb0.tar.gz
the-abstraction-engine-java-d6a294e14604aecd9046cbc19dc1837ca694fbb0.zip
feat: Adding ValueEmitter, LambdaEvaluatorSystem
Diffstat (limited to 'core/src/test/java/coffee/liz/ecs')
-rw-r--r--core/src/test/java/coffee/liz/ecs/DAGWorldTest.java83
1 files changed, 40 insertions, 43 deletions
diff --git a/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java b/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java
index 648745b..b596876 100644
--- a/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java
+++ b/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java
@@ -8,55 +8,60 @@ import coffee.liz.ecs.model.Component;
import coffee.liz.ecs.model.Entity;
import coffee.liz.ecs.model.System;
import coffee.liz.ecs.model.World;
+import coffee.liz.ecs.model.Query;
import lombok.RequiredArgsConstructor;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
import java.time.Duration;
import java.util.Collection;
-import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
-final class DAGWorldTest {
- @Test
- public void queryReturnsEntitiesMatchingAllRequestedComponents() {
- final DAGWorld<String> world = new DAGWorld<>(Map.of());
- final Entity matching = world.createEntity();
- matching.add(new PositionComponent());
- matching.add(new VelocityComponent());
- final Entity partial = world.createEntity();
- partial.add(new PositionComponent());
- final Entity nonMatching = world.createEntity();
- nonMatching.add(new VelocityComponent());
+public class DAGWorldTest {
+ @ParameterizedTest
+ @MethodSource("queryScenarios")
+ public void queryResolvesExpectedEntities(final Query query, final Set<String> expectedEntityKeys) {
+ final World<String> world = new DAGWorld<>();
+ final Entity both = world.createEntity();
+ both.add(new PositionComponent());
+ both.add(new VelocityComponent());
+ final Entity positionOnly = world.createEntity();
+ positionOnly.add(new PositionComponent());
+ final Entity velocityOnly = world.createEntity();
+ velocityOnly.add(new VelocityComponent());
+ final Entity neither = world.createEntity();
world.update("state", Duration.ZERO);
- final Set<Entity> results = world.query(Set.of(PositionComponent.class, VelocityComponent.class));
-
- assertEquals(Set.of(matching), results);
+ final Map<String, Entity> entities = Map.of("both", both, "positionOnly", positionOnly, "velocityOnly",
+ velocityOnly, "neither", neither);
+ final Set<Entity> expectedEntities = expectedEntityKeys.stream().map(entities::get).collect(Collectors.toSet());
+ assertEquals(expectedEntities, world.resolve(query));
}
- @Test
- public void queryWithoutComponentsReturnsAllEntities() {
- final DAGWorld<String> world = new DAGWorld<>(Map.of());
- final Entity entityOne = world.createEntity();
- final Entity entityTwo = world.createEntity();
-
- final Set<Entity> results = world.query(List.<Class<? extends Component>>of());
-
- assertEquals(Set.of(entityOne, entityTwo), results);
+ private static Stream<Arguments> queryScenarios() {
+ return Stream.of(Arguments.of(Query.allOf(PositionComponent.class, VelocityComponent.class), Set.of("both")),
+ Arguments.of(Query.allOf(), Set.of("both", "positionOnly", "velocityOnly", "neither")),
+ Arguments.of(Query.anyOf(PositionComponent.class, VelocityComponent.class),
+ Set.of("both", "positionOnly", "velocityOnly")),
+ Arguments.of(Query.noneOf(PositionComponent.class, VelocityComponent.class), Set.of("neither")));
}
@Test
public void updateExecutesSystemsInTopologicalOrder() {
final CopyOnWriteArrayList<String> executionLog = new CopyOnWriteArrayList<>();
- final DAGWorld<String> world = new DAGWorld<>(Map.of(SystemC.class, new SystemC(executionLog), SystemA.class,
- new SystemA(executionLog), SystemB.class, new SystemB(executionLog)));
+ final DAGWorld<String> world = new DAGWorld<>(new SystemC(executionLog), new SystemA(executionLog),
+ new SystemB(executionLog));
world.update("state", Duration.ZERO);
assertEquals(List.of("A", "B", "C"), executionLog);
@@ -64,29 +69,27 @@ final class DAGWorldTest {
@Test
public void updateRefreshesComponentCacheAfterEntityMutations() {
- final DAGWorld<String> world = new DAGWorld<>(Map.of());
+ final DAGWorld<String> world = new DAGWorld<>();
final Entity subject = world.createEntity();
world.update("state", Duration.ZERO);
- assertTrue(world.query(Set.of(PositionComponent.class)).isEmpty());
+ assertTrue(world.resolve(Query.allOf(PositionComponent.class)).isEmpty());
subject.add(new PositionComponent());
world.update("state", Duration.ZERO);
- assertEquals(1, world.query(Set.of(PositionComponent.class)).size());
+ assertEquals(1, world.resolve(Query.allOf(PositionComponent.class)).size());
subject.remove(PositionComponent.class);
world.update("state", Duration.ZERO);
- assertTrue(world.query(Set.of(PositionComponent.class)).isEmpty());
+ assertTrue(world.resolve(Query.allOf(PositionComponent.class)).isEmpty());
}
@Test
public void queryFindsComponentsAddedByEarlierSystemInSameTick() {
final List<Set<Entity>> queryResults = new CopyOnWriteArrayList<>();
- final Map<Class<? extends System<String>>, System<String>> systems = new LinkedHashMap<>();
- systems.put(ComponentAdderSystem.class, new ComponentAdderSystem());
- systems.put(ComponentReaderSystem.class, new ComponentReaderSystem(queryResults));
- final DAGWorld<String> world = new DAGWorld<>(systems);
+ final DAGWorld<String> world = new DAGWorld<>(new ComponentAdderSystem(),
+ new ComponentReaderSystem(queryResults));
final Entity entity = world.createEntity();
entity.add(new PositionComponent());
@@ -99,13 +102,7 @@ final class DAGWorldTest {
@Test
public void circularDependencyDetectionThrowsIllegalStateException() {
- final Map<Class<? extends System<String>>, System<String>> systems = new LinkedHashMap<>();
- final SystemCycleA systemA = new SystemCycleA();
- final SystemCycleB systemB = new SystemCycleB();
- systems.put(SystemCycleA.class, systemA);
- systems.put(SystemCycleB.class, systemB);
-
- assertThrows(IllegalStateException.class, () -> new DAGWorld<>(systems));
+ assertThrows(IllegalStateException.class, () -> new DAGWorld<>(new SystemCycleA(), new SystemCycleB()));
}
private static final class PositionComponent implements Component {
@@ -188,7 +185,7 @@ final class DAGWorldTest {
@Override
public void update(final World<String> world, final String state, final Duration duration) {
- world.query(Set.of(PositionComponent.class)).forEach(e -> e.add(new VelocityComponent()));
+ world.resolve(Query.allOf(PositionComponent.class)).forEach(e -> e.add(new VelocityComponent()));
}
}
@@ -203,7 +200,7 @@ final class DAGWorldTest {
@Override
public void update(final World<String> world, final String state, final Duration duration) {
- queryResults.add(world.query(Set.of(VelocityComponent.class, PositionComponent.class)));
+ queryResults.add(world.resolve(Query.allOf(VelocityComponent.class, PositionComponent.class)));
}
}
}