Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More constructor tests #531

Merged
merged 4 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -57,7 +57,7 @@ public ConstructorBlockEntity(final BlockPos pos, final BlockState state) {
}

@Override
public void setFilters(final List<ResourceKey> filters) {
protected void setFilters(final List<ResourceKey> filters) {
this.tasks.clear();
this.tasks.addAll(filters.stream().map(TaskImpl::new).toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected void setTaskExecutor(final TaskExecutor<ExporterNetworkNode.TaskContex
}

@Override
public void setFilters(final List<ResourceKey> filters) {
protected void setFilters(final List<ResourceKey> filters) {
mainNode.setFilters(filters);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByte

protected abstract void setTaskExecutor(TaskExecutor<C> taskExecutor);

public abstract void setFilters(List<ResourceKey> filters);
protected abstract void setFilters(List<ResourceKey> filters);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package com.refinedmods.refinedstorage2.platform.common.constructordestructor;

import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil;

import java.util.List;

import net.minecraft.core.Direction;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.gametest.framework.GameTestHelper;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.material.FluidState;
import net.neoforged.neoforge.gametest.GameTestHolder;
import net.neoforged.neoforge.gametest.PrefixGameTestTemplate;

import static com.refinedmods.refinedstorage2.platform.common.constructordestructor.ConstructorTestPlots.preparePlot;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.asResource;
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.storageContainsExactly;
import static net.minecraft.world.item.Items.DIRT;
import static net.minecraft.world.item.Items.FIREWORK_ROCKET;
import static net.minecraft.world.item.Items.STONE;
import static net.minecraft.world.level.material.Fluids.WATER;

@GameTestHolder(IdentifierUtil.MOD_ID)
@PrefixGameTestTemplate(false)
public final class ConstructorTest {
private ConstructorTest() {
}

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

// Act
constructor.setFilters(List.of(asResource(DIRT)));

// Assert
sequence
.thenWaitUntil(() -> helper.assertBlockPresent(Blocks.DIRT, pos.east()))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 9),
new ResourceAmount(asResource(STONE), 15)
))
.thenSucceed();
});
}

@GameTest(template = "empty_15x15")
public static void shouldPlaceWater(final GameTestHelper helper) {
preparePlot(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(asResource(WATER)));

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

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

// Act
constructor.setDropItems(true);
constructor.setFilters(List.of(asResource(DIRT)));

// Assert
sequence
.thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.DIRT, pos.east()))
.thenWaitUntil(() -> helper.assertItemEntityPresent(DIRT, pos.east(), 1))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 9),
new ResourceAmount(asResource(STONE), 15)
))
.thenSucceed();
});
}

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

// Act
constructor.setFilters(List.of(asResource(FIREWORK_ROCKET)));

// Assert
sequence
.thenWaitUntil(() -> helper.assertBlockPresent(Blocks.AIR, pos.east()))
.thenWaitUntil(() -> helper.assertEntityPresent(EntityType.FIREWORK_ROCKET, pos.east()))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 10),
new ResourceAmount(asResource(FIREWORK_ROCKET), 14)
))
.thenSucceed();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.refinedmods.refinedstorage2.platform.common.constructordestructor;

import com.refinedmods.refinedstorage2.platform.common.storage.FluidStorageType;
import com.refinedmods.refinedstorage2.platform.common.storage.ItemStorageType;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.gametest.framework.GameTestHelper;
import net.minecraft.gametest.framework.GameTestSequence;
import org.apache.commons.lang3.function.TriConsumer;

import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.RSBLOCKS;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.requireBlockEntity;
import static net.minecraft.core.BlockPos.ZERO;

final class ConstructorTestPlots {
private ConstructorTestPlots() {
}

static void preparePlot(final GameTestHelper helper,
final Direction direction,
final TriConsumer<ConstructorBlockEntity, BlockPos, GameTestSequence> 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(
requireBlockEntity(helper, constructorPos, ConstructorBlockEntity.class),
constructorPos,
helper.startSequence()
);
}
}

This file was deleted.

Loading
Loading