Skip to content

Commit

Permalink
Enable game tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Jul 25, 2024
1 parent 2ea3b97 commit 3bbf0ee
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ jobs:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
if: ${{ env.COVERALLS_REPO_TOKEN }}
run: ./gradlew test jacocoTestReport coveralls
- name: 'Game Test'
env:
GAME_TEST: ${{ secrets.GAME_TEST }}
if: ${{ env.GAME_TEST }}
run: ./gradlew runGameTestServer
- name: 'Deploy as GitHub CI artifacts'
uses: actions/upload-artifact@v4
with:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.cyclops.integrateddynamics.core.evaluate.variable.integration;

import net.minecraft.gametest.framework.GameTestAssertException;
import net.minecraft.gametest.framework.GameTestGenerator;
import net.minecraft.gametest.framework.TestFunction;
import net.neoforged.neoforge.gametest.GameTestHolder;
import org.apache.commons.compress.utils.Lists;
import org.cyclops.integrateddynamics.Reference;
import org.cyclops.integrateddynamics.command.CommandTest;
import org.cyclops.integrateddynamics.core.test.IntegrationBefore;
import org.cyclops.integrateddynamics.core.test.IntegrationTest;

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

/**
* @author rubensworks
*/
@GameTestHolder(Reference.MOD_ID)
public class GameTester {

@GameTestGenerator
public Collection<TestFunction> integrationTests() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
List<TestFunction> testsList = Lists.newArrayList();

for(String className : CommandTest.CLASSES) {
Class<?> clazz = Class.forName(className);
Object testInstance = clazz.newInstance();

// Collect test methods
List<Method> befores = com.google.common.collect.Lists.newLinkedList();
List<Method> tests = com.google.common.collect.Lists.newLinkedList();
for(Method method : clazz.getDeclaredMethods()) {
if(method.isAnnotationPresent(IntegrationBefore.class)) {
befores.add(method);
}
if(method.isAnnotationPresent(IntegrationTest.class)) {
tests.add(method);
}
}

// Run tests
for(Method test : tests) {
String testName = className.replace(CommandTest.P, "") + "#" + test.getName();

testsList.add(new TestFunction(
"defaultBatch",
testName,
"integrateddynamics:test",
1,
1,
true,
(gameTestHelpers) -> {
gameTestHelpers.succeedIf(() -> {
try {
for(Method before : befores) {
before.invoke(testInstance);
}
test.invoke(testInstance);
} catch (InvocationTargetException e) {
Class<?> excepted = test.getAnnotation(IntegrationTest.class).expected();
if(!excepted.isInstance(e.getTargetException())) {
if (e.getTargetException() instanceof IllegalStateException || e.getTargetException() instanceof AssertionError) {
e.getTargetException().printStackTrace();
throw new GameTestAssertException("Test " + testName + " failed!");
} else {
e.getTargetException().printStackTrace();
throw new GameTestAssertException(String.format("Expected at %s exception %s, but found:", testName, e));
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new GameTestAssertException(e.getMessage());
}
});
}
));
}
}

return testsList;
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.cyclops.integrateddynamics.core.evaluate.variable.integration;

import com.mojang.authlib.GameProfile;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ClientInformation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EquipmentSlot;
Expand All @@ -18,6 +21,7 @@
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import org.cyclops.cyclopscore.helper.MinecraftHelpers;
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
Expand All @@ -34,6 +38,8 @@
import org.cyclops.integrateddynamics.core.test.IntegrationTest;
import org.cyclops.integrateddynamics.core.test.TestHelpers;

import java.util.UUID;

/**
* Test the different logical operators.
* @author rubensworks
Expand Down Expand Up @@ -105,7 +111,16 @@ public int getUseItemRemainingTicks() {
eChicken = new DummyVariableEntity(makeEntity(new Chicken(EntityType.CHICKEN, world)));
eItem = new DummyVariableEntity(makeEntity(new ItemEntity(world, 0, 0, 0, ItemStack.EMPTY)));
eItemFrame = new DummyVariableEntity(makeEntity(new ItemFrame(world, new BlockPos(0, 0, 0), Direction.NORTH)));
ePlayer = new DummyVariableEntity(makeEntity(world.players().get(0)));
if (MinecraftHelpers.isClientSide()) {
ePlayer = new DummyVariableEntity(makeEntity(world.players().get(0)));
} else {
ePlayer = new DummyVariableEntity(makeEntity(new ServerPlayer(
ServerLifecycleHooks.getCurrentServer(),
ServerLifecycleHooks.getCurrentServer().overworld(),
new GameProfile(UUID.randomUUID(), "test"),
ClientInformation.createDefault()
)));
}
Zombie zombieHeldItems = new Zombie(world);
zombieHeldItems.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.APPLE));
zombieHeldItems.setItemSlot(EquipmentSlot.OFFHAND, new ItemStack(Items.POTATO));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public void testIngredientsType() {
itemStacks.add(serializeStack(ItemStack.EMPTY));
tag.put("minecraft:itemstack", itemStacks);

TestHelpers.assertEqual(i0.getType().serialize(ValueDeseralizationContext.ofClient(), i0.getValue()), tag, "Serialization is correct");
TestHelpers.assertEqual(i0.getType().deserialize(ValueDeseralizationContext.ofClient(), tag), i0.getValue(), "Deserialization is correct");
TestHelpers.assertEqual(i0.getType().serialize(ValueDeseralizationContext.ofAllEnabled(), i0.getValue()), tag, "Serialization is correct");
TestHelpers.assertEqual(i0.getType().deserialize(ValueDeseralizationContext.ofAllEnabled(), tag), i0.getValue(), "Deserialization is correct");
}

@IntegrationTest
Expand Down Expand Up @@ -158,8 +158,8 @@ public void testRecipeType() {
tag.put("input", input);
tag.put("inputReusable", inputReusable);

TestHelpers.assertEqual(r0.getType().serialize(ValueDeseralizationContext.ofClient(), r0.getValue()), tag, "Serialization is correct");
TestHelpers.assertEqual(r0.getType().deserialize(ValueDeseralizationContext.ofClient(), tag), r0.getValue(), "Deserialization is correct");
TestHelpers.assertEqual(r0.getType().serialize(ValueDeseralizationContext.ofAllEnabled(), r0.getValue()), tag, "Serialization is correct");
TestHelpers.assertEqual(r0.getType().deserialize(ValueDeseralizationContext.ofAllEnabled(), tag), r0.getValue(), "Deserialization is correct");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
public class CommandTest implements Command<CommandSourceStack> {

private static final String P = "org.cyclops.integrateddynamics.core.evaluate.variable.integration.";
public static final String P = "org.cyclops.integrateddynamics.core.evaluate.variable.integration.";
public static final List<String> CLASSES = ImmutableList.of(
P + "TestVariables",
P + "TestBlockOperators",
Expand Down
Binary file not shown.

0 comments on commit 3bbf0ee

Please sign in to comment.