Skip to content

Commit dac15d5

Browse files
committed
Basic unit tests, delete machine data file if it exists
1 parent aef304d commit dac15d5

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

build.gradle

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,26 @@ minecraft {
9494
}
9595
}
9696
}
97+
98+
unitTests {
99+
parent runs.server // This run config inherits settings from the server config
100+
workingDirectory project.file('run/test')
101+
main 'com.alcatrazescapee.mcjunitlib.DedicatedTestServerLauncher' // The main class which launches a customized server which then runs JUnit tests
102+
ideaModule "${project.name}.test" // Tell IDEA to use the classpath of the test module
103+
property 'forge.logging.console.level', 'unittest' // This logging level prevents any other server information messages and leaves only the unit test output
104+
environment 'MOD_CLASSES', String.join(File.pathSeparator,
105+
"${mod_id}%%${sourceSets.main.output.resourcesDir}",
106+
"${mod_id}%%${sourceSets.main.output.classesDirs[0]}",
107+
"${mod_id}%%${sourceSets.test.output.resourcesDir}",
108+
"${mod_id}%%${sourceSets.test.output.classesDirs[0]}",
109+
) // Forge will ignore all test sources unless we explicitly tell it to include them as mod sources
110+
environment 'target', 'fmltestserver' // This is a custom service used to launch with ModLauncher's transforming class loader
111+
mods {
112+
compactmachines { // The mod that is being tested - Replace this with your mod ID!
113+
sources sourceSets.main
114+
}
115+
}
116+
}
97117
}
98118
}
99119

@@ -129,6 +149,9 @@ def getModVersion(filename) {
129149

130150
def dev_mods_dir = "mods"
131151

152+
task("dev") {
153+
println(sourceSets.main.output.classesDirs[0]);
154+
}
132155
repositories {
133156
// Built mods
134157
flatDir {
@@ -146,6 +169,12 @@ repositories {
146169
name 'tterrag maven'
147170
url "http://maven.tterrag.com/"
148171
}
172+
173+
// Unit Tests
174+
maven {
175+
name 'MCUnitTests'
176+
url 'https://jitpack.io'
177+
}
149178
}
150179

151180
def dev_mods = fileTree(dev_mods_dir).filter { it -> it.isFile() }.files.name.collect({ getModVersion(it) })
@@ -156,12 +185,14 @@ dependencies {
156185
// The userdev artifact is a special name and will get all sorts of transformations applied to it.
157186
minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}"
158187

188+
159189
// Deobfuscate each dev mod for runtime
160190
dev_mods.each {
161191
compileOnly fg.deobf(it)
162192
runtimeOnly fg.deobf(it)
163193
}
164194

195+
165196
// JEI
166197
compileOnly fg.deobf("mezz.jei:jei-${jei_mc_version}:${jei_version}:api")
167198
runtimeOnly fg.deobf("mezz.jei:jei-${jei_mc_version}:${jei_version}")
@@ -170,6 +201,8 @@ dependencies {
170201
compileOnly fg.deobf("mcjty.theoneprobe:TheOneProbe-1.16:${top_version}:api")
171202
runtimeOnly fg.deobf("mcjty.theoneprobe:TheOneProbe-1.16:${top_version}")
172203

204+
testImplementation(fg.deobf("com.github.alcatrazEscapee:mcjunitlib:1.3.3-${minecraft_version}"))
205+
173206
}
174207

175208
// Example for how to get properties into the manifest for reading by the runtime..
@@ -227,3 +260,10 @@ publishing {
227260
}
228261
}
229262
}
263+
264+
test {
265+
useJUnitPlatform()
266+
filter {
267+
exclude "com/robotgryphon/compactmachines/tests/minecraft/**"
268+
}
269+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.robotgryphon.compactmachines.tests.minecraft;
2+
3+
import com.robotgryphon.compactmachines.core.Registration;
4+
import com.robotgryphon.compactmachines.data.SavedMachineData;
5+
import net.minecraft.block.Block;
6+
import net.minecraft.server.MinecraftServer;
7+
import net.minecraft.util.ResourceLocation;
8+
import net.minecraft.world.storage.DimensionSavedDataManager;
9+
import net.minecraft.world.storage.WorldSavedData;
10+
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
11+
import net.minecraftforge.fml.network.FMLNetworkConstants;
12+
import net.minecraftforge.fml.server.ServerLifecycleHooks;
13+
import net.minecraftforge.registries.ForgeRegistries;
14+
import org.junit.jupiter.api.Assertions;
15+
import org.junit.jupiter.api.Test;
16+
17+
import java.io.File;
18+
import java.lang.reflect.InvocationTargetException;
19+
import java.lang.reflect.Method;
20+
21+
public class ExampleTest {
22+
23+
@Test
24+
void CanDoBasicTest() {
25+
MinecraftServer serv = ServerLifecycleHooks.getCurrentServer();
26+
// SavedMachineData sd = SavedMachineData.getInstance(serv);
27+
28+
DimensionSavedDataManager ds = serv
29+
.getLevel(Registration.COMPACT_DIMENSION)
30+
.getDataStorage();
31+
32+
SavedMachineData found = ds.get(SavedMachineData::new, SavedMachineData.DATA_NAME);
33+
if(found != null) {
34+
// Assertions.assertNotNull(sd);
35+
Method getDataFile = ObfuscationReflectionHelper.findMethod(
36+
DimensionSavedDataManager.class,
37+
"func_215754_a",
38+
String.class);
39+
40+
try {
41+
File i = (File) getDataFile.invoke(ds, found.getId());
42+
System.out.println(i.getAbsolutePath());
43+
44+
i.delete();
45+
} catch (IllegalAccessException e) {
46+
e.printStackTrace();
47+
} catch (InvocationTargetException e) {
48+
e.printStackTrace();
49+
}
50+
} else {
51+
// File not found
52+
Assertions.assertTrue(true);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)