Skip to content

Commit

Permalink
More exporter and importer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
starforcraft committed May 26, 2024
1 parent 482c4e9 commit f4109bf
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 3 deletions.
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);
}

protected 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
Expand Up @@ -2,10 +2,12 @@

import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.support.resource.ItemResource;
import com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil;
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.Blocks;
import net.minecraft.world.level.block.LayeredCauldronBlock;
import net.neoforged.neoforge.gametest.GameTestHolder;
Expand All @@ -15,8 +17,7 @@

import static com.refinedmods.refinedstorage2.platform.common.exporter.ExporterTestPlots.preparePlot;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.*;
import static net.minecraft.world.item.Items.DIRT;
import static net.minecraft.world.item.Items.STONE;
import static net.minecraft.world.item.Items.*;
import static net.minecraft.world.level.material.Fluids.WATER;

@GameTestHolder(IdentifierUtil.MOD_ID)
Expand Down Expand Up @@ -52,6 +53,40 @@ public static void shouldExportBlock(final GameTestHelper helper) {
});
}

@GameTest(template = "empty_15x15")
public static void shouldExportBlockFuzzy(final GameTestHelper helper) {
preparePlot(helper, Direction.EAST, (exporter, pos, sequence) -> {
// Arrange
helper.setBlock(pos.east(), Blocks.CHEST);

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, new ItemResource(damagedDiamondChestplate.getItem(), damagedDiamondChestplate.getTag()), 1);
}));

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

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

@GameTest(template = "empty_15x15")
public static void shouldExportWater(final GameTestHelper helper) {
preparePlot(helper, Direction.EAST, (exporter, pos, sequence) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.refinedmods.refinedstorage2.platform.common.importer;

import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.api.resource.filter.FilterMode;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil;
import net.minecraft.core.Direction;

Check failure on line 7 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java

View workflow job for this annotation

GitHub Actions / build / build

Import statement for 'net.minecraft.core.Direction' is in the wrong order. Should be in the 'THIRD_PARTY_PACKAGE' group, expecting group 'STANDARD_JAVA_PACKAGE' on this line.
import net.minecraft.gametest.framework.GameTest;

Check failure on line 8 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java

View workflow job for this annotation

GitHub Actions / build / build

Import statement for 'net.minecraft.gametest.framework.GameTest' is in the wrong order. Should be in the 'THIRD_PARTY_PACKAGE' group, expecting group 'STANDARD_JAVA_PACKAGE' on this line.
import net.minecraft.gametest.framework.GameTestHelper;

Check failure on line 9 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java

View workflow job for this annotation

GitHub Actions / build / build

Import statement for 'net.minecraft.gametest.framework.GameTestHelper' is in the wrong order. Should be in the 'THIRD_PARTY_PACKAGE' group, expecting group 'STANDARD_JAVA_PACKAGE' on this line.
import net.minecraft.world.item.ItemStack;

Check failure on line 10 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java

View workflow job for this annotation

GitHub Actions / build / build

Import statement for 'net.minecraft.world.item.ItemStack' is in the wrong order. Should be in the 'THIRD_PARTY_PACKAGE' group, expecting group 'STANDARD_JAVA_PACKAGE' on this line.
import net.minecraft.world.item.Items;

Check failure on line 11 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java

View workflow job for this annotation

GitHub Actions / build / build

Import statement for 'net.minecraft.world.item.Items' is in the wrong order. Should be in the 'THIRD_PARTY_PACKAGE' group, expecting group 'STANDARD_JAVA_PACKAGE' on this line.
import net.minecraft.world.level.block.Blocks;

Check failure on line 12 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java

View workflow job for this annotation

GitHub Actions / build / build

Import statement for 'net.minecraft.world.level.block.Blocks' is in the wrong order. Should be in the 'THIRD_PARTY_PACKAGE' group, expecting group 'STANDARD_JAVA_PACKAGE' on this line.
import net.minecraft.world.level.block.LayeredCauldronBlock;

Check failure on line 13 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java

View workflow job for this annotation

GitHub Actions / build / build

Import statement for 'net.minecraft.world.level.block.LayeredCauldronBlock' is in the wrong order. Should be in the 'THIRD_PARTY_PACKAGE' group, expecting group 'STANDARD_JAVA_PACKAGE' on this line.
import net.neoforged.neoforge.gametest.GameTestHolder;
import net.neoforged.neoforge.gametest.PrefixGameTestTemplate;

import java.util.Set;

import static com.refinedmods.refinedstorage2.platform.common.importer.ImporterTestPlots.prepareChest;
import static com.refinedmods.refinedstorage2.platform.common.importer.ImporterTestPlots.preparePlot;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.*;
Expand Down Expand Up @@ -48,6 +53,71 @@ public static void shouldImportBlock(final GameTestHelper helper) {
});
}

@GameTest(template = "empty_15x15")
public static void shouldImportBlockFuzzyBlocklist(final GameTestHelper helper) {
preparePlot(helper, Direction.EAST, (importer, pos, sequence) -> {
// Arrange
ItemStack damagedDiamondChestplate = DIAMOND_CHESTPLATE.getDefaultInstance();
damagedDiamondChestplate.setDamageValue(500);
prepareChest(helper, pos.east(), Items.DIRT.getDefaultInstance(), DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate);

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

// Act
importer.setFuzzyMode(true);
importer.setFilters(Set.of(asResource(DIAMOND_CHESTPLATE.getDefaultInstance())));
importer.setFilterMode(FilterMode.BLOCK);

// Assert
sequence
.thenWaitUntil(() -> assertContainerContains(helper, pos.east(), DIAMOND_CHESTPLATE.getDefaultInstance()))
.thenWaitUntil(() -> assertContainerContains(helper, pos.east(), damagedDiamondChestplate))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 11),
new ResourceAmount(asResource(STONE), 15)
))
.thenSucceed();
});
}

@GameTest(template = "empty_15x15")
public static void shouldImportBlockFuzzyAllowlist(final GameTestHelper helper) {
preparePlot(helper, Direction.EAST, (importer, pos, sequence) -> {
// Arrange
ItemStack damagedDiamondChestplate = DIAMOND_CHESTPLATE.getDefaultInstance();
damagedDiamondChestplate.setDamageValue(500);
prepareChest(helper, pos.east(), Items.DIRT.getDefaultInstance(), DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate);

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

// Act
importer.setFuzzyMode(true);
importer.setFilters(Set.of(asResource(DIAMOND_CHESTPLATE.getDefaultInstance())));
importer.setFilterMode(FilterMode.ALLOW);

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

@GameTest(template = "empty_15x15")
public static void shouldImportWater(final GameTestHelper helper) {
preparePlot(helper, Direction.EAST, (importer, pos, sequence) -> {
Expand All @@ -61,6 +131,7 @@ public static void shouldImportWater(final GameTestHelper helper) {

// Assert
sequence
.thenWaitUntil(() -> helper.assertBlockPresent(Blocks.CAULDRON, pos.east()))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@

import java.util.Arrays;
import java.util.function.Consumer;
import java.util.stream.IntStream;
import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.gametest.framework.GameTestAssertException;
import net.minecraft.gametest.framework.GameTestHelper;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.FluidState;
Expand Down Expand Up @@ -94,6 +99,24 @@ public static <T extends BlockEntity> T requireBlockEntity(
return (T) blockEntity;
}

public static void assertContainerContains(final GameTestHelper helper,
final BlockPos pos,
final ItemStack itemStack) {
BlockPos blockpos = helper.absolutePos(pos);
BlockEntity blockentity = helper.getLevel().getBlockEntity(blockpos);
if (!(blockentity instanceof BaseContainerBlockEntity containerBlockEntity)) {
throw new GameTestAssertException("Expected a container at " + pos + ", found " + BuiltInRegistries.BLOCK_ENTITY_TYPE.getKey(blockentity.getType()));
} else {
boolean success = IntStream.range(0, containerBlockEntity.getContainerSize())
.mapToObj(containerBlockEntity::getItem)
.anyMatch(stack -> stack.getItem().equals(itemStack.getItem()) && stack.getTag().equals(itemStack.getTag()));

if(!success) {
throw new GameTestAssertException("Container should contain: " + itemStack.getItem() + " with tag: " + itemStack.getTag());
}
}
}

public static void assertFluidPresent(final GameTestHelper helper,
final BlockPos pos,
final Fluid fluid,
Expand Down Expand Up @@ -131,6 +154,10 @@ public static ItemResource asResource(final Item item) {
return new ItemResource(item, null);
}

public static ItemResource asResource(final ItemStack itemStack) {
return new ItemResource(itemStack.getItem(), itemStack.getTag());
}

public static FluidResource asResource(final Fluid fluid) {
return new FluidResource(fluid, null);
}
Expand Down

0 comments on commit f4109bf

Please sign in to comment.