Skip to content

Commit

Permalink
Merge pull request #747 from starforcraft/feat/GH-248/controllertests
Browse files Browse the repository at this point in the history
Controller gametest
  • Loading branch information
raoulvdberge authored Dec 12, 2024
2 parents 102ae60 + 23666ec commit acfc501
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
});
}
}
Original file line number Diff line number Diff line change
@@ -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<ControllerBlockEntity, BlockPos, GameTestSequence> 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()
);
}
}

0 comments on commit acfc501

Please sign in to comment.