Skip to content

Commit

Permalink
Merge pull request #533 from starforcraft/feat/GH-248/impexptests
Browse files Browse the repository at this point in the history
Importer and exporter gametests
  • Loading branch information
raoulvdberge authored May 28, 2024
2 parents ef15450 + e345cfa commit 51519e3
Show file tree
Hide file tree
Showing 6 changed files with 498 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.refinedmods.refinedstorage2.platform.common.upgrade.UpgradeDestinations;

import java.util.List;
import java.util.Set;
import java.util.function.LongSupplier;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -57,7 +58,7 @@ public ImporterBlockEntity(final BlockPos pos, final BlockState state) {
this.filter = FilterWithFuzzyMode.createAndListenForUniqueFilters(
ResourceContainerImpl.createForFilter(),
this::setChanged,
mainNode::setFilters
this::setFilters
);
this.mainNode.setNormalizer(filter.createNormalizer());
}
Expand Down Expand Up @@ -96,6 +97,10 @@ public void readConfiguration(final CompoundTag tag) {
filter.load(tag);
}

void setFilters(final Set<ResourceKey> filters) {
mainNode.setFilters(filters);
}

boolean isFuzzyMode() {
return filter.isFuzzyMode();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.refinedmods.refinedstorage2.platform.common.exporter;

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.item.ItemStack;
import net.minecraft.world.level.block.LayeredCauldronBlock;
import net.neoforged.neoforge.gametest.GameTestHolder;
import net.neoforged.neoforge.gametest.PrefixGameTestTemplate;

import static com.refinedmods.refinedstorage2.platform.common.exporter.ExporterTestPlots.preparePlot;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.asResource;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.containerContainsExactly;
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.DIAMOND_CHESTPLATE;
import static net.minecraft.world.item.Items.DIRT;
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 ExporterTest {
private ExporterTest() {
}

@GameTest(template = "empty_15x15")
public static void shouldExportItem(final GameTestHelper helper) {
preparePlot(helper, true, Direction.EAST, (exporter, pos, sequence) -> {
// Arrange
final ItemStack damagedDiamondChestplate = DIAMOND_CHESTPLATE.getDefaultInstance();
damagedDiamondChestplate.setDamageValue(500);

sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
insert(helper, network, asResource(damagedDiamondChestplate), 1);
}));

// Act
exporter.setFilters(List.of(asResource(DIRT), asResource(DIAMOND_CHESTPLATE.getDefaultInstance())));

// Assert
sequence
.thenWaitUntil(containerContainsExactly(
helper,
pos.east(),
new ResourceAmount(asResource(DIRT), 1)))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 9),
new ResourceAmount(asResource(STONE), 15),
new ResourceAmount(asResource(damagedDiamondChestplate), 1)
))
.thenSucceed();
});
}

@GameTest(template = "empty_15x15")
public static void shouldExportItemFuzzy(final GameTestHelper helper) {
preparePlot(helper, true, Direction.EAST, (exporter, pos, sequence) -> {
// Arrange
final ItemStack damagedDiamondChestplate = DIAMOND_CHESTPLATE.getDefaultInstance();
damagedDiamondChestplate.setDamageValue(500);

sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
insert(helper, network, DIAMOND_CHESTPLATE, 1);
insert(helper, network, asResource(damagedDiamondChestplate), 1);
}));

// Act
exporter.setFuzzyMode(true);
exporter.setFilters(List.of(asResource(DIAMOND_CHESTPLATE)));

// Assert
sequence
.thenWaitUntil(containerContainsExactly(
helper,
pos.east(),
new ResourceAmount(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()), 1),
new ResourceAmount(asResource(damagedDiamondChestplate), 1)))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 10),
new ResourceAmount(asResource(STONE), 15)
))
.thenSucceed();
});
}

@GameTest(template = "empty_15x15")
public static void shouldExportFluid(final GameTestHelper helper) {
preparePlot(helper, false, Direction.EAST, (exporter, 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
exporter.setFilters(List.of(asResource(WATER)));

// Assert
sequence
.thenWaitUntil(() -> helper.assertBlockProperty(pos.east(), LayeredCauldronBlock.LEVEL,
LayeredCauldronBlock.MAX_FILL_LEVEL))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 10),
new ResourceAmount(asResource(STONE), 15),
new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount())
))
.thenSucceed();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.refinedmods.refinedstorage2.platform.common.exporter;

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 net.minecraft.world.level.block.Blocks;
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 ExporterTestPlots {
private ExporterTestPlots() {
}

static void preparePlot(final GameTestHelper helper,
final boolean itemTest,
final Direction direction,
final TriConsumer<ExporterBlockEntity, 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 exporterPos = ZERO.above().above().above();
helper.setBlock(exporterPos, RSBLOCKS.getExporter().getDefault().rotated(direction));
helper.setBlock(exporterPos.east(), itemTest ? Blocks.CHEST : Blocks.CAULDRON);
consumer.accept(
requireBlockEntity(helper, exporterPos, ExporterBlockEntity.class),
exporterPos,
helper.startSequence()
);
}
}
Loading

0 comments on commit 51519e3

Please sign in to comment.