Skip to content

Commit

Permalink
feat: provide Engines and MainLoop classes for injection to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keturn committed Nov 2, 2021
1 parent d9e12ec commit b891e14
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ public void beforeAll(ExtensionContext context) {

@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Class<?> type = parameterContext.getParameter().getType();
ModuleTestingHelper helper = getHelper(extensionContext);
return helper.getHostContext().get(parameterContext.getParameter().getType()) != null
|| parameterContext.getParameter().getType().equals(ModuleTestingHelper.class);
return helper.getHostContext().get(type) != null
|| type.isAssignableFrom(Engines.class)
|| type.isAssignableFrom(MainLoop.class)
|| type.isAssignableFrom(ModuleTestingHelper.class);
}

@Override
Expand All @@ -77,10 +80,15 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
}

private Object getDIInstance(ModuleTestingHelper helper, Class<?> type) {
if (type.equals(ModuleTestingHelper.class)) {
if (type.isAssignableFrom(Engines.class)) {
return helper.engines;
} else if (type.isAssignableFrom(MainLoop.class)) {
return helper.mainLoop;
} else if (type.isAssignableFrom(ModuleTestingHelper.class)) {
return helper;
} else {
return helper.getHostContext().get(type);
}
return helper.getHostContext().get(type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
*/
public class ModuleTestingHelper implements ModuleTestingEnvironment {

protected final Engines engines;
protected final MainLoop mainLoop;
final Engines engines;
final MainLoop mainLoop;

protected ModuleTestingHelper(Set<String> dependencies, String worldGeneratorUri) {
engines = new Engines(dependencies, worldGeneratorUri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class ChunkRegionFutureTest {
WorldProvider world;

@Test
void createChunkRegionFuture(EntityManager entityManager, RelevanceSystem relevanceSystem, ModuleTestingHelper mte) {
void createChunkRegionFuture(EntityManager entityManager, RelevanceSystem relevanceSystem, MainLoop mainLoop) {
ChunkRegionFuture chunkRegionFuture = ChunkRegionFuture.create(entityManager, relevanceSystem, center, sizeInChunks);

mte.runUntil(chunkRegionFuture.getFuture());
mainLoop.runUntil(chunkRegionFuture.getFuture());

Vector3fc someplaceInside = center.add(
sizeInChunks.x() * Chunks.SIZE_X / 3f, 0, 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ public class ModuleTestingEnvironmentTest {
public static final int THE_ANSWER = 42;

@Test
public void runUntilWithUnsatisfiedFutureExplainsTimeout(ModuleTestingHelper mte) {
public void runUntilWithUnsatisfiedFutureExplainsTimeout(MainLoop mainLoop) {
SettableFuture<?> unsatisfiedFuture = SettableFuture.create();

UncheckedTimeoutException exception = assertThrows(UncheckedTimeoutException.class,
// TODO: change the timeout for this test so it doesn't always take
// a minimum of 30 seconds.
() -> mte.runUntil(unsatisfiedFuture));
() -> mainLoop.runUntil(unsatisfiedFuture));
assertThat(exception).hasMessageThat().contains("default timeout");
}

@Test
public void runUntilWithImmediateFutureReturnsValue(ModuleTestingHelper mte) {
public void runUntilWithImmediateFutureReturnsValue(MainLoop mainLoop) {
ListenableFuture<Integer> valueFuture = Futures.immediateFuture(THE_ANSWER);
assertThat(mte.runUntil(valueFuture)).isEqualTo(THE_ANSWER);
assertThat(mainLoop.runUntil(valueFuture)).isEqualTo(THE_ANSWER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,37 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.terasology.engine.entitySystem.entity.EntityManager;
import org.terasology.moduletestingenvironment.extension.Dependencies;
import org.terasology.engine.registry.In;
import org.terasology.moduletestingenvironment.extension.Dependencies;

@Tag("MteTest")
@ExtendWith(MTEExtension.class)
@Dependencies({"engine", "ModuleTestingEnvironment"})
public class NestedTest {
@In
public static ModuleTestingHelper outerHelper;
public static Engines outerEngines;

@In
public static EntityManager outerManager;

@Test
public void outerTest() {
Assertions.assertNotNull(outerHelper);
Assertions.assertNotNull(outerEngines);
Assertions.assertNotNull(outerManager);
}

@Nested
class NestedTestClass {
@In
ModuleTestingHelper innerHelper;
Engines innerEngines;

@In
EntityManager innerManager;

@Test
public void innerTest() {
Assertions.assertSame(innerManager, outerManager);
Assertions.assertSame(innerHelper, outerHelper);
Assertions.assertSame(innerEngines, outerEngines);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.terasology.moduletestingenvironment.extension.Dependencies;
import org.terasology.engine.registry.In;
import org.terasology.engine.world.WorldProvider;
import org.terasology.engine.world.block.BlockManager;
import org.terasology.moduletestingenvironment.extension.Dependencies;

@Tag("MteTest")
@ExtendWith(MTEExtension.class)
Expand All @@ -22,11 +22,11 @@ public class WorldProviderTest {
@In
BlockManager blockManager;
@In
ModuleTestingHelper helper;
MainLoop mainLoop;

@Test
public void defaultWorldSetBlockTest() {
helper.forceAndWaitForGeneration(new Vector3i());
mainLoop.forceAndWaitForGeneration(new Vector3i());

// this will change if the worldgenerator changes or the seed is altered, the main point is that this is a real
// block type and not engine:unloaded
Expand Down

0 comments on commit b891e14

Please sign in to comment.