diff options
Diffstat (limited to 'core/src/test/java/coffee/liz/ecs')
| -rw-r--r-- | core/src/test/java/coffee/liz/ecs/DAGWorldTest.java | 290 | ||||
| -rw-r--r-- | core/src/test/java/coffee/liz/ecs/model/EntityTest.java | 120 |
2 files changed, 205 insertions, 205 deletions
diff --git a/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java b/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java index a656d28..648745b 100644 --- a/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java +++ b/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java @@ -22,188 +22,188 @@ import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; 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()); + @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()); - world.update("state", Duration.ZERO); + world.update("state", Duration.ZERO); - final Set<Entity> results = world.query(Set.of(PositionComponent.class, VelocityComponent.class)); + final Set<Entity> results = world.query(Set.of(PositionComponent.class, VelocityComponent.class)); - assertEquals(Set.of(matching), results); - } + assertEquals(Set.of(matching), results); + } - @Test - public void queryWithoutComponentsReturnsAllEntities() { - final DAGWorld<String> world = new DAGWorld<>(Map.of()); - final Entity entityOne = world.createEntity(); - final Entity entityTwo = world.createEntity(); + @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()); + final Set<Entity> results = world.query(List.<Class<? extends Component>>of()); - assertEquals(Set.of(entityOne, entityTwo), results); - } + assertEquals(Set.of(entityOne, entityTwo), results); + } - @Test - public void updateExecutesSystemsInTopologicalOrder() { - final CopyOnWriteArrayList<String> executionLog = new CopyOnWriteArrayList<>(); + @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))); - world.update("state", Duration.ZERO); + final DAGWorld<String> world = new DAGWorld<>(Map.of(SystemC.class, new SystemC(executionLog), SystemA.class, + new SystemA(executionLog), SystemB.class, new SystemB(executionLog))); + world.update("state", Duration.ZERO); - assertEquals(List.of("A", "B", "C"), executionLog); - } + assertEquals(List.of("A", "B", "C"), executionLog); + } - @Test - public void updateRefreshesComponentCacheAfterEntityMutations() { - final DAGWorld<String> world = new DAGWorld<>(Map.of()); - final Entity subject = world.createEntity(); + @Test + public void updateRefreshesComponentCacheAfterEntityMutations() { + final DAGWorld<String> world = new DAGWorld<>(Map.of()); + final Entity subject = world.createEntity(); - world.update("state", Duration.ZERO); - assertTrue(world.query(Set.of(PositionComponent.class)).isEmpty()); + world.update("state", Duration.ZERO); + assertTrue(world.query(Set.of(PositionComponent.class)).isEmpty()); - subject.add(new PositionComponent()); - world.update("state", Duration.ZERO); - assertEquals(1, world.query(Set.of(PositionComponent.class)).size()); + subject.add(new PositionComponent()); + world.update("state", Duration.ZERO); + assertEquals(1, world.query(Set.of(PositionComponent.class)).size()); - subject.remove(PositionComponent.class); - world.update("state", Duration.ZERO); - assertTrue(world.query(Set.of(PositionComponent.class)).isEmpty()); - } + subject.remove(PositionComponent.class); + world.update("state", Duration.ZERO); + assertTrue(world.query(Set.of(PositionComponent.class)).isEmpty()); + } - @Test - public void queryFindsComponentsAddedByEarlierSystemInSameTick() { - final List<Set<Entity>> queryResults = new CopyOnWriteArrayList<>(); + @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 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 Entity entity = world.createEntity(); - entity.add(new PositionComponent()); + final Entity entity = world.createEntity(); + entity.add(new PositionComponent()); - world.update("state", Duration.ZERO); + world.update("state", Duration.ZERO); - assertEquals(1, queryResults.size()); - assertEquals(Set.of(entity), queryResults.getFirst()); - } + assertEquals(1, queryResults.size()); + assertEquals(Set.of(entity), queryResults.getFirst()); + } - @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); + @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<>(systems)); + } - private static final class PositionComponent implements Component { - } + private static final class PositionComponent implements Component { + } - private static final class VelocityComponent implements Component { - } + private static final class VelocityComponent implements Component { + } - @RequiredArgsConstructor - private abstract static class RecordingSystem implements System<String> { - private final List<String> log; - private final String label; + @RequiredArgsConstructor + private abstract static class RecordingSystem implements System<String> { + private final List<String> log; + private final String label; - @Override - public final void update(final World<String> world, final String state, final Duration duration) { - log.add(label); - } - } + @Override + public final void update(final World<String> world, final String state, final Duration duration) { + log.add(label); + } + } - private static final class SystemA extends RecordingSystem { - private SystemA(final List<String> log) { - super(log, "A"); - } + private static final class SystemA extends RecordingSystem { + private SystemA(final List<String> log) { + super(log, "A"); + } - @Override - public Collection<Class<? extends System<String>>> getDependencies() { - return List.of(); - } - } + @Override + public Collection<Class<? extends System<String>>> getDependencies() { + return List.of(); + } + } - private static final class SystemB extends RecordingSystem { - private SystemB(final List<String> log) { - super(log, "B"); - } + private static final class SystemB extends RecordingSystem { + private SystemB(final List<String> log) { + super(log, "B"); + } - @Override - public Collection<Class<? extends System<String>>> getDependencies() { - return Set.of(SystemA.class); - } - } + @Override + public Collection<Class<? extends System<String>>> getDependencies() { + return Set.of(SystemA.class); + } + } - private static final class SystemC extends RecordingSystem { - private SystemC(final List<String> log) { - super(log, "C"); - } + private static final class SystemC extends RecordingSystem { + private SystemC(final List<String> log) { + super(log, "C"); + } - @Override - public Collection<Class<? extends System<String>>> getDependencies() { - return Set.of(SystemB.class); - } - } + @Override + public Collection<Class<? extends System<String>>> getDependencies() { + return Set.of(SystemB.class); + } + } - private static final class SystemCycleA implements System<String> { - @Override - public Collection<Class<? extends System<String>>> getDependencies() { - return Set.of(SystemCycleB.class); - } + private static final class SystemCycleA implements System<String> { + @Override + public Collection<Class<? extends System<String>>> getDependencies() { + return Set.of(SystemCycleB.class); + } - @Override - public void update(final World<String> world, final String state, final Duration duration) { - } - } + @Override + public void update(final World<String> world, final String state, final Duration duration) { + } + } - private static final class SystemCycleB implements System<String> { - @Override - public Collection<Class<? extends System<String>>> getDependencies() { - return Set.of(SystemCycleA.class); - } + private static final class SystemCycleB implements System<String> { + @Override + public Collection<Class<? extends System<String>>> getDependencies() { + return Set.of(SystemCycleA.class); + } - @Override - public void update(final World<String> world, final String state, final Duration duration) { - } - } + @Override + public void update(final World<String> world, final String state, final Duration duration) { + } + } - private static final class ComponentAdderSystem implements System<String> { - @Override - public Collection<Class<? extends System<String>>> getDependencies() { - return List.of(); - } + private static final class ComponentAdderSystem implements System<String> { + @Override + public Collection<Class<? extends System<String>>> getDependencies() { + return List.of(); + } - @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())); - } - } + @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())); + } + } - @RequiredArgsConstructor - private static final class ComponentReaderSystem implements System<String> { - private final List<Set<Entity>> queryResults; + @RequiredArgsConstructor + private static final class ComponentReaderSystem implements System<String> { + private final List<Set<Entity>> queryResults; - @Override - public Collection<Class<? extends System<String>>> getDependencies() { - return Set.of(ComponentAdderSystem.class); - } + @Override + public Collection<Class<? extends System<String>>> getDependencies() { + return Set.of(ComponentAdderSystem.class); + } - @Override - public void update(final World<String> world, final String state, final Duration duration) { - queryResults.add(world.query(Set.of(VelocityComponent.class, PositionComponent.class))); - } - } + @Override + public void update(final World<String> world, final String state, final Duration duration) { + queryResults.add(world.query(Set.of(VelocityComponent.class, PositionComponent.class))); + } + } } 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 c9ce59a..a8fd1e3 100644 --- a/core/src/test/java/coffee/liz/ecs/model/EntityTest.java +++ b/core/src/test/java/coffee/liz/ecs/model/EntityTest.java @@ -18,80 +18,80 @@ import java.util.List; import java.util.stream.Stream; final class EntityTest { - @ParameterizedTest - @MethodSource("componentCombinationProvider") - public void hasAllReportsPresenceForComponentSets(final Collection<Class<? extends Component>> query, - final boolean expected) { - final Entity entity = Entity.builder().id(7).build(); - entity.add(new AlphaComponent("first")); - entity.add(new BetaComponent(3)); - entity.add(new GammaKeyedComponent()); + @ParameterizedTest + @MethodSource("componentCombinationProvider") + public void hasAllReportsPresenceForComponentSets(final Collection<Class<? extends Component>> query, + final boolean expected) { + final Entity entity = Entity.builder().id(7).build(); + entity.add(new AlphaComponent("first")); + entity.add(new BetaComponent(3)); + entity.add(new GammaKeyedComponent()); - assertEquals(expected, entity.hasAll(query)); - } + assertEquals(expected, entity.hasAll(query)); + } - private static Stream<Arguments> componentCombinationProvider() { - return Stream - .of(Arguments.of(List.of(AlphaComponent.class), true), Arguments.of(List.of(BetaComponent.class), true), - Arguments.of(List.of(AlphaComponent.class, BetaComponent.class, GammaComponent.class), true), - Arguments.of(List.of(AlphaComponent.class, BetaComponent.class, GammaKeyedComponent.class), - false), - Arguments.of(List.of(GammaComponent.class), true), - Arguments.of(List.of(GammaKeyedComponent.class), false)); - } + private static Stream<Arguments> componentCombinationProvider() { + return Stream + .of(Arguments.of(List.of(AlphaComponent.class), true), Arguments.of(List.of(BetaComponent.class), true), + Arguments.of(List.of(AlphaComponent.class, BetaComponent.class, GammaComponent.class), true), + Arguments.of(List.of(AlphaComponent.class, BetaComponent.class, GammaKeyedComponent.class), + false), + Arguments.of(List.of(GammaComponent.class), true), + Arguments.of(List.of(GammaKeyedComponent.class), false)); + } - @Test - public void getThrowsForMissingComponentsWithHelpfulMessage() { - final Entity entity = Entity.builder().id(99).build(); + @Test + public void getThrowsForMissingComponentsWithHelpfulMessage() { + final Entity entity = Entity.builder().id(99).build(); - final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, - () -> entity.get(AlphaComponent.class)); + final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, + () -> entity.get(AlphaComponent.class)); - assertTrue(thrown.getMessage().contains("AlphaComponent")); - assertTrue(thrown.getMessage().contains("99")); - } + assertTrue(thrown.getMessage().contains("AlphaComponent")); + assertTrue(thrown.getMessage().contains("99")); + } - @Test - public void addReplacesExistingComponentInstance() { - final Entity entity = Entity.builder().id(17).build(); - final AlphaComponent initial = new AlphaComponent("initial"); - entity.add(initial); + @Test + public void addReplacesExistingComponentInstance() { + final Entity entity = Entity.builder().id(17).build(); + final AlphaComponent initial = new AlphaComponent("initial"); + entity.add(initial); - final AlphaComponent replacement = new AlphaComponent("replacement"); - entity.add(replacement); + final AlphaComponent replacement = new AlphaComponent("replacement"); + entity.add(replacement); - assertSame(replacement, entity.get(AlphaComponent.class)); - } + assertSame(replacement, entity.get(AlphaComponent.class)); + } - @Test - public void removeClearsComponentPresence() { - final Entity entity = Entity.builder().id(45).build(); - entity.add(new BetaComponent(2)); - assertTrue(entity.has(BetaComponent.class)); + @Test + public void removeClearsComponentPresence() { + final Entity entity = Entity.builder().id(45).build(); + entity.add(new BetaComponent(2)); + assertTrue(entity.has(BetaComponent.class)); - entity.remove(BetaComponent.class); + entity.remove(BetaComponent.class); - assertFalse(entity.has(BetaComponent.class)); - assertTrue(entity.componentTypes().isEmpty()); - } + assertFalse(entity.has(BetaComponent.class)); + assertTrue(entity.componentTypes().isEmpty()); + } - private record AlphaComponent(String name) implements Component { - } + private record AlphaComponent(String name) implements Component { + } - private record BetaComponent(int level) implements Component { - } + private record BetaComponent(int level) implements Component { + } - @RequiredArgsConstructor - private class GammaComponent implements Component { - @Override - public Class<? extends Component> getKey() { - return GammaComponent.class; - } - } + @RequiredArgsConstructor + private class GammaComponent implements Component { + @Override + public Class<? extends Component> getKey() { + return GammaComponent.class; + } + } - private class GammaKeyedComponent extends GammaComponent { - } + private class GammaKeyedComponent extends GammaComponent { + } - private record ContextualComponent(int ownerId) implements Component { - } + private record ContextualComponent(int ownerId) implements Component { + } } |
