From 23666ec58f21182f603e83f89d0bb13faa5bc11c Mon Sep 17 00:00:00 2001 From: Starforcraft Date: Tue, 10 Dec 2024 19:27:12 +0100 Subject: [PATCH] feat: controller gametest --- .../refinedstorage/common/GameTestUtil.java | 11 +++ .../common/controller/ControllerTest.java | 72 +++++++++++++++++++ .../controller/ControllerTestPlots.java | 29 ++++++++ 3 files changed, 112 insertions(+) create mode 100644 refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/controller/ControllerTest.java create mode 100644 refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/controller/ControllerTestPlots.java diff --git a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/GameTestUtil.java b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/GameTestUtil.java index ea15f2a27..de0a09336 100644 --- a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/GameTestUtil.java +++ b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/GameTestUtil.java @@ -2,6 +2,7 @@ import com.refinedmods.refinedstorage.api.core.Action; import com.refinedmods.refinedstorage.api.network.Network; +import com.refinedmods.refinedstorage.api.network.energy.EnergyStorage; import com.refinedmods.refinedstorage.api.network.node.NetworkNode; import com.refinedmods.refinedstorage.api.network.storage.StorageNetworkComponent; import com.refinedmods.refinedstorage.api.resource.ResourceAmount; @@ -228,6 +229,16 @@ private static Runnable assertResourceContainerEmpty(final Component displayName }; } + public static Runnable energyStoredExactly(final EnergyStorage storage, + final long energyAmount) { + return () -> { + if (storage.getStored() != energyAmount) { + throw new GameTestAssertException("Energy stored should be: " + energyAmount + + " but is " + storage.getStored()); + } + }; + } + public static Runnable interfaceContainsExactly(final GameTestHelper helper, final BlockPos pos, final ResourceAmount... expected) { diff --git a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/controller/ControllerTest.java b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/controller/ControllerTest.java new file mode 100644 index 000000000..2af8892d9 --- /dev/null +++ b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/controller/ControllerTest.java @@ -0,0 +1,72 @@ +package com.refinedmods.refinedstorage.common.controller; + +import com.refinedmods.refinedstorage.api.core.Action; +import com.refinedmods.refinedstorage.api.network.energy.EnergyStorage; +import com.refinedmods.refinedstorage.common.Platform; +import com.refinedmods.refinedstorage.common.util.IdentifierUtil; + +import net.minecraft.gametest.framework.GameTest; +import net.minecraft.gametest.framework.GameTestHelper; +import net.neoforged.neoforge.gametest.GameTestHolder; +import net.neoforged.neoforge.gametest.PrefixGameTestTemplate; + +import static com.refinedmods.refinedstorage.common.GameTestUtil.RSBLOCKS; +import static com.refinedmods.refinedstorage.common.GameTestUtil.energyStoredExactly; +import static com.refinedmods.refinedstorage.common.GameTestUtil.networkIsAvailable; + +@GameTestHolder(IdentifierUtil.MOD_ID) +@PrefixGameTestTemplate(false) +public final class ControllerTest { + private ControllerTest() { + } + + @GameTest(template = "empty_15x15") + public static void shouldConsumeEnergy(final GameTestHelper helper) { + ControllerTestPlots.preparePlot(helper, false, (controller, pos, sequence) -> { + // Arrange + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + })); + + // Act + final EnergyStorage energyStorage = controller.getEnergyStorage(); + energyStorage.receive(energyStorage.getCapacity(), Action.EXECUTE); + + // Assert + sequence + .thenIdle(20) + .thenExecute(energyStoredExactly(energyStorage, energyStorage.getCapacity())) + .thenWaitUntil(() -> helper.setBlock(pos.above(), RSBLOCKS.getGrid().getDefault())) + .thenIdle(1) + .thenExecute(energyStoredExactly( + energyStorage, + energyStorage.getCapacity() - Platform.INSTANCE.getConfig().getGrid().getEnergyUsage() + )) + .thenIdle(9) + .thenExecute(energyStoredExactly( + energyStorage, + energyStorage.getCapacity() - Platform.INSTANCE.getConfig().getGrid().getEnergyUsage() * 10 + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldNotConsumeEnergy(final GameTestHelper helper) { + ControllerTestPlots.preparePlot(helper, true, (controller, pos, sequence) -> { + // Arrange + final EnergyStorage energyStorage = controller.getEnergyStorage(); + + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + })); + + // Assert + sequence + .thenIdle(20) + .thenExecute(energyStoredExactly(energyStorage, energyStorage.getCapacity())) + .thenWaitUntil(() -> helper.setBlock(pos.above(), RSBLOCKS.getGrid().getDefault())) + .thenIdle(20) + .thenExecute(energyStoredExactly(energyStorage, energyStorage.getCapacity())) + .thenSucceed(); + }); + } +} diff --git a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/controller/ControllerTestPlots.java b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/controller/ControllerTestPlots.java new file mode 100644 index 000000000..f0de90e9e --- /dev/null +++ b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/controller/ControllerTestPlots.java @@ -0,0 +1,29 @@ +package com.refinedmods.refinedstorage.common.controller; + +import net.minecraft.core.BlockPos; +import net.minecraft.gametest.framework.GameTestHelper; +import net.minecraft.gametest.framework.GameTestSequence; +import org.apache.commons.lang3.function.TriConsumer; + +import static com.refinedmods.refinedstorage.common.GameTestUtil.RSBLOCKS; +import static com.refinedmods.refinedstorage.common.GameTestUtil.requireBlockEntity; +import static net.minecraft.core.BlockPos.ZERO; + +final class ControllerTestPlots { + private ControllerTestPlots() { + } + + static void preparePlot(final GameTestHelper helper, + final boolean isCreative, + final TriConsumer consumer) { + final BlockPos controllerPos = ZERO.above(); + helper.setBlock(controllerPos, isCreative + ? RSBLOCKS.getCreativeController().getDefault() + : RSBLOCKS.getController().getDefault()); + consumer.accept( + requireBlockEntity(helper, controllerPos, ControllerBlockEntity.class), + controllerPos, + helper.startSequence() + ); + } +}