Skip to content

Commit

Permalink
Add GameTestLoaderHelpers for Forge and NeoForge
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Nov 1, 2024
1 parent 74e1e03 commit c42775d
Showing 1 changed file with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.cyclops.cyclopscore.gametest;

import com.google.common.collect.Lists;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.gametest.framework.GameTestAssertException;
import net.minecraft.gametest.framework.StructureUtils;
import net.minecraft.gametest.framework.TestFunction;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;

/**
* Utilities for loading game tests in a multi-loader environment.
* @author rubensworks
*/
public class GameTestLoaderHelpers {

public static Collection<TestFunction> generateCommonTests(String modId, Class<?>[] testClasses) throws InstantiationException, IllegalAccessException {
List<TestFunction> testsList = Lists.newArrayList();

for(Class<?> clazz : testClasses) {
Object instance = clazz.newInstance();
for (Method method : clazz.getDeclaredMethods()) {
if (method.isAnnotationPresent(GameTest.class)) {
GameTest gameTest = method.getAnnotation(GameTest.class);
testsList.add(new TestFunction(
modId,
modId + "." + method.getName(),
gameTest.template(),
StructureUtils.getRotationForRotationSteps(gameTest.rotationSteps()),
gameTest.timeoutTicks(),
gameTest.setupTicks(),
gameTest.required(),
gameTest.manualOnly(),
gameTest.attempts(),
gameTest.requiredSuccesses(),
gameTest.skyAccess(),
(gameTestHelpers) -> {
try {
method.invoke(instance, gameTestHelpers);
} catch (InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
throw new GameTestAssertException(e.getMessage());
}
}
));
}
}
}

return testsList;
}

}

0 comments on commit c42775d

Please sign in to comment.