mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-19 11:59:08 +00:00
test: characterize registered factory lookup
This commit is contained in:
1 parent
fbef5d4653
commit
2a5298077f
2 files changed
+109
No files matched your search
+67
@@ -0,0 +1,67 @@
|
||||
package codechicken.multipart.test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.function.Function;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import codechicken.microblock.MicroMaterialRegistry;
|
||||
import codechicken.microblock.Microblock;
|
||||
import codechicken.microblock.MicroblockClass;
|
||||
import codechicken.microblock.MicroblockClient;
|
||||
import codechicken.multipart.MultiPartRegistry$;
|
||||
import codechicken.multipart.MultiPartRegistry.IPartFactory2;
|
||||
import cpw.mods.fml.relauncher.ReflectionHelper;
|
||||
import scala.Option;
|
||||
|
||||
class PartFactoryLookupFunctionalTest {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void reflectedFactoriesSupportMicroblockConstructionAndLoading() throws Exception {
|
||||
Field field = ReflectionHelper
|
||||
.findField(MultiPartRegistry$.class, "codechicken$multipart$MultiPartRegistry$$typeMap");
|
||||
scala.collection.mutable.Map<String, IPartFactory2> map = (scala.collection.mutable.Map<String, IPartFactory2>) field
|
||||
.get(MultiPartRegistry$.MODULE$);
|
||||
checkMicroblocks(name -> {
|
||||
Option<IPartFactory2> factory = map.get(name);
|
||||
return factory.isEmpty() ? null : factory.get();
|
||||
});
|
||||
}
|
||||
|
||||
private static void checkMicroblocks(Function<String, IPartFactory2> lookup) throws Exception {
|
||||
assertNull(lookup.apply("test:no_such_factory"));
|
||||
assertFalse(lookup.apply("mc_torch") instanceof MicroblockClass);
|
||||
int material = MicroMaterialRegistry.materialID("minecraft:stone");
|
||||
for (String name : new String[] { "mcr_face", "mcr_hllw", "mcr_cnr", "mcr_edge", "mcr_post" }) {
|
||||
IPartFactory2 factory = lookup.apply(name);
|
||||
assertTrue(factory instanceof MicroblockClass);
|
||||
assertSame(factory, lookup.apply(new String(name)));
|
||||
Microblock part = (Microblock) MicroblockClass.class.getMethod("create", boolean.class, int.class)
|
||||
.invoke(factory, false, material);
|
||||
assertSame(factory, part.microClass());
|
||||
assertEquals(name, part.getType());
|
||||
assertEquals(material, part.material());
|
||||
assertFalse(part instanceof MicroblockClient);
|
||||
assertNull(part.tile());
|
||||
assertNotSame(part, ((MicroblockClass) factory).create(false, material));
|
||||
NBTTagCompound tag = new NBTTagCompound();
|
||||
tag.setString("id", name);
|
||||
tag.setString("material", "minecraft:stone");
|
||||
tag.setByte("shape", (byte) 0x21);
|
||||
part.load(tag);
|
||||
assertEquals(0x21, part.shape());
|
||||
assertEquals(material, part.material());
|
||||
assertNull(part.tile(), "Factory construction and NBT load do not bind a part");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.function.Function;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.material.Material;
|
||||
@@ -33,6 +34,44 @@ import scala.Option;
|
||||
*/
|
||||
class MultiPartRegistryCharacterizationTest {
|
||||
|
||||
@Test
|
||||
void reflectedFactoryLookupTracksRegistryWithoutConstructingParts() throws IllegalAccessException {
|
||||
checkFactoryLookup(reflectedFactoryLookup());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Function<String, IPartFactory2> reflectedFactoryLookup() throws IllegalAccessException {
|
||||
Field field = ReflectionHelper
|
||||
.findField(MultiPartRegistry$.class, "codechicken$multipart$MultiPartRegistry$$typeMap");
|
||||
scala.collection.mutable.Map<String, IPartFactory2> map = (scala.collection.mutable.Map<String, IPartFactory2>) field
|
||||
.get(MultiPartRegistry$.MODULE$);
|
||||
return name -> {
|
||||
Option<IPartFactory2> factory = map.get(name);
|
||||
return factory.isEmpty() ? null : factory.get();
|
||||
};
|
||||
}
|
||||
|
||||
private static void checkFactoryLookup(Function<String, IPartFactory2> lookup) {
|
||||
String name = "test:factory_lookup";
|
||||
TestFactory first = new TestFactory(new TestPart());
|
||||
TestFactory second = new TestFactory(new TestPart());
|
||||
assertNull(lookup.apply(name));
|
||||
assertNull(lookup.apply(""));
|
||||
try {
|
||||
MultiPartRegistry.typeMapBacking().put(name, first);
|
||||
assertSame(first, lookup.apply(new String(name)));
|
||||
assertNull(lookup.apply("TEST:factory_lookup"));
|
||||
MultiPartRegistry.typeMapBacking().put(name, second);
|
||||
assertSame(second, lookup.apply(name));
|
||||
MultiPartRegistry.typeMapBacking().remove(name);
|
||||
assertNull(lookup.apply(name));
|
||||
assertEquals(0, first.calls);
|
||||
assertEquals(0, second.calls);
|
||||
} finally {
|
||||
MultiPartRegistry.typeMapBacking().remove(name);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void noPartsAreLoadedBeforeRegistrationRuns() {
|
||||
assertFalse(MultiPartRegistry.loaded());
|
||||
@@ -144,6 +183,7 @@ class MultiPartRegistryCharacterizationTest {
|
||||
private static final class TestFactory implements IPartFactory2 {
|
||||
|
||||
private final TMultiPart part;
|
||||
private int calls;
|
||||
|
||||
private TestFactory(TMultiPart part) {
|
||||
this.part = part;
|
||||
@@ -151,11 +191,13 @@ class MultiPartRegistryCharacterizationTest {
|
||||
|
||||
@Override
|
||||
public TMultiPart createPart(String name, NBTTagCompound nbt) {
|
||||
calls++;
|
||||
return part;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TMultiPart createPart(String name, MCDataInput packet) {
|
||||
calls++;
|
||||
return part;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user