Skip to content

Commit

Permalink
Add a test for startup configs (#1037)
Browse files Browse the repository at this point in the history
  • Loading branch information
TelepathicGrunt authored Jun 5, 2024
1 parent af94732 commit acc3fe8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
2 changes: 2 additions & 0 deletions tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ afterEvaluate {
args.set(outputIndex+1, file('src/generated/resources/').absolutePath);
programArguments.set(args);
}
runs.junit.modSources = runs.junit.modSources.get().stream()
.filter { it != sourceSets.main }.toList()
}

license {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.unittest;

import net.neoforged.fml.ModContainer;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.config.ModConfig;
import net.neoforged.neoforge.common.ModConfigSpec;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

@TestMethodOrder(MethodOrderer.MethodName.class)
public class StartupConfigTest {
private static final String MOD_ID = "startup_config_test";
private static boolean wasLoadedAtInitStartupConfig = false;
private static boolean wasLoadedAtInitCommonConfig = false;
private static boolean wasLoadedAtInitServerConfig = false;
private static boolean wasLoadedAtInitClientConfig = false;

@Test
void testStartupConfigs() {
Assertions.assertTrue(wasLoadedAtInitStartupConfig, "Startup Config was supposed to be loaded at mod init.");
Assertions.assertFalse(wasLoadedAtInitCommonConfig, "Common Config was NOT supposed to be loaded at mod init.");
Assertions.assertFalse(wasLoadedAtInitServerConfig, "Server Config was NOT supposed to be loaded at mod init.");
Assertions.assertFalse(wasLoadedAtInitClientConfig, "Client Config was NOT supposed to be loaded at mod init.");
}

@Mod(value = MOD_ID)
public static class StartupConfigTestMod {
public StartupConfigTestMod(ModContainer modContainer) {
modContainer.registerConfig(ModConfig.Type.STARTUP, StartupConfig.SPEC);
modContainer.registerConfig(ModConfig.Type.COMMON, CommonConfig.SPEC);
modContainer.registerConfig(ModConfig.Type.SERVER, ServerConfig.SPEC);
modContainer.registerConfig(ModConfig.Type.CLIENT, ClientConfig.SPEC);
wasLoadedAtInitStartupConfig = StartupConfig.SPEC.isLoaded();
wasLoadedAtInitCommonConfig = CommonConfig.SPEC.isLoaded();
wasLoadedAtInitServerConfig = ServerConfig.SPEC.isLoaded();
wasLoadedAtInitClientConfig = ClientConfig.SPEC.isLoaded();
}

public static class StartupConfig {
private static final ModConfigSpec.Builder BUILDER = new ModConfigSpec.Builder();
private static final ModConfigSpec.BooleanValue TEST_MARKER = BUILDER.define("testMarker1", true);
static final ModConfigSpec SPEC = BUILDER.build();
}

public static class CommonConfig {
private static final ModConfigSpec.Builder BUILDER = new ModConfigSpec.Builder();
private static final ModConfigSpec.BooleanValue TEST_MARKER = BUILDER.define("testMarker2", true);
static final ModConfigSpec SPEC = BUILDER.build();
}

public static class ServerConfig {
private static final ModConfigSpec.Builder BUILDER = new ModConfigSpec.Builder();
private static final ModConfigSpec.BooleanValue TEST_MARKER = BUILDER.define("testMarker3", true);
static final ModConfigSpec SPEC = BUILDER.build();
}

public static class ClientConfig {
private static final ModConfigSpec.Builder BUILDER = new ModConfigSpec.Builder();
private static final ModConfigSpec.BooleanValue TEST_MARKER = BUILDER.define("testMarker4", true);
static final ModConfigSpec SPEC = BUILDER.build();
}
}
}
8 changes: 8 additions & 0 deletions tests/src/junit/resources/META-INF/neoforge.mods.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
modLoader="javafml"
loaderVersion="[1,)"
license="LGPL v2.1"

[[mods]]
modId="startup_config_test"
[[mods]]
modId="creative_tab_order_test"
3 changes: 0 additions & 3 deletions tests/src/main/resources/META-INF/neoforge.mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ license="LGPL v2.1"
[[mods]]
modId="multiple_entrypoints_test"

[[mods]]
modId="creative_tab_order_test"

[[mods]]
modId="ordered_test_1"
[[mods]]
Expand Down

0 comments on commit acc3fe8

Please sign in to comment.