-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
CraftTweakerGameTests.java
90 lines (69 loc) · 3.63 KB
/
CraftTweakerGameTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.blamejared.crafttweaker.gametest;
import com.blamejared.crafttweaker.platform.Services;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.gametest.framework.GameTestGenerator;
import net.minecraft.gametest.framework.GameTestHelper;
import net.minecraft.gametest.framework.StructureUtils;
import net.minecraft.gametest.framework.TestFunction;
import net.minecraft.world.level.block.Rotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class CraftTweakerGameTests {
@GameTestGenerator
public static List<TestFunction> getTests() {
List<TestFunction> functions = new ArrayList<>();
Services.PLATFORM.findClassesWithAnnotation(CraftTweakerGameTestHolder.class).forEach(aClass -> {
for(Method method : aClass.getDeclaredMethods()) {
if(!method.isAnnotationPresent(GameTest.class)) {
continue;
}
GameTest annotation = method.getAnnotation(GameTest.class);
String className = aClass.getSimpleName().toLowerCase();
String testName = className + "." + method.getName().toLowerCase();
String template = annotation.template().isEmpty() ? testName : annotation.template();
String batch = annotation.batch();
Rotation rotation = StructureUtils.getRotationForRotationSteps(annotation.rotationSteps());
ModifingConsumer consumer = new ModifingConsumer(aClass, method, Modifier.from(method));
functions.add(new TestFunction(batch, testName, template, rotation, annotation.timeoutTicks(), annotation.setupTicks(), annotation.required(), annotation.requiredSuccesses(), annotation.attempts(), consumer));
}
});
return functions;
}
public static class ModifingConsumer implements Consumer<GameTestHelper> {
private final Class<?> testClass;
private final Method testMethod;
private final Modifier modifier;
public ModifingConsumer(Class<?> testClass, Method testMethod, Modifier modifier) {
this.testClass = testClass;
this.testMethod = testMethod;
this.modifier = modifier;
}
@Override
public void accept(GameTestHelper gameTestHelper) {
Object instance;
try {
instance = testClass.newInstance();
} catch(InstantiationException | IllegalAccessException e) {
throw new RuntimeException("Error while creating test class " + testClass, e.getCause());
}
try {
testMethod.invoke(instance, gameTestHelper);
if(modifier.isImplicitSuccession()) {
gameTestHelper.succeed();
}
} catch(IllegalAccessException e) {
throw new RuntimeException("Failed to invoke test method (%s) in (%s) because %s".formatted(testMethod.getName(), testMethod.getDeclaringClass()
.getCanonicalName(), e.getMessage()), e);
} catch(InvocationTargetException e) {
if(e.getCause() instanceof RuntimeException runtimeException) {
throw runtimeException;
} else {
throw new RuntimeException(e.getCause());
}
}
}
}
}