Skip to content

Commit 87b1dcd

Browse files
committed
[wip] [no ci]
1 parent 3b42d42 commit 87b1dcd

File tree

11 files changed

+125
-164
lines changed

11 files changed

+125
-164
lines changed

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ plugins {
5757
}
5858

5959
include("spatial")
60-
// include("spatial-neoforge")
60+
include("spatial-neoforge")

spatial-neoforge/build.gradle.kts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@ neoForge {
2727
}
2828

2929
create("spatialtest") {
30-
modSourceSets.add(spatialLib.sourceSets.main)
31-
modSourceSets.add(sourceSets.main)
3230
modSourceSets.add(sourceSets.test)
3331
}
3432
}
3533

3634
unitTest {
3735
enable()
38-
testedMod = mods.named("spatialtest")
36+
testedMod = mods.named("spatial")
3937
}
4038

4139
runs {
@@ -53,6 +51,13 @@ neoForge {
5351
dependencies {
5452
testImplementation(spatialLib)
5553
testImplementation(neoforged.testframework)
54+
55+
testImplementation("org.junit.jupiter:junit-jupiter:5.7.1")
56+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
57+
}
58+
59+
tasks.withType<Test> {
60+
useJUnitPlatform()
5661
}
5762

5863
tasks.withType<ProcessResources>().configureEach {

spatial/src/test/java/dev/compactmods/spatial/test/gametest/TestUtils.java renamed to spatial-neoforge/src/test/java/dev/compactmods/spatial/test/TestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dev.compactmods.spatial.test.gametest;
1+
package dev.compactmods.spatial.test;
22

33
import net.minecraft.util.RandomSource;
44
import net.minecraft.world.phys.Vec3;

spatial/src/test/java/dev/compactmods/spatial/test/core/EmptyTestSizes.java renamed to spatial-neoforge/src/test/java/dev/compactmods/spatial/test/core/EmptyTestSizes.java

File renamed without changes.

spatial/src/test/java/dev/compactmods/spatial/test/core/SpatialGametests.java renamed to spatial-neoforge/src/test/java/dev/compactmods/spatial/test/core/SpatialGametests.java

File renamed without changes.

spatial/src/test/java/dev/compactmods/spatial/test/core/SpatialTestMod.java renamed to spatial-neoforge/src/test/java/dev/compactmods/spatial/test/core/SpatialTestMod.java

File renamed without changes.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package dev.compactmods.spatial.test.junit;
2+
3+
import dev.compactmods.spatial.aabb.AABBHelper;
4+
import dev.compactmods.spatial.test.TestUtils;
5+
import dev.compactmods.spatial.test.junit.util.MCAssertions;
6+
import net.minecraft.core.Direction;
7+
import net.minecraft.util.RandomSource;
8+
import net.minecraft.world.phys.AABB;
9+
import net.minecraft.world.phys.Vec3;
10+
import org.junit.jupiter.api.Assertions;
11+
import org.junit.jupiter.api.DynamicTest;
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.TestFactory;
14+
15+
import java.util.Collection;
16+
import java.util.stream.Stream;
17+
18+
public class AABBHelperTests {
19+
20+
@Test
21+
public void canFloorToY0() {
22+
// Source minY = 5
23+
AABB before = AABB.ofSize(new Vec3(0, 7.5, 0), 5, 5, 5);
24+
25+
// Align to Y-0
26+
final var after = AABBHelper.alignFloor(before, 0);
27+
28+
Assertions.assertEquals(5, before.minY, "Before was modified in-place rather than immutably moved.");
29+
Assertions.assertEquals(0, after.minY, "After y level should be zero. (was: %s)".formatted(after.minY));
30+
Assertions.assertEquals(5, after.getYsize(), "AABB size was modified; should have remained the same.");
31+
}
32+
33+
@Test
34+
public void canFloorToAnotherAABB() {
35+
// Source minY = 5
36+
AABB before = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 7.5), 5, 5, 5);
37+
38+
// Target minY = 1 (bounds are Y 1-11)
39+
AABB bounds = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 6), 10, 10, 10);
40+
41+
// Align to Y-0
42+
final var after = AABBHelper.alignFloor(before, bounds);
43+
44+
Assertions.assertEquals(5, before.minY, "Before was modified in-place rather than immutably moved.");
45+
Assertions.assertEquals(1, after.minY, "After y level should be 1. (was: %s)".formatted(after.minY));
46+
47+
Assertions.assertEquals(5, after.getYsize(), "AABB size was modified; should have remained the same.");
48+
}
49+
50+
@Test
51+
public void normalizeToZero() {
52+
AABB before = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 7.5), 5, 5, 5);
53+
54+
// Align to Y-0
55+
final var after = AABBHelper.normalize(before);
56+
57+
Assertions.assertEquals(5, before.minY, "Before was modified in-place rather than immutably moved.");
58+
59+
Assertions.assertEquals(0, after.minX, "After x level was not zero (was: %s)".formatted(after.minX));
60+
Assertions.assertEquals(0, after.minY, "After y level was not zero (was: %s)".formatted(after.minY));
61+
Assertions.assertEquals(0, after.minZ, "After z level was not zero (was: %s)".formatted(after.minZ));
62+
63+
Assertions.assertEquals(5, after.getYsize(), "AABB size was modified; should have remained the same.");
64+
}
65+
66+
@TestFactory
67+
public static Stream<DynamicTest> normalizeBoundaryTests() {
68+
final var random = RandomSource.create();
69+
70+
return Stream.concat(
71+
TestUtils.randomVec3Stream(random).limit(10),
72+
73+
// Ensure at least one negative and one positive bound are part of the test
74+
Stream.of(
75+
Vec3.ZERO.subtract(-3, -2, 5),
76+
Vec3.ZERO.add(2, 5, 1)
77+
)
78+
).map(randomOffset -> DynamicTest.dynamicTest(
79+
"normalize_boundaries_%s".formatted(randomOffset.toString()),
80+
() -> normalizeIntoBoundaries(randomOffset)
81+
));
82+
}
83+
84+
private static void normalizeIntoBoundaries(Vec3 randomOffset) {
85+
AABB before = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 7.5), 5, 5, 5);
86+
AABB bounds = AABB.ofSize(randomOffset, 5, 5, 5);
87+
88+
final var after = AABBHelper.normalizeWithin(before, bounds);
89+
90+
Assertions.assertEquals(5, before.minY, "Before was modified in-place rather than immutably moved.");
91+
92+
MCAssertions.assertVec3Equals(AABBHelper.minCorner(after), AABBHelper.minCorner(bounds));
93+
94+
Assertions.assertEquals(5, after.getYsize(), "AABB size was modified; should have remained the same.");
95+
}
96+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dev.compactmods.spatial.test.junit.util;
2+
3+
import com.google.common.math.DoubleMath;
4+
import net.minecraft.world.phys.Vec3;
5+
import org.junit.jupiter.api.Assertions;
6+
7+
public class MCAssertions {
8+
public static void assertVec3Equals(Vec3 actual, Vec3 expected) {
9+
if(!DoubleMath.fuzzyEquals(actual.x, expected.x, 0.001))
10+
Assertions.fail("X did not match expected value (was: %s; expected: %s)".formatted(actual.x, expected.x));
11+
12+
if(!DoubleMath.fuzzyEquals(actual.y, expected.y, 0.001))
13+
Assertions.fail("Y did not match expected value (was: %s; expected: %s)".formatted(actual.y, expected.y));
14+
15+
if(!DoubleMath.fuzzyEquals(actual.z, expected.z, 0.001))
16+
Assertions.fail("Z did not match expected value (was: %s; expected: %s)".formatted(actual.z, expected.z));
17+
}
18+
}

spatial/src/test/resources/META-INF/neoforge.mods.toml renamed to spatial-neoforge/src/test/resources/META-INF/neoforge.mods.toml

File renamed without changes.

spatial/build.gradle.kts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,5 @@ java {
1616
}
1717

1818
neoForge {
19-
version = neoforged.versions.neoforge
20-
21-
mods {
22-
create("spatial") {
23-
modSourceSets.add(sourceSets.main)
24-
}
25-
26-
create("spatialtest") {
27-
modSourceSets.add(sourceSets.main)
28-
modSourceSets.add(sourceSets.test)
29-
}
30-
}
31-
32-
unitTest {
33-
enable()
34-
testedMod = mods.named("spatialtest")
35-
}
36-
37-
runs {
38-
create("gameTestServer") {
39-
this.type = "gameTestServer"
40-
gameDirectory.set(file("runs/gametest"))
41-
42-
systemProperty("neoforge.enabledGameTestNamespaces", "spatialtest")
43-
44-
this.sourceSet = sourceSets.test
45-
}
46-
}
47-
}
48-
49-
dependencies {
50-
testImplementation(neoforged.testframework)
19+
neoFormVersion = neoforged.versions.neoform
5120
}

0 commit comments

Comments
 (0)