Add a basic gametest to test the grinder.

This commit is contained in:
modmuss50 2021-09-01 23:08:42 +01:00
parent 6c3b461e11
commit 8c6a0155e7
5 changed files with 250 additions and 0 deletions

View file

@ -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'

View file

@ -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))
}
}

View file

@ -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
}
}
}

View file

@ -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)
}
}
}

View file

@ -0,0 +1,12 @@
{
"schemaVersion": 1,
"id": "techreborn-gametest",
"name": "TechReborn Gametest",
"version": "1.0.0",
"environment": "*",
"entrypoints": {
"fabric-gametest" : [
"techreborn.test.machine.GrinderTest"
]
}
}