Skip to content

Commit

Permalink
refactor: constructor fluid place test
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed May 26, 2024
1 parent f0ec6ca commit 6e43ee1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Additionally, tests in the `refinedstorage2-network` module use the `refinedstor
To test the entire chain from Minecraft to the API modules, we use integration tests. These tests are located in the
test source set of the `refinedstorage2-platform-forge` module.

We write these integration tests as Minecraft gametests using the NeoForge testing framework.
We write these integration tests as Minecraft gametests.

### Test coverage

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.refinedmods.refinedstorage2.platform.forge;

import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.constructordestructor.ConstructorBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.storage.FluidStorageType;
import com.refinedmods.refinedstorage2.platform.common.storage.ItemStorageType;
import com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil;

Expand All @@ -14,14 +16,18 @@
import net.minecraft.gametest.framework.GameTestSequence;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.neoforged.neoforge.gametest.GameTestHolder;
import net.neoforged.neoforge.gametest.PrefixGameTestTemplate;
import org.apache.commons.lang3.function.TriConsumer;

import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.DIRT;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.RSBLOCKS;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.STONE;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.insertItem;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.WATER;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.assertFluidPresent;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.insert;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.networkIsAvailable;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.requireBlockEntity;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.storageContainsExactly;
Expand All @@ -40,6 +46,10 @@ private static void prepareConstructorPlot(final GameTestHelper helper,
consumer) {
helper.setBlock(ZERO.above(), RSBLOCKS.getCreativeController().getDefault());
helper.setBlock(ZERO.above().above(), RSBLOCKS.getItemStorageBlock(ItemStorageType.Variant.ONE_K));
helper.setBlock(
ZERO.above().above().north(),
RSBLOCKS.getFluidStorageBlock(FluidStorageType.Variant.SIXTY_FOUR_B)
);
final BlockPos constructorPos = ZERO.above().above().above();
helper.setBlock(constructorPos, RSBLOCKS.getConstructor().getDefault().rotated(direction));
consumer.accept(
Expand All @@ -54,8 +64,8 @@ public static void shouldPlaceBlock(final GameTestHelper helper) {
prepareConstructorPlot(helper, Direction.EAST, (constructor, pos, sequence) -> {
// Arrange
sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insertItem(helper, network, DIRT, 10);
insertItem(helper, network, STONE, 15);
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
}));

// Act
Expand All @@ -74,13 +84,40 @@ public static void shouldPlaceBlock(final GameTestHelper helper) {
});
}

@GameTest(template = "empty_15x15")
public static void shouldPlaceWater(final GameTestHelper helper) {
prepareConstructorPlot(helper, Direction.EAST, (constructor, pos, sequence) -> {
// Arrange
sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
insert(helper, network, WATER, Platform.INSTANCE.getBucketAmount() * 2);
}));

// Act
constructor.setFilters(List.of(WATER));

// Assert
sequence
.thenWaitUntil(() -> assertFluidPresent(helper, pos.east(), Fluids.WATER, FluidState.AMOUNT_FULL))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(DIRT, 10),
new ResourceAmount(STONE, 15),
new ResourceAmount(WATER, Platform.INSTANCE.getBucketAmount())
))
.thenSucceed();
});
}

@GameTest(template = "empty_15x15")
public static void shouldDropItem(final GameTestHelper helper) {
prepareConstructorPlot(helper, Direction.EAST, (constructor, pos, sequence) -> {
// Arrange
sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insertItem(helper, network, DIRT, 10);
insertItem(helper, network, STONE, 15);
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
}));

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.refinedmods.refinedstorage2.api.network.node.NetworkNode;
import com.refinedmods.refinedstorage2.api.network.storage.StorageNetworkComponent;
import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.storage.EmptyActor;
import com.refinedmods.refinedstorage2.platform.api.support.network.AbstractNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.content.Blocks;
import com.refinedmods.refinedstorage2.platform.common.support.resource.FluidResource;
import com.refinedmods.refinedstorage2.platform.common.support.resource.ItemResource;

import java.util.Arrays;
Expand All @@ -20,10 +22,14 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;

public final class GameTestUtil {
public static final ItemResource DIRT = ItemResource.ofItemStack(new ItemStack(Items.DIRT));
public static final ItemResource STONE = ItemResource.ofItemStack(new ItemStack(Items.STONE));
public static final FluidResource WATER = new FluidResource(Fluids.WATER, null);
public static final Blocks RSBLOCKS = Blocks.INSTANCE;

private GameTestUtil() {
Expand Down Expand Up @@ -52,13 +58,13 @@ public static Runnable networkIsAvailable(final GameTestHelper helper,
};
}

public static void insertItem(final GameTestHelper helper,
final Network network,
final ItemResource resource,
final long amount) {
public static void insert(final GameTestHelper helper,
final Network network,
final ResourceKey resource,
final long amount) {
final StorageNetworkComponent storage = network.getComponent(StorageNetworkComponent.class);
final long inserted = storage.insert(resource, amount, Action.EXECUTE, EmptyActor.INSTANCE);
helper.assertTrue(inserted == amount, "Item couldn't be inserted");
helper.assertTrue(inserted == amount, "Resource couldn't be inserted");
}

@SuppressWarnings("unchecked")
Expand All @@ -79,6 +85,17 @@ public static <T extends BlockEntity> T requireBlockEntity(
return (T) blockEntity;
}

public static void assertFluidPresent(final GameTestHelper helper,
final BlockPos pos,
final Fluid fluid,
final int level) {
final FluidState fluidState = helper.getLevel().getFluidState(helper.absolutePos(pos));
helper.assertTrue(
fluidState.getType() == fluid && fluidState.getAmount() == level,
"Unexpected " + fluidState.getType() + ", " + fluidState.getAmount()
);
}

public static Runnable storageContainsExactly(final GameTestHelper helper,
final BlockPos networkPos,
final ResourceAmount... expected) {
Expand Down

0 comments on commit 6e43ee1

Please sign in to comment.