Skip to content

Commit f60c235

Browse files
committed
feat: rework gametest api
should be more concise now
1 parent b203ca5 commit f60c235

File tree

15 files changed

+339
-308
lines changed

15 files changed

+339
-308
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ loom {
120120
getByName("client") {
121121
name("Minecraft Client")
122122
source(testmod)
123-
property("fabric-api.gametest")
124123
}
125124
register("gametest") {
126125
name("GameTest Server")
127126
server()
128127
source(testmod)
129128
property("fabric-api.gametest")
129+
property("fabric-api.gametest.report-file", "${project.layout.buildDirectory.get()}/junit.xml")
130130
}
131131
}
132132
}

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx1G
33
# Mod Information
44
mod.id=machinelib
55
mod.name=MachineLib
6-
mod.version=0.4.1
6+
mod.version=0.4.2
77

88
# Minecraft and Fabric Loader
99
minecraft.version=1.20.4

src/main/java/dev/galacticraft/machinelib/api/block/entity/RecipeMachineBlockEntity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ protected RecipeMachineBlockEntity(@NotNull MachineType<? extends RecipeMachineB
165165
assert recipe != null;
166166
profiler.push("working");
167167
this.extractResourcesToWork();
168-
if (++this.progress > this.getProcessingTime(recipe)) {
168+
if (++this.progress >= this.getProcessingTime(recipe)) {
169169
profiler.push("crafting");
170170
this.craft(profiler, recipe);
171171
profiler.pop();

src/main/java/dev/galacticraft/machinelib/api/gametest/GameUnitTest.java

-112
This file was deleted.

src/main/java/dev/galacticraft/machinelib/api/gametest/MachineGameTest.java

+30-105
Original file line numberDiff line numberDiff line change
@@ -22,178 +22,103 @@
2222

2323
package dev.galacticraft.machinelib.api.gametest;
2424

25-
import com.google.common.base.CaseFormat;
2625
import dev.galacticraft.machinelib.api.block.entity.MachineBlockEntity;
27-
import dev.galacticraft.machinelib.api.gametest.annotation.InstantTest;
28-
import dev.galacticraft.machinelib.api.gametest.annotation.ProcessingTest;
29-
import dev.galacticraft.machinelib.api.gametest.annotation.container.DefaultedMetadata;
26+
import dev.galacticraft.machinelib.api.gametest.annotation.BasicTest;
27+
import dev.galacticraft.machinelib.api.gametest.annotation.MachineTest;
3028
import dev.galacticraft.machinelib.api.machine.MachineType;
31-
import net.fabricmc.fabric.api.gametest.v1.FabricGameTest;
3229
import net.minecraft.core.BlockPos;
33-
import net.minecraft.core.registries.BuiltInRegistries;
34-
import net.minecraft.gametest.framework.GameTest;
3530
import net.minecraft.gametest.framework.GameTestAssertException;
31+
import net.minecraft.gametest.framework.GameTestGenerator;
3632
import net.minecraft.gametest.framework.GameTestHelper;
3733
import net.minecraft.gametest.framework.TestFunction;
3834
import net.minecraft.world.item.Item;
39-
import net.minecraft.world.level.block.Rotation;
4035
import org.jetbrains.annotations.MustBeInvokedByOverriders;
4136
import org.jetbrains.annotations.NotNull;
4237

43-
import java.lang.reflect.InvocationTargetException;
4438
import java.lang.reflect.Method;
45-
import java.util.ArrayList;
4639
import java.util.List;
47-
import java.util.function.Function;
4840

4941
/**
5042
* A class for testing machines.
5143
*
5244
* @param <Machine> the type of machine block entity
53-
* @see InstantTest
54-
* @see ProcessingTest
45+
* @see BasicTest
46+
* @see MachineTest
5547
*/
56-
public abstract class MachineGameTest<Machine extends MachineBlockEntity> implements FabricGameTest {
57-
private static final Function<String, String> NAME_CONVERSION = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE);
48+
public abstract class MachineGameTest<Machine extends MachineBlockEntity> extends SimpleGameTest {
49+
public static final BlockPos MACHINE_POS = new BlockPos(1, 2, 1);
5850

5951
private final MachineType<Machine, ?> type;
6052

6153
protected MachineGameTest(MachineType<Machine, ?> type) {
6254
this.type = type;
6355
}
6456

65-
@Override
66-
public void invokeTestMethod(GameTestHelper context, Method method) {
67-
if (method.getAnnotation(GameTest.class) != null) FabricGameTest.super.invokeTestMethod(context, method);
68-
}
69-
57+
@GameTestGenerator
7058
@MustBeInvokedByOverriders
71-
public @NotNull List<TestFunction> generateTests() {
72-
List<TestFunction> tests = new ArrayList<>();
59+
public @NotNull List<TestFunction> registerTests() {
60+
List<TestFunction> tests = super.registerTests();
7361

74-
tests.add(new TestFunction(this.getBaseId(), this.getBaseId() + "/create_machine", EMPTY_STRUCTURE, Rotation.NONE, 1, 1, true, 1, 1, helper -> {
62+
tests.add(this.createTest("place", STRUCTURE_3x3, 1, 1, helper -> {
7563
if (this.createMachine(helper) == null) {
7664
throw new GameTestAssertException("No machine associated with block!");
7765
}
7866
helper.succeed();
7967
}));
8068

81-
Class<? extends MachineGameTest<Machine>> clazz = (Class<? extends MachineGameTest<Machine>>) this.getClass();
82-
DefaultedMetadata meta = clazz.getAnnotation(DefaultedMetadata.class);
83-
String structure = meta != null ? meta.structure() : EMPTY_STRUCTURE;
84-
for (Method method : clazz.getMethods()) {
85-
ProcessingTest processingTest = method.getAnnotation(ProcessingTest.class);
86-
if (processingTest != null) {
87-
String subId = "";
88-
if (!processingTest.group().isBlank()) {
89-
subId = "/" + processingTest.group();
90-
} else {
91-
if (meta != null && !meta.group().isBlank()) {
92-
subId = "/" + meta.group();
93-
}
94-
}
95-
tests.add(new TestFunction(this.getBaseId() + subId, this.getBaseId() + '/' + NAME_CONVERSION.apply(method.getName()), structure, Rotation.NONE, processingTest.workTime() + 1, 1, true, 1, 1, helper -> {
69+
for (Method method : this.getClass().getMethods()) {
70+
MachineTest machineTest = method.getAnnotation(MachineTest.class);
71+
if (machineTest != null) {
72+
tests.add(this.createTest(machineTest.batch(), machineTest.group(), method.getName(), machineTest.structure(), machineTest.workTime(), machineTest.setupTime(), helper -> {
9673
Machine machine = this.createMachine(helper);
97-
if (processingTest.requiresEnergy()) machine.energyStorage().setEnergy(machine.energyStorage().getCapacity());
9874

99-
try {
100-
Runnable runnable = (Runnable) method.invoke(this, machine);
101-
helper.runAfterDelay(processingTest.workTime() + 1, () -> {
75+
Runnable runnable = this.invokeTestMethod(method, machine, helper);
76+
if (runnable != null) {
77+
helper.runAfterDelay(machineTest.workTime(), () -> {
10278
runnable.run();
10379
helper.succeed();
10480
});
105-
} catch (IllegalAccessException e) {
106-
throw new RuntimeException("Failed to invoke test method!", e);
107-
} catch (InvocationTargetException e) {
108-
handleException(e);
10981
}
11082
}));
111-
} else {
112-
InstantTest instantTest = method.getAnnotation(InstantTest.class);
113-
if (instantTest != null) {
114-
String subId = "";
115-
if (!instantTest.group().isBlank()) {
116-
subId = "/" + instantTest.group();
117-
} else {
118-
if (meta != null && !meta.group().isBlank()) {
119-
subId = "/" + meta.group();
120-
}
121-
}
122-
tests.add(new TestFunction(this.getBaseId() + subId, this.getBaseId() + '/' + NAME_CONVERSION.apply(method.getName()), EMPTY_STRUCTURE, Rotation.NONE, 1, 1, true, 1, 1, helper -> {
123-
Machine machine = this.createMachine(helper);
124-
if (instantTest.requiresEnergy())
125-
machine.energyStorage().setEnergy(machine.energyStorage().getCapacity());
126-
try {
127-
Runnable runnable = (Runnable) method.invoke(this, machine);
128-
if (runnable == null) {
129-
helper.succeed();
130-
return;
131-
}
132-
133-
helper.runAfterDelay(1, () -> {
134-
runnable.run();
135-
helper.succeed();
136-
});
137-
} catch (IllegalAccessException e) {
138-
throw new RuntimeException("Failed to invoke test method!", e);
139-
} catch (InvocationTargetException e) {
140-
handleException(e);
141-
}
142-
}));
143-
}
14483
}
14584
}
14685

14786
return tests;
14887
}
14988

150-
public @NotNull TestFunction createChargeFromEnergyItemTest(int slot, Item infiniteBattery) {
151-
return new TestFunction(this.getBaseId(), this.getBaseId() + "/charge_from_item", EMPTY_STRUCTURE, Rotation.NONE, 2, 1, true, 1, 1, helper -> {
89+
public TestFunction createChargeFromEnergyItemTest(int slot, Item infiniteBattery) {
90+
return this.createTest("chargeFromItem", STRUCTURE_3x3, 2, 1, helper -> {
15291
Machine machine = this.createMachine(helper);
15392
machine.itemStorage().getSlot(slot).set(infiniteBattery, 1);
15493
helper.runAfterDelay(1, () -> {
15594
if (machine.energyStorage().isEmpty()) {
156-
helper.fail("Machine did not charge from the stack!", BlockPos.ZERO);
95+
helper.fail("Machine did not charge from the stack!", machine.getBlockPos());
96+
} else {
97+
helper.succeed();
15798
}
158-
helper.succeed();
15999
});
160100
});
161101
}
162102

163-
public @NotNull TestFunction createDrainToEnergyItemTest(int slot, Item battery) {
164-
return new TestFunction(this.getBaseId(), this.getBaseId() + "/drain_to_item", EMPTY_STRUCTURE, Rotation.NONE, 2, 1, true, 1, 1, helper -> {
103+
public TestFunction createDrainToEnergyItemTest(int slot, Item battery) {
104+
return this.createTest("drainToItem", STRUCTURE_3x3, 2, 1, helper -> {
165105
Machine machine = this.createMachine(helper);
166-
machine.energyStorage().setEnergy(machine.energyStorage().getCapacity());
167106

107+
machine.energyStorage().setEnergy(machine.energyStorage().getCapacity());
168108
machine.itemStorage().getSlot(slot).set(battery, 1);
169109

170110
helper.runAfterDelay(1, () -> {
171111
if (machine.energyStorage().isFull()) {
172112
helper.fail("Machine did not drain energy to the stack!", BlockPos.ZERO);
113+
} else {
114+
helper.succeed();
173115
}
174-
helper.succeed();
175116
});
176117
});
177118
}
178119

179-
public static void handleException(@NotNull InvocationTargetException e) {
180-
if (e.getCause() instanceof GameTestAssertException ex) {
181-
throw ex;
182-
} else if (e.getCause() instanceof RuntimeException ex){
183-
throw ex;
184-
} else if (e.getCause() != null) {
185-
throw new RuntimeException(e.getCause());
186-
} else {
187-
throw new RuntimeException(e);
188-
}
189-
}
190-
191-
public @NotNull String getBaseId() {
192-
return BuiltInRegistries.BLOCK.getKey(this.type.getBlock()).toString().replace(":", ".");
193-
}
194-
195120
protected Machine createMachine(GameTestHelper helper) {
196-
helper.setBlock(BlockPos.ZERO, this.type.getBlock());
197-
return (Machine) helper.getBlockEntity(BlockPos.ZERO);
121+
helper.setBlock(MACHINE_POS, this.type.getBlock());
122+
return (Machine) helper.getBlockEntity(MACHINE_POS);
198123
}
199124
}

0 commit comments

Comments
 (0)