From 6744f5b04cb12d8140267d950217a015568767b3 Mon Sep 17 00:00:00 2001 From: Algent Date: Sat, 5 Sep 2026 00:34:18 +0200 Subject: [PATCH] test: characterize tile occlusion queries --- .../test/ForgeEnvironmentSmokeTest.java | 2 + .../multipart/TileMultipartOcclusionTest.java | 122 ++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/test/java/codechicken/multipart/TileMultipartOcclusionTest.java diff --git a/src/functionalTest/java/codechicken/multipart/test/ForgeEnvironmentSmokeTest.java b/src/functionalTest/java/codechicken/multipart/test/ForgeEnvironmentSmokeTest.java index 36ae91a..1da29eb 100644 --- a/src/functionalTest/java/codechicken/multipart/test/ForgeEnvironmentSmokeTest.java +++ b/src/functionalTest/java/codechicken/multipart/test/ForgeEnvironmentSmokeTest.java @@ -583,6 +583,8 @@ class ForgeEnvironmentSmokeTest { tile.getClass().getMethod("partialOcclusionTest", scala.collection.Seq.class).getReturnType()); assertTrue(tile.canAddPart(new PartialPart(voxel(1)))); assertFalse(tile.canAddPart(new PartialPart(voxel(0)))); + assertTrue(tile.occlusionTest(tile.partList(), new PartialPart(voxel(1)))); + assertFalse(tile.occlusionTest(tile.partList(), new PartialPart(voxel(0)))); } @Test diff --git a/src/test/java/codechicken/multipart/TileMultipartOcclusionTest.java b/src/test/java/codechicken/multipart/TileMultipartOcclusionTest.java new file mode 100644 index 0000000..61453cb --- /dev/null +++ b/src/test/java/codechicken/multipart/TileMultipartOcclusionTest.java @@ -0,0 +1,122 @@ +package codechicken.multipart; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import scala.collection.JavaConversions; +import scala.collection.Seq; + +/** Pair ordering, failures and the tile-level override hook behind collection occlusion queries. */ +class TileMultipartOcclusionTest { + + @Test + void legacyQueryChecksBothDirectionsInOrderAndStopsAtEitherRejection() { + TileMultipart tile = new TileMultipart(); + List calls = new ArrayList<>(); + Part first = new Part("first", calls); + Part second = new Part("second", calls); + Part candidate = new Part("candidate", calls); + assertTrue(tile.occlusionTest(seq(first, second), candidate)); + assertEquals( + Arrays.asList("first:candidate", "candidate:first", "second:candidate", "candidate:second"), + calls); + assertNull(first.tile(), "Unbound parts participate in geometry queries"); + calls.clear(); + first.accepts = false; + assertFalse(tile.occlusionTest(seq(first, second), candidate)); + assertEquals(Arrays.asList("first:candidate"), calls); + calls.clear(); + first.accepts = true; + candidate.accepts = false; + assertFalse(tile.occlusionTest(seq(first, second), candidate)); + assertEquals(Arrays.asList("first:candidate", "candidate:first"), calls); + } + + @Test + void legacyQueryRetainsLazyNullChecksAndOriginalCallbackFailures() { + TileMultipart tile = new TileMultipart(); + List calls = new ArrayList<>(); + Part existing = new Part("existing", calls); + Part candidate = new Part("candidate", calls); + assertTrue(tile.occlusionTest(seq(), null)); + assertThrows(NullPointerException.class, () -> tile.occlusionTest(null, candidate)); + existing.accepts = false; + assertFalse(tile.occlusionTest(seq(existing, null), null)); + assertEquals(Arrays.asList("existing:null"), calls); + existing.accepts = true; + calls.clear(); + IllegalStateException failure = new IllegalStateException("candidate callback"); + candidate.failure = failure; + assertSame( + failure, + assertThrows(IllegalStateException.class, () -> tile.occlusionTest(seq(existing, null), candidate))); + assertEquals(Arrays.asList("existing:candidate", "candidate:existing"), calls); + } + + @Test + void replacementChecksKeepTheLegacyTileOverrideAndExcludeTheOutgoingPart() { + List calls = new ArrayList<>(); + List> inputs = new ArrayList<>(); + TileMultipart tile = new TileMultipart() { + + @Override + public boolean occlusionTest(Seq parts, TMultiPart candidate) { + inputs.add(parts); + return false; + } + }; + Part outgoing = new Part("outgoing", calls); + Part retained = new Part("retained", calls); + Part candidate = new Part("candidate", calls); + tile.partList_$eq(seq(outgoing, retained)); + assertFalse(tile.canReplacePart(outgoing, candidate)); + assertEquals(Arrays.asList(seq(retained)), inputs); + assertTrue(calls.isEmpty(), "The override can reject without invoking pair callbacks"); + assertEquals(Arrays.asList(outgoing, retained), tile.jPartList()); + } + + private static Seq seq(TMultiPart... parts) { + return JavaConversions.asScalaBuffer(Arrays.asList(parts)).toList(); + } + + private static class Part extends TMultiPart { + + private final String name; + private final List calls; + private boolean accepts = true; + private RuntimeException failure; + private Runnable onTest; + + private Part(String name, List calls) { + this.name = name; + this.calls = calls; + } + + @Override + public String getType() { + return name; + } + + @Override + public boolean occlusionTest(TMultiPart other) { + calls.add(name + ":" + (other == null ? "null" : other.getType())); + if (onTest != null) { + onTest.run(); + } + if (failure != null) { + throw failure; + } + return accepts; + } + } +}