Skip to content

Commit

Permalink
Merge pull request #615 from starforcraft/feat/GH-248/stacktests
Browse files Browse the repository at this point in the history
Stack upgrade gametests
  • Loading branch information
raoulvdberge authored Jul 18, 2024
2 parents 30e9191 + d961e5f commit 5842cfc
Show file tree
Hide file tree
Showing 7 changed files with 547 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,37 @@
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.storage.EmptyActor;
import com.refinedmods.refinedstorage.platform.api.support.network.AbstractNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage.platform.api.support.resource.ResourceContainer;
import com.refinedmods.refinedstorage.platform.common.content.Blocks;
import com.refinedmods.refinedstorage.platform.common.content.Items;
import com.refinedmods.refinedstorage.platform.common.iface.InterfaceBlockEntity;
import com.refinedmods.refinedstorage.platform.common.support.resource.FluidResource;
import com.refinedmods.refinedstorage.platform.common.support.resource.ItemResource;

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

import net.minecraft.core.BlockPos;
import net.minecraft.gametest.framework.GameTestAssertException;
import net.minecraft.gametest.framework.GameTestHelper;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
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;
import net.minecraft.world.phys.AABB;

public final class GameTestUtil {
public static final Blocks RSBLOCKS = Blocks.INSTANCE;
public static final Items RSITEMS = Items.INSTANCE;

private GameTestUtil() {
}
Expand Down Expand Up @@ -106,29 +115,110 @@ public static void assertFluidPresent(final GameTestHelper helper,
);
}

public static void assertItemEntityPresentExactly(final GameTestHelper helper,
final ItemStack itemStack,
final BlockPos pos,
final double expansionAmount) {
final BlockPos blockpos = helper.absolutePos(pos);
final Iterator<ItemEntity> entityIterator = helper.getLevel().getEntities(EntityType.ITEM,
(new AABB(blockpos)).inflate(expansionAmount), Entity::isAlive).iterator();

ItemEntity itemEntity;
do {
if (!entityIterator.hasNext()) {
throw new GameTestAssertException("Expected " + itemStack.getItem().getDescription().getString()
+ " item at: " + blockpos + " with count: " + itemStack.getCount());
}

itemEntity = entityIterator.next();
} while (!itemEntity.getItem().getItem().equals(itemStack.getItem())
|| itemEntity.getItem().getCount() != itemStack.getCount());
}

public static Runnable assertInterfaceEmpty(final GameTestHelper helper,
final BlockPos pos) {
final var interfaceBlockEntity = requireBlockEntity(helper, pos, InterfaceBlockEntity.class);

return assertResourceContainerEmpty(interfaceBlockEntity.getDisplayName(),
interfaceBlockEntity.getExportedResources());
}

private static Runnable assertResourceContainerEmpty(final Component displayName,
final ResourceContainer container) {
return () -> {
for (int i = 0; i < container.size(); i++) {
if (!container.isEmpty(i)) {
throw new GameTestAssertException(displayName.getString() + " should be empty");
}
}
};
}

public static Runnable interfaceContainsExactly(final GameTestHelper helper,
final BlockPos pos,
final ResourceAmount... expected) {
final var interfaceBlockEntity = requireBlockEntity(helper, pos, InterfaceBlockEntity.class);

return resourceContainerContainsExactly(helper, interfaceBlockEntity.getDisplayName(),
interfaceBlockEntity.getExportedResources(), expected);
}

private static Runnable resourceContainerContainsExactly(final GameTestHelper helper,
final Component displayName,
final ResourceContainer container,
final ResourceAmount... expected) {
return () -> {
// TODO: This does not take duplicate ResourceAmount into account
for (final ResourceAmount expectedStack : expected) {
final boolean contains = IntStream.range(0, container.size())
.mapToObj(container::get)
.anyMatch(resource -> resource != null
&& resource.getResource().equals(expectedStack.getResource())
&& resource.getAmount() == expectedStack.getAmount());

helper.assertTrue(contains, "Expected resource is missing from " + displayName.getString() + ": "
+ expectedStack + " with count: " + expectedStack.getAmount());
}

for (int i = 0; i < container.size(); i++) {
final ResourceAmount resource = container.get(i);
if (resource != null) {
final boolean wasExpected = Arrays.stream(expected).anyMatch(
expectedResource -> expectedResource.getResource().equals(resource.getResource())
&& expectedResource.getAmount() == resource.getAmount()
);

helper.assertTrue(wasExpected, "Unexpected resource found in " + displayName.getString() + ": "
+ resource.getResource() + " with count: " + resource.getAmount());
}
}
};
}

public static Runnable containerContainsExactly(final GameTestHelper helper,
final BlockPos pos,
final ResourceAmount... expected) {
final var containerBlockEntity = requireBlockEntity(helper, pos, BaseContainerBlockEntity.class);

return () -> {
// TODO: This does not take duplicate ResourceAmount into account
for (final ResourceAmount expectedStack : expected) {
final boolean contains = IntStream.range(0, containerBlockEntity.getContainerSize())
.mapToObj(containerBlockEntity::getItem)
.anyMatch(inContainer -> asResource(inContainer).equals(expectedStack.getResource())
&& inContainer.getCount() == expectedStack.getAmount());
helper.assertTrue(contains, "Expected resource is missing from storage: "
helper.assertTrue(contains, "Expected resource is missing from container: "
+ expectedStack + " with count: " + expectedStack.getAmount());
}
for (int i = 0; i < containerBlockEntity.getContainerSize(); i++) {
final ItemStack inContainer = containerBlockEntity.getItem(i);

if (inContainer.getItem() != Items.AIR) {
if (!inContainer.isEmpty()) {
final boolean wasExpected = Arrays.stream(expected).anyMatch(
expectedStack -> expectedStack.getResource().equals(asResource(inContainer))
&& expectedStack.getAmount() == inContainer.getCount()
);
helper.assertTrue(wasExpected, "Unexpected resource found in storage: "
helper.assertTrue(wasExpected, "Unexpected resource found in container: "
+ inContainer.getDescriptionId() + " with count: " + inContainer.getCount());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import net.neoforged.neoforge.gametest.GameTestHolder;
import net.neoforged.neoforge.gametest.PrefixGameTestTemplate;

import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.RSITEMS;
import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.asResource;
import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.assertFluidPresent;
import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.assertItemEntityPresentExactly;
import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.insert;
import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.networkIsAvailable;
import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.storageContainsExactly;
Expand Down Expand Up @@ -100,7 +102,12 @@ public static void shouldDropItem(final GameTestHelper helper) {
// Assert
sequence
.thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.DIRT, pos.east()))
.thenWaitUntil(() -> helper.assertItemEntityPresent(DIRT, pos.east(), 1))
.thenWaitUntil(() -> assertItemEntityPresentExactly(
helper,
DIRT.getDefaultInstance().copyWithCount(1),
pos.east(),
1
))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
Expand All @@ -111,6 +118,46 @@ public static void shouldDropItem(final GameTestHelper helper) {
});
}

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

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

// Assert
sequence
.thenIdle(9)
.thenExecute(() -> helper.assertBlockNotPresent(Blocks.DIRT, pos.east()))
.thenExecute(() -> assertItemEntityPresentExactly(
helper,
DIRT.getDefaultInstance().copyWithCount(64),
pos.east(),
1
))
.thenExecute(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 1),
new ResourceAmount(asResource(STONE), 15)
))
.thenIdle(9)
.thenExecute(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(STONE), 15)
))
.thenSucceed();
});
}

@GameTest(template = "empty_15x15")
public static void shouldPlaceFireworks(final GameTestHelper helper) {
preparePlot(helper, Direction.EAST, (constructor, pos, sequence) -> {
Expand Down
Loading

0 comments on commit 5842cfc

Please sign in to comment.