Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Minecraftschurli committed Mar 18, 2024
1 parent 62b7302 commit 69365ba
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 10 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ plugins {

helper.withApiSourceSet()
helper.withDataGenSourceSet()
helper.withTestSourceSet()

dependencies {
implementation(helper.neoforge())
testImplementation("net.neoforged:testframework:${helper.neoVersion.get()}")
}

helper.withCommonRuns()
helper.withDataGenRuns()
helper.withGameTestRuns()

helper.publication.pom {
organization {
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ org.gradle.jvmargs=-Xmx4G
org.gradle.daemon=false
org.gradle.parallel=true
org.gradle.caching=true
org.gradle.configuration-cache=true

mod.group=com.github.minecraftschurlimods
mod.id=bibliocraft
Expand All @@ -28,7 +29,7 @@ neogradle.subsystems.parchment.mappingsVersion=2024.02.25
mc_version=1.20.4
mc_version_range=[1.20.4,1.20.5)

neo_version=20.4.167
neo_version=20.4.198
neo_version_range=[20.4.80-beta,20.5)

loader.version=[1,)
4 changes: 2 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pluginManagement {
plugins {
id("net.neoforged.gradle.userdev") version "7.0.80"
id("com.github.minecraftschurlimods.helperplugin") version "1.9"
id("net.neoforged.gradle.userdev") version "7.0.+"
id("com.github.minecraftschurlimods.helperplugin") version "1.10"
}
repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public void register(WoodType woodType, Supplier<BlockBehaviour.Properties> prop
public void register(ResourceLocation id, WoodType woodType, Supplier<BlockBehaviour.Properties> properties, ResourceLocation texture, BlockFamily family) {
if (this.woodTypes.containsKey(id))
throw new IllegalStateException("Wood type " + id + " is already registered");
System.out.println("Registering wood type " + woodType.name());
this.woodTypes.put(id, new BibliocraftWoodType(id, woodType, properties, texture, family));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ private static void entityAttributeCreation(EntityAttributeCreationEvent event)

@SubscribeEvent
private static void registerBibliocraftWoodTypes(RegisterBibliocraftWoodTypesEvent event) {
System.out.println("registering vanilla bibliocraft wood types");
registerVanilla(event, WoodType.OAK, Blocks.OAK_PLANKS, BlockFamilies.OAK_PLANKS);
registerVanilla(event, WoodType.SPRUCE, Blocks.SPRUCE_PLANKS, BlockFamilies.SPRUCE_PLANKS);
registerVanilla(event, WoodType.BIRCH, Blocks.BIRCH_PLANKS, BlockFamilies.BIRCH_PLANKS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public BibliocraftWoodTypeRegistryImpl() {

@ApiStatus.Internal
public void register() {
System.out.println("Posting Bibliocraft Wood Types registry event");
ModLoader.get().postEvent(new RegisterBibliocraftWoodTypesEvent(this.values));
this.loaded = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ protected void defineSynchedData() {

@Override
public void tick() {
super.tick();
BlockPos pos = blockPosition();
BlockState state = level().getBlockState(pos);
if (level().isClientSide()) return;
if (!(state.getBlock() instanceof SeatBlock)) {
getPassengers().forEach(Entity::stopRiding);
removeAfterChangingDimensions();
discard();
} else if (getPassengers().isEmpty()) {
removeAfterChangingDimensions();
discard();
if (state.getBlock() instanceof SeatBlock) {
level().setBlockAndUpdate(pos, state.setValue(SeatBlock.OCCUPIED, false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class WoodTypeDeferredHolder<R, T extends R> {
* @param creator A function of {@link BibliocraftWoodType} to {@code T}, responsible for actually creating the {@link DeferredHolder}.
*/
public WoodTypeDeferredHolder(DeferredRegister<R> register, String suffix, Function<BibliocraftWoodType, ? extends T> creator) {
System.out.println("Creating " + suffix);
for (BibliocraftWoodType type : BibliocraftApi.getWoodTypeRegistry().getAll()) {
map.put(type, register.register(type.getRegistrationPrefix() + "_" + suffix, () -> creator.apply(type)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.minecraftschurlimods.bibliocraft.test;


import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftApi;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftDatagenHelper;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftWoodTypeRegistry;
import com.github.minecraftschurlimods.bibliocraft.apiimpl.BibliocraftDatagenHelperImpl;
import com.github.minecraftschurlimods.bibliocraft.apiimpl.BibliocraftWoodTypeRegistryImpl;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.world.level.block.state.properties.WoodType;
import net.neoforged.testframework.annotation.ForEachTest;
import net.neoforged.testframework.annotation.TestHolder;
import net.neoforged.testframework.gametest.EmptyTemplate;
import net.neoforged.testframework.gametest.ExtendedGameTestHelper;

import static com.github.minecraftschurlimods.bibliocraft.test.GametestAssertions.*;

@ForEachTest(groups = BibliocraftApi.MOD_ID + ".api")
public class BibliocraftApiTest {
@GameTest
@EmptyTemplate
@TestHolder(description = "Test that the BibliocraftDatagenHelper is available via the BibliocraftApi")
public static void testDatagenHelperAvailable(ExtendedGameTestHelper helper) {
BibliocraftDatagenHelper datagenHelper = BibliocraftApi.getDatagenHelper();
assertNotNull(datagenHelper, "BibliocraftDatagenHelper is not available");
assertInstance(datagenHelper, BibliocraftDatagenHelperImpl.class, "BibliocraftDatagenHelper implementation is replaced");
helper.succeed();
}

@GameTest
@EmptyTemplate
@TestHolder(description = "Test that the BibliocraftWoodTypeRegistry is available via the BibliocraftApi")
public static void testWoodTypeRegistryAvailable(ExtendedGameTestHelper helper) {
BibliocraftWoodTypeRegistry woodTypeRegistry = BibliocraftApi.getWoodTypeRegistry();
assertNotNull(woodTypeRegistry, "BibliocraftWoodTypeRegistry is not available");
assertInstance(woodTypeRegistry, BibliocraftWoodTypeRegistryImpl.class, "BibliocraftWoodTypeRegistry implementation is replaced");
assertNotNull(woodTypeRegistry.get(WoodType.OAK), "Oak WoodType is not registered");
assertNotNull(woodTypeRegistry.get(WoodType.SPRUCE), "Spruce WoodType is not registered");
assertNotNull(woodTypeRegistry.get(WoodType.BIRCH), "Birch WoodType is not registered");
assertNotNull(woodTypeRegistry.get(WoodType.JUNGLE), "Jungle WoodType is not registered");
assertNotNull(woodTypeRegistry.get(WoodType.ACACIA), "Acacia WoodType is not registered");
assertNotNull(woodTypeRegistry.get(WoodType.DARK_OAK), "Dark Oak WoodType is not registered");
assertNotNull(woodTypeRegistry.get(WoodType.CRIMSON), "Crimson WoodType is not registered");
assertNotNull(woodTypeRegistry.get(WoodType.WARPED), "Warped WoodType is not registered");
assertNotNull(woodTypeRegistry.get(WoodType.MANGROVE), "Mangrove WoodType is not registered");
assertNotNull(woodTypeRegistry.get(WoodType.BAMBOO), "Bamboo WoodType is not registered");
assertNotNull(woodTypeRegistry.get(WoodType.CHERRY), "Cherry WoodType is not registered");
helper.succeed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.github.minecraftschurlimods.bibliocraft.test;

import net.minecraft.gametest.framework.GameTestAssertException;
import org.jetbrains.annotations.ApiStatus.NonExtendable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;

import java.math.BigDecimal;
import java.util.Objects;

@NonExtendable
public interface GametestAssertions {
@Contract("false, _ -> fail")
static void assertTrue(boolean condition, String message) {
if (!condition) {
fail(message);
}
}

@Contract("true, _ -> fail")
static void assertFalse(boolean condition, String message) {
if (condition) {
fail(message);
}
}

@SuppressWarnings("UnusedReturnValue")
static <T> T assertInstance(@Nullable Object instance, Class<T> clazz, String message) {
if (!clazz.isInstance(instance)) {
fail(message);
return null; // never reached
} else {
return clazz.cast(instance);
}
}

@Contract("!null, _ -> fail")
static void assertNull(@Nullable Object o, String message) {
if (o != null) {
fail(message);
}
}

@Contract("null, _ -> fail")
static void assertNotNull(@Nullable Object o, String message) {
if (o == null) {
fail(message);
}
}

static void assertEquals(@Nullable Object expected, @Nullable Object actual, String message) {
if (expected == null) {
assertNull(actual, message);
}
if (expected instanceof BigDecimal bd1 && actual instanceof BigDecimal bd2) {
assertEquals(bd1, bd2, message);
}
if (!Objects.equals(expected, actual)) {
fail(message);
}
}

static void assertEquals(@Nullable BigDecimal expected, @Nullable BigDecimal actual, String message) {
if (expected == null) {
assertNull(actual, message);
}
if (Objects.compare(expected, actual, Comparable::compareTo) != 0) {
fail(message);
}
}

@Contract("_ -> fail")
static void fail(String message) throws GameTestAssertException {
throw new GameTestAssertException(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.minecraftschurlimods.bibliocraft.test;

import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftApi;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.bus.api.EventPriority;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.ModList;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLConstructModEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import net.neoforged.testframework.conf.ClientConfiguration;
import net.neoforged.testframework.conf.Feature;
import net.neoforged.testframework.conf.FrameworkConfiguration;
import net.neoforged.testframework.impl.MutableTestFramework;
import net.neoforged.testframework.summary.GitHubActionsStepSummaryFormatter;
import org.lwjgl.glfw.GLFW;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD, modid = BibliocraftApi.MOD_ID)
public final class Tests {
@SubscribeEvent(priority = EventPriority.HIGHEST)
static void init(FMLConstructModEvent event) {
ModList.get().getModContainerById(BibliocraftApi.MOD_ID).ifPresent(Tests::init);
}

private static void init(ModContainer container) {
final MutableTestFramework framework = FrameworkConfiguration.builder(new ResourceLocation(container.getModId(), "tests"))
.clientConfiguration(() -> ClientConfiguration.builder()
.toggleOverlayKey(GLFW.GLFW_KEY_J)
.openManagerKey(GLFW.GLFW_KEY_N)
.build())
.enable(Feature.CLIENT_SYNC, Feature.CLIENT_MODIFICATIONS, Feature.TEST_STORE)
.formatters(new GitHubActionsStepSummaryFormatter("Bibliocraft Gametest Summary"))
.build().create();

framework.init(container.getEventBus(), container);

NeoForge.EVENT_BUS.addListener((final RegisterCommandsEvent event) -> {
final LiteralArgumentBuilder<CommandSourceStack> node = Commands.literal("tests");
framework.registerCommands(node);
event.getDispatcher().register(node);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
package com.github.minecraftschurlimods.bibliocraft.test;

import net.minecraft.MethodsReturnNonnullByDefault;

import javax.annotation.ParametersAreNonnullByDefault;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
todo:
- add tests for interactions
- bookcase
- display case
- fancy armor stand
- potion shelf
- seat
- shelf
- sword pedestal
- table
- toolrack
- add tests for automation (hoppers etc.)

0 comments on commit 69365ba

Please sign in to comment.