Skip to content

Commit

Permalink
Move transfer API tests to junit
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Mar 1, 2024
1 parent 258892a commit 22cc090
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,12 @@

package net.fabricmc.fabric.test.transfer.unittests;

import org.slf4j.LoggerFactory;
import net.minecraft.Bootstrap;
import net.minecraft.SharedConstants;

import net.fabricmc.api.ModInitializer;

public class UnitTestsInitializer implements ModInitializer {
@Override
public void onInitialize() {
AttributeTests.run();
BaseStorageTests.run();
FluidItemTests.run();
FluidTests.run();
FluidVariantTests.run();
ItemTests.run();
PlayerInventoryStorageTests.run();
SingleVariantItemStorageTests.run();
TransactionStateTests.run();
UnderlyingViewTests.run();

LoggerFactory.getLogger("fabric-transfer-api-v1 testmod").info("Transfer API unit tests successful.");
public abstract class AbstractTransferApiTest {
protected static void bootstrap() {
SharedConstants.createGameVersion();
Bootstrap.initialize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package net.fabricmc.fabric.test.transfer.unittests;

import static net.fabricmc.fabric.test.transfer.unittests.TestUtil.assertEquals;
import static net.fabricmc.fabric.test.transfer.TestUtil.assertEquals;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import net.minecraft.fluid.Fluids;
import net.minecraft.sound.SoundEvents;
Expand All @@ -28,13 +31,14 @@
/**
* Test that fluid attributes for vanilla fluids have the correct values.
*/
public class AttributeTests {
public static void run() {
testWater();
testLava();
public class AttributeTests extends AbstractTransferApiTest {
@BeforeAll
static void beforeAll() {
bootstrap();
}

private static void testWater() {
@Test
public void testWater() {
FluidVariant water = FluidVariant.of(Fluids.WATER);

assertEquals(SoundEvents.ITEM_BUCKET_FILL, FluidVariantAttributes.getFillSound(water));
Expand All @@ -45,7 +49,8 @@ private static void testWater() {
assertEquals(false, FluidVariantAttributes.isLighterThanAir(water));
}

private static void testLava() {
@Test
public void testLava() {
FluidVariant lava = FluidVariant.of(Fluids.LAVA);

assertEquals(SoundEvents.ITEM_BUCKET_FILL_LAVA, FluidVariantAttributes.getFillSound(lava));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
package net.fabricmc.fabric.test.transfer.unittests;

import static net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants.BUCKET;
import static net.fabricmc.fabric.test.transfer.unittests.TestUtil.assertEquals;
import static net.fabricmc.fabric.test.transfer.TestUtil.assertEquals;

import java.util.Iterator;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import net.minecraft.fluid.Fluids;

import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
Expand All @@ -32,13 +35,14 @@
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleVariantStorage;
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;

public class BaseStorageTests {
public static void run() {
testFilteringStorage();
testNonEmptyIteratorWithModifiedView();
public class BaseStorageTests extends AbstractTransferApiTest {
@BeforeAll
static void beforeAll() {
bootstrap();
}

private static void testFilteringStorage() {
@Test
public void testFilteringStorage() {
SingleVariantStorage<FluidVariant> storage = new SingleVariantStorage<>() {
@Override
protected FluidVariant getBlankVariant() {
Expand Down Expand Up @@ -102,7 +106,8 @@ protected boolean canInsert(FluidVariant resource) {
* Regression test for <a href="https://github.com/FabricMC/fabric/issues/3414">
* {@code nonEmptyIterator} not handling views that become empty during iteration correctly</a>.
*/
private static void testNonEmptyIteratorWithModifiedView() {
@Test
public void testNonEmptyIteratorWithModifiedView() {
SingleVariantStorage<FluidVariant> storage = SingleFluidStorage.withFixedCapacity(BUCKET, () -> { });
storage.variant = FluidVariant.of(Fluids.WATER);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

import static net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants.BOTTLE;
import static net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants.BUCKET;
import static net.fabricmc.fabric.test.transfer.unittests.TestUtil.assertEquals;
import static net.fabricmc.fabric.test.transfer.TestUtil.assertEquals;

import java.util.List;

import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.PotionContentsComponent;
Expand All @@ -48,17 +50,14 @@
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;

class FluidItemTests {
public static void run() {
testFluidItemApi();
testWaterPotion();
testSimpleContentsQuery();

// Ensure this doesn't throw an error due to the empty stack.
assertEquals(null, ContainerItemContext.withConstant(ItemStack.EMPTY).find(FluidStorage.ITEM));
class FluidItemTests extends AbstractTransferApiTest {
@BeforeAll
static void beforeAll() {
bootstrap();
}

private static void testFluidItemApi() {
@Test
public void testFluidItemApi() {
FluidVariant water = FluidVariant.of(Fluids.WATER);
ItemVariant waterBucket = ItemVariant.of(Items.WATER_BUCKET);
Inventory testInventory = new FluidItemTestInventory(ItemStack.EMPTY, new ItemStack(Items.BUCKET), new ItemStack(Items.WATER_BUCKET));
Expand Down Expand Up @@ -144,7 +143,8 @@ public List<SingleSlotStorage<ItemVariant>> getAdditionalSlots() {
}
}

private static void testWaterPotion() {
@Test
public void testWaterPotion() {
FluidVariant water = FluidVariant.of(Fluids.WATER);
Inventory testInventory = new SimpleInventory(new ItemStack(Items.GLASS_BOTTLE));

Expand Down Expand Up @@ -175,7 +175,8 @@ private static void testWaterPotion() {
}
}

private static void testSimpleContentsQuery() {
@Test
public void testSimpleContentsQuery() {
assertEquals(
new ResourceAmount<>(FluidVariant.of(Fluids.WATER), BUCKET),
StorageUtil.findExtractableContent(
Expand All @@ -194,6 +195,12 @@ private static void testSimpleContentsQuery() {
);
}

@Test
public void testDoesNotThrow() {
// Ensure this doesn't throw an error due to the empty stack.
assertEquals(null, ContainerItemContext.withConstant(ItemStack.EMPTY).find(FluidStorage.ITEM));
}

@Nullable
public static RegistryEntry<Potion> getPotion(ItemStack stack) {
return stack.getOrDefault(DataComponentTypes.POTION_CONTENTS, PotionContentsComponent.DEFAULT).potion().orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import static net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants.BUCKET;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import net.minecraft.component.ComponentChanges;
import net.minecraft.component.DataComponentType;
import net.minecraft.fluid.Fluids;
Expand All @@ -34,15 +37,10 @@
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
import net.fabricmc.fabric.test.transfer.ingame.TransferTestInitializer;

class FluidTests {
public static void run() {
testFluidStorage();
}

private static final FluidVariant TAGGED_WATER, TAGGED_WATER_2, WATER, LAVA;
class FluidTests extends AbstractTransferApiTest {
private static FluidVariant TAGGED_WATER, TAGGED_WATER_2, WATER, LAVA;
private static int finalCommitCount = 0;
public static final DataComponentType<Integer> TEST = Registry.register(Registries.DATA_COMPONENT_TYPE, new Identifier(TransferTestInitializer.MOD_ID, "test"),
DataComponentType.<Integer>builder().codec(Codecs.NONNEGATIVE_INT).packetCodec(PacketCodecs.VAR_INT).build());
public static DataComponentType<Integer> TEST;
private static SingleSlotStorage<FluidVariant> createWaterStorage() {
return new SingleVariantStorage<>() {
@Override
Expand All @@ -67,17 +65,23 @@ protected void onFinalCommit() {
};
}

static {
@BeforeAll
static void beforeAll() {
bootstrap();

ComponentChanges components = ComponentChanges.builder()
.add(TEST, 1)
.build();
TAGGED_WATER = FluidVariant.of(Fluids.WATER, components);
TAGGED_WATER_2 = FluidVariant.of(Fluids.WATER, components);
WATER = FluidVariant.of(Fluids.WATER);
LAVA = FluidVariant.of(Fluids.LAVA);
TEST = Registry.register(Registries.DATA_COMPONENT_TYPE, new Identifier(TransferTestInitializer.MOD_ID, "test"),
DataComponentType.<Integer>builder().codec(Codecs.NONNEGATIVE_INT).packetCodec(PacketCodecs.VAR_INT).build());
}

private static void testFluidStorage() {
@Test
public void testFluidStorage() {
SingleSlotStorage<FluidVariant> waterStorage = createWaterStorage();

// Test content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@

package net.fabricmc.fabric.test.transfer.unittests;

import static net.fabricmc.fabric.test.transfer.unittests.TestUtil.assertEquals;
import static net.fabricmc.fabric.test.transfer.TestUtil.assertEquals;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;

import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;

class FluidVariantTests {
public static void run() {
testFlowing();
class FluidVariantTests extends AbstractTransferApiTest {
@BeforeAll
static void beforeAll() {
bootstrap();
}

private static void testFlowing() {
@Test
public void testFlowing() {
assertFluidEquals(Fluids.WATER, FluidVariant.of(Fluids.WATER), FluidVariant.of(Fluids.FLOWING_WATER));
assertFluidEquals(Fluids.LAVA, FluidVariant.of(Fluids.LAVA), FluidVariant.of(Fluids.FLOWING_LAVA));
assertEquals(FluidVariant.of(Fluids.WATER), FluidVariant.of(Fluids.FLOWING_WATER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

package net.fabricmc.fabric.test.transfer.unittests;

import static net.fabricmc.fabric.test.transfer.unittests.TestUtil.assertEquals;
import static net.fabricmc.fabric.test.transfer.TestUtil.assertEquals;

import java.util.stream.IntStream;

import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import net.minecraft.component.ComponentChanges;
import net.minecraft.component.DataComponentType;
Expand Down Expand Up @@ -48,19 +50,18 @@
/**
* Tests for the item transfer APIs.
*/
class ItemTests {
public static final DataComponentType<Integer> ENERGY = Registry.register(Registries.DATA_COMPONENT_TYPE, new Identifier(TransferTestInitializer.MOD_ID, "energy"),
DataComponentType.<Integer>builder().codec(Codecs.NONNEGATIVE_INT).packetCodec(PacketCodecs.VAR_INT).build());

public static void run() {
testStackReference();
testInventoryWrappers();
testLimitedStackCountInventory();
testLimitedStackCountItem();
testSimpleInventoryUpdates();
class ItemTests extends AbstractTransferApiTest {
public static DataComponentType<Integer> ENERGY;

@BeforeAll
static void beforeAll() {
bootstrap();
ENERGY = Registry.register(Registries.DATA_COMPONENT_TYPE, new Identifier(TransferTestInitializer.MOD_ID, "energy"),
DataComponentType.<Integer>builder().codec(Codecs.NONNEGATIVE_INT).packetCodec(PacketCodecs.VAR_INT).build());
}

private static void testStackReference() {
@Test
public void testStackReference() {
// Ensure that Inventory wrappers will try to mutate the backing stack as much as possible.
// In many cases, MC code captures a reference to the ItemStack so we want to edit that stack directly
// and not a copy whenever we can. Obviously this can't be perfect, but we try to cover as many cases as possible.
Expand Down Expand Up @@ -98,7 +99,8 @@ private static void testStackReference() {
if (!stackEquals(stack, newVariant, 5)) throw new AssertionError("Failed to update stack NBT or count.");
}

private static void testInventoryWrappers() {
@Test
public void testInventoryWrappers() {
ItemVariant emptyBucket = ItemVariant.of(Items.BUCKET);
TestSidedInventory testInventory = new TestSidedInventory();
checkComparatorOutput(testInventory);
Expand Down Expand Up @@ -189,7 +191,8 @@ public boolean canExtract(int slot, ItemStack stack, Direction dir) {
/**
* Test insertion when {@link Inventory#getMaxCountPerStack()} is the bottleneck.
*/
private static void testLimitedStackCountInventory() {
@Test
public void testLimitedStackCountInventory() {
ItemVariant diamond = ItemVariant.of(Items.DIAMOND);
LimitedStackCountInventory inventory = new LimitedStackCountInventory(diamond.toStack(), diamond.toStack(), diamond.toStack());
InventoryStorage wrapper = InventoryStorage.of(inventory, null);
Expand All @@ -207,7 +210,8 @@ private static void testLimitedStackCountInventory() {
/**
* Test insertion when {@link Item#getMaxCount()} is the bottleneck.
*/
private static void testLimitedStackCountItem() {
@Test
public void testLimitedStackCountItem() {
ItemVariant diamondPickaxe = ItemVariant.of(Items.DIAMOND_PICKAXE);
LimitedStackCountInventory inventory = new LimitedStackCountInventory(5);
InventoryStorage wrapper = InventoryStorage.of(inventory, null);
Expand Down Expand Up @@ -256,7 +260,8 @@ private static void checkComparatorOutput(Inventory inventory) {
/**
* Ensure that SimpleInventory only calls markDirty at the end of a successful transaction.
*/
private static void testSimpleInventoryUpdates() {
@Test
public void testSimpleInventoryUpdates() {
var simpleInventory = new SimpleInventory(2) {
boolean throwOnMarkDirty = true;
boolean markDirtyCalled = false;
Expand Down
Loading

0 comments on commit 22cc090

Please sign in to comment.