From 8c6a0155e7f7a437c459cd507062866cbac2c3de Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Wed, 1 Sep 2021 23:08:42 +0100 Subject: [PATCH] Add a basic gametest to test the grinder. --- build.gradle | 35 ++++++ .../groovy/techreborn/test/TRGameTest.groovy | 42 +++++++ .../techreborn/test/TRTestContext.groovy | 113 ++++++++++++++++++ .../test/machine/GrinderTest.groovy | 48 ++++++++ src/gametest/resources/fabric.mod.json | 12 ++ 5 files changed, 250 insertions(+) create mode 100644 src/gametest/groovy/techreborn/test/TRGameTest.groovy create mode 100644 src/gametest/groovy/techreborn/test/TRTestContext.groovy create mode 100644 src/gametest/groovy/techreborn/test/machine/GrinderTest.groovy create mode 100644 src/gametest/resources/fabric.mod.json diff --git a/build.gradle b/build.gradle index 5467c6465..07504a0d4 100644 --- a/build.gradle +++ b/build.gradle @@ -6,6 +6,7 @@ buildscript { plugins { id 'java' + id 'groovy' id 'java-library' id 'idea' id 'eclipse' @@ -72,6 +73,7 @@ def ENV = System.getenv() license { header file('HEADER') include '**/*.java' + include '**/*.groovy' ignoreFailures = true //Stops the build from failing if a file does not have a license header } @@ -149,6 +151,15 @@ allprojects { } } +sourceSets { + gametest { + compileClasspath += sourceSets.main.compileClasspath + runtimeClasspath += sourceSets.main.runtimeClasspath + compileClasspath += sourceSets.main.output + runtimeClasspath += sourceSets.main.output + } +} + // TechReborn specific dependencies dependencies { api project(":RebornCore") @@ -158,6 +169,8 @@ dependencies { disabledOptionalDependency "com.github.emilyploszaj:trinkets:${project.trinkets_version}" disabledOptionalDependency "com.github.dexman545:autoswitch-api:${project.autoswitch_version}" disabledOptionalDependency "net.oskarstrom:DashLoader:${project.dashloader_version}" + + gametestImplementation 'org.codehaus.groovy:groovy-all:3.0.8' } def optionalDependency(String dep) { @@ -178,6 +191,28 @@ def disabledOptionalDependency(String dep) { } } +loom { + runs { + // Use to run the tests + gametest { + server() + name "Game Test" + vmArg "-Dfabric-api.gametest" + vmArg "-Dfabric-api.gametest.report-file=${project.buildDir}/junit.xml" + runDir "build/gametest" + source sourceSets.gametest + } + + // Use to debug tests + gametestClient { + client() + name "Game Test Client" + source sourceSets.gametest + } + } +} +test.dependsOn runGametest + jar { exclude "**/*.psd" classifier = 'universal' diff --git a/src/gametest/groovy/techreborn/test/TRGameTest.groovy b/src/gametest/groovy/techreborn/test/TRGameTest.groovy new file mode 100644 index 000000000..93086b31a --- /dev/null +++ b/src/gametest/groovy/techreborn/test/TRGameTest.groovy @@ -0,0 +1,42 @@ +/* + * This file is part of TechReborn, licensed under the MIT License (MIT). + * + * Copyright (c) 2020 TechReborn + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package techreborn.test + +import net.fabricmc.fabric.api.gametest.v1.FabricGameTest +import net.minecraft.test.TestContext + +import java.lang.reflect.Method + +/** + * Base class that all TR game tests should extend from. + * + * All test methods should accept 1 argument of TRTestContext + */ +abstract class TRGameTest implements FabricGameTest { + @Override + void invokeTestMethod(TestContext context, Method method) { + method.invoke(this, new TRTestContext(context)) + } +} diff --git a/src/gametest/groovy/techreborn/test/TRTestContext.groovy b/src/gametest/groovy/techreborn/test/TRTestContext.groovy new file mode 100644 index 000000000..3d298a1ac --- /dev/null +++ b/src/gametest/groovy/techreborn/test/TRTestContext.groovy @@ -0,0 +1,113 @@ +/* + * This file is part of TechReborn, licensed under the MIT License (MIT). + * + * Copyright (c) 2020 TechReborn + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package techreborn.test + +import net.minecraft.item.ItemConvertible +import net.minecraft.item.ItemStack +import net.minecraft.test.TestContext +import net.minecraft.util.math.BlockPos +import techreborn.blockentity.machine.GenericMachineBlockEntity +import techreborn.init.TRContent + +class TRTestContext extends TestContext { + TRTestContext(TestContext parentContext) { + //noinspection GroovyAccessibility + super(parentContext.test) + } + + /** + * Place a machine with a creative solar panel + */ + def poweredMachine(TRContent.Machine machine, @DelegatesTo(MachineContext) Closure machineContextClosure) { + def machinePos = new BlockPos(0, 2, 0) + setBlockState(machinePos, machine.block) + setBlockState(machinePos.down(), TRContent.SolarPanels.CREATIVE.block) + + waitAndRun(5) { + try { + new MachineContext(machinePos).with(machineContextClosure) + } catch (e) { + e.printStackTrace() + throw e + } + } + } + + class MachineContext { + final BlockPos machinePos + + MachineContext(BlockPos machinePos) { + this.machinePos = machinePos + } + + def input(ItemConvertible item, int slot = -1) { + this.input(new ItemStack(item), slot) + } + + def input(ItemStack stack, int slot = -1) { + if (slot == -1) { + // If not slot is provided use the first input slot + slot = blockEntity.crafter.inputSlots[0] + } + + blockEntity.inventory.setStack(slot, stack) + } + + def expectOutput(ItemConvertible item, int ticks, int slot = -1) { + expectOutput(new ItemStack(item), ticks, slot) + } + + def expectOutput(ItemStack stack, int ticks, int slot = -1) { + if (slot == -1) { + // If not slot is provided use the first output slot + slot = blockEntity.crafter.outputSlots[0] + } + + addFinalTaskWithDuration(ticks) { + if (!blockEntity.inventory.getStack(slot).isItemEqual(stack)) { + throwGameTestException("Failed to find $stack in slot $slot") + } + } + } + + def withUpgrades(TRContent.Upgrades upgrade, int count = -1) { + count = (count != -1 ? count : blockEntity.getUpgradeSlotCount()) -1 + + (0..count).each { + blockEntity.upgradeInventory.setStack(it, new ItemStack(upgrade)) + } + } + + GenericMachineBlockEntity getBlockEntity() { + def be = getBlockEntity(machinePos) + + if (!be) { + throwPositionedException("Failed to get machine block entity", machinePos) + } + + return be as GenericMachineBlockEntity + } + } +} diff --git a/src/gametest/groovy/techreborn/test/machine/GrinderTest.groovy b/src/gametest/groovy/techreborn/test/machine/GrinderTest.groovy new file mode 100644 index 000000000..83b4fe9c5 --- /dev/null +++ b/src/gametest/groovy/techreborn/test/machine/GrinderTest.groovy @@ -0,0 +1,48 @@ +/* + * This file is part of TechReborn, licensed under the MIT License (MIT). + * + * Copyright (c) 2020 TechReborn + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +//file:noinspection GrMethodMayBeStatic +package techreborn.test.machine + +import net.minecraft.item.Items +import net.minecraft.test.GameTest +import techreborn.init.TRContent +import techreborn.test.TRGameTest +import techreborn.test.TRTestContext + +class GrinderTest extends TRGameTest { + @GameTest(structureName = EMPTY_STRUCTURE, tickLimit = 150) + def testGrind2OCs(TRTestContext context) { + /** + * Test that grinder with 2 overclocker upgrades grinds coal into coal dust in 116 ticks + */ + context.poweredMachine(TRContent.Machine.GRINDER) { + input(Items.COAL) + + withUpgrades(TRContent.Upgrades.OVERCLOCKER, 2) + + expectOutput(TRContent.Dusts.COAL, 116) + } + } +} diff --git a/src/gametest/resources/fabric.mod.json b/src/gametest/resources/fabric.mod.json new file mode 100644 index 000000000..36a58fcc4 --- /dev/null +++ b/src/gametest/resources/fabric.mod.json @@ -0,0 +1,12 @@ +{ + "schemaVersion": 1, + "id": "techreborn-gametest", + "name": "TechReborn Gametest", + "version": "1.0.0", + "environment": "*", + "entrypoints": { + "fabric-gametest" : [ + "techreborn.test.machine.GrinderTest" + ] + } +} \ No newline at end of file