mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-30 17:54:56 +00:00
Register structure list, info and verify through the tooling api
StructureModelModule mounts /iris structure list|info|verify as a module Command: list writes the structure index through ToolPack.edit, info takes the structure as a pack resource, and verify reads the world the sender stands in and reports in plain text. The tests run list, info and verify through the dispatcher and pin that no other module can take the structure root name or its aliases.
This commit is contained in:
1 parent
bcec8e474d
commit
a5d0229417
6 files changed
+535
No files matched your search
+43
@@ -0,0 +1,43 @@
|
||||
package art.arcane.iris.core.modules;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class StructureVerificationContractTest {
|
||||
private static String verificationSource() throws IOException {
|
||||
return Files.readString(Path.of("src/main/java/art/arcane/iris/core/modules/StructureVerification.java"))
|
||||
.replace("\r\n", "\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reportsDensityLimitAndUsesExactLocatedOrigin() throws IOException {
|
||||
String source = verificationSource();
|
||||
|
||||
assertTrue(source.contains("[iris-search-limit]"));
|
||||
assertTrue(source.contains("density searches safety-limited"));
|
||||
assertTrue(source.contains("result.x() + \",\" + result.y() + \",\" + result.z()"));
|
||||
assertFalse(source.contains("at[0] + 8"));
|
||||
assertFalse(source.contains("at[2] + 8"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void partitionsPolicyBeforeNativeReachability() throws IOException {
|
||||
String source = verificationSource();
|
||||
int methodStart = source.indexOf("private Set<String> reachableNativeKeys(");
|
||||
int methodEnd = source.indexOf("private void verify(", methodStart);
|
||||
String method = source.substring(methodStart, methodEnd);
|
||||
int placedCheck = method.indexOf("!ModuleServices.placesStructure(engine, key)");
|
||||
int policyResolution = method.indexOf("NativeStructureGenerationPolicy.resolve(engine, key) == NativeStructureGenerationStatus.GENERATE_NATIVE", placedCheck);
|
||||
int reachabilityLookup = method.indexOf("StructureReachability.reachableKeys(engine)", policyResolution);
|
||||
|
||||
assertTrue(placedCheck >= 0);
|
||||
assertTrue(policyResolution > placedCheck);
|
||||
assertTrue(reachabilityLookup > policyResolution);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package art.arcane.iris.core.tooling;
|
||||
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.modules.ModuleResourceFixture;
|
||||
import art.arcane.iris.core.modules.StructureModelModule;
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.ModuleSetup;
|
||||
import com.volmit.tooling.Command;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class CommandRootCollisionTest {
|
||||
private static IrisModule registering(String id, Command root) {
|
||||
return new IrisModule() {
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of(id, id, "1");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup(ModuleSetup setup) {
|
||||
setup.tooling().registerCommand(root);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aModuleCannotTakeTheRootNameOfTheStructureModule() {
|
||||
var collision = assertThrows(IllegalArgumentException.class, () -> ModuleResourceFixture.catalogOf(
|
||||
new StructureModelModule(), registering("test:rival", Command.named("structure"))));
|
||||
|
||||
assertTrue(collision.getMessage(), collision.getMessage().contains("Command root structure is registered by module"));
|
||||
assertTrue(collision.getMessage(), collision.getMessage().contains("iris:structure-model"));
|
||||
assertTrue(collision.getMessage(), collision.getMessage().contains("test:rival"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aModuleCannotTakeAnAliasOfTheStructureModule() {
|
||||
var collision = assertThrows(IllegalArgumentException.class, () -> ModuleResourceFixture.catalogOf(
|
||||
registering("test:rival", Command.named("shapes").alias("str")), new StructureModelModule()));
|
||||
|
||||
assertTrue(collision.getMessage(), collision.getMessage().contains("Command root str is registered by module"));
|
||||
assertTrue(collision.getMessage(), collision.getMessage().contains("test:rival"));
|
||||
assertTrue(collision.getMessage(), collision.getMessage().contains("iris:structure-model"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aPackWhoseModulesCollideAnswersEveryLineWithTheReason() {
|
||||
IrisData data = mock(IrisData.class);
|
||||
when(data.getDataFolder()).thenReturn(new File("clashing"));
|
||||
when(data.getModules()).thenThrow(new IllegalArgumentException("Command root structure is registered by module "
|
||||
+ "iris:structure-model already, and module test:rival registers it too"));
|
||||
ServedPack clashing = new IrisServedPack(data);
|
||||
RecordingSender sender = new RecordingSender();
|
||||
sender.studio = Optional.of(clashing);
|
||||
ToolingCommands dispatcher = new ToolingCommands(new PackDirectory() {
|
||||
@Override
|
||||
public Optional<ServedPack> find(String name) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> names() {
|
||||
return List.of();
|
||||
}
|
||||
}, Runnable::run, Optional::empty);
|
||||
|
||||
assertTrue(dispatcher.execute(sender, List.of("structure", "list")));
|
||||
|
||||
assertEquals(1, sender.errors.size());
|
||||
assertTrue(sender.errors.get(0), sender.errors.get(0).startsWith("The modules of pack clashing did not load: Command root structure"));
|
||||
assertTrue(sender.errors.get(0), sender.errors.get(0).contains("test:rival"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package art.arcane.iris.core.tooling;
|
||||
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.modules.ModuleResourceFixture;
|
||||
import art.arcane.iris.core.modules.StructureModelModule;
|
||||
import art.arcane.iris.engine.framework.structure.StructureResources;
|
||||
import art.arcane.iris.engine.mantle.components.StructureModule;
|
||||
import art.arcane.iris.engine.object.structure.IrisStructure;
|
||||
import art.arcane.iris.spi.IrisPlatform;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.spi.PlatformStructureHooks;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class StructureCommandsTest {
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
private IrisPlatform previousPlatform;
|
||||
private File packFolder;
|
||||
private IrisData data;
|
||||
private ToolingCommands dispatcher;
|
||||
private final RecordingSender sender = new RecordingSender();
|
||||
|
||||
@Before
|
||||
public void aPackServingTheStructureModule() throws Exception {
|
||||
previousPlatform = IrisPlatforms.isBound() ? IrisPlatforms.get() : null;
|
||||
IrisPlatforms.unbind();
|
||||
IrisPlatform platform = mock(IrisPlatform.class);
|
||||
PlatformStructureHooks hooks = mock(PlatformStructureHooks.class);
|
||||
when(platform.structureHooks()).thenReturn(hooks);
|
||||
when(hooks.structureKeys()).thenReturn(List.of("minecraft:village", "example:castle"));
|
||||
when(hooks.structureSetKeys()).thenReturn(List.of("minecraft:villages"));
|
||||
IrisPlatforms.bind(platform);
|
||||
|
||||
packFolder = temporaryFolder.newFolder("overworld");
|
||||
data = mock(IrisData.class);
|
||||
ModuleResourceFixture.serveModules(data, new StructureModelModule(), new StructureModule());
|
||||
when(data.getDataFolder()).thenReturn(packFolder);
|
||||
IrisStructure emptyCastle = new IrisStructure();
|
||||
emptyCastle.setStartPool("ghost-pool");
|
||||
ModuleResourceFixture.serve(data).resource(StructureResources.STRUCTURES, "empty-castle", emptyCastle);
|
||||
when(data.getPackResources().require(StructureResources.STRUCTURES, new ResourceId("test", "empty-castle")))
|
||||
.thenReturn(emptyCastle);
|
||||
|
||||
ServedPack served = new IrisServedPack(data);
|
||||
dispatcher = new ToolingCommands(new PackDirectory() {
|
||||
@Override
|
||||
public Optional<ServedPack> find(String name) {
|
||||
return name.equals("overworld") ? Optional.of(served) : Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> names() {
|
||||
return List.of("overworld");
|
||||
}
|
||||
}, Runnable::run, Optional::empty);
|
||||
}
|
||||
|
||||
@After
|
||||
public void restorePlatform() {
|
||||
IrisPlatforms.unbind();
|
||||
if (previousPlatform != null) {
|
||||
IrisPlatforms.bind(previousPlatform);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean run(String line) {
|
||||
return dispatcher.execute(sender, List.of(line.split(" ")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listWritesTheStructureIndexThroughAnEditAndReloadsOnce() throws Exception {
|
||||
assertTrue(run("structure list pack=overworld"));
|
||||
|
||||
File index = new File(packFolder, ".iris/structure-index.json");
|
||||
assertTrue(index.isFile());
|
||||
assertTrue(Files.readString(index.toPath()).contains("example:castle"));
|
||||
assertEquals(List.of("Wrote the structure index: " + index.getPath()), sender.successes);
|
||||
assertTrue(sender.errors.isEmpty());
|
||||
verify(data, times(1)).invalidateStructureResources();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void theAliasesOfTheRootAndTheCommandAnswerToo() {
|
||||
assertTrue(run("str ls pack=overworld"));
|
||||
|
||||
assertEquals(1, sender.successes.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void infoNamesTheStructureByTheResourcesOfThePack() {
|
||||
assertTrue(run("structure info nowhere pack=overworld"));
|
||||
|
||||
assertEquals(List.of("No structures resource 'nowhere' in this pack"), sender.errors);
|
||||
assertEquals(List.of("Use: structure info <structure>"), sender.infos);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void infoAssemblesTheStructureAndReportsAnInvalidGraphAsTheCommandsFailure() {
|
||||
assertTrue(run("structure info empty-castle pack=overworld"));
|
||||
|
||||
assertEquals(1, sender.errors.size());
|
||||
assertTrue(sender.errors.get(0), sender.errors.get(0).startsWith("structure info failed: "));
|
||||
assertTrue(sender.errors.get(0), sender.errors.get(0).contains("references missing start pool 'ghost-pool'"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyNeedsTheWorldOfTheSender() {
|
||||
assertTrue(run("structure verify pack=overworld"));
|
||||
|
||||
assertEquals(List.of("Stand in a world generated with this pack: verify reads the running world"), sender.errors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void theGroupListsTheThreeCommands() {
|
||||
assertTrue(run("structure pack=overworld"));
|
||||
|
||||
assertEquals(4, sender.infos.size());
|
||||
assertTrue(sender.infos.get(1).startsWith(" structure list"));
|
||||
assertTrue(sender.infos.get(2).startsWith(" structure info <structure>"));
|
||||
assertTrue(sender.infos.get(3).startsWith(" structure verify [radius]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void completionOffersTheStructuresOfThePack() {
|
||||
when(data.getPackResources().packId()).thenReturn(new ResourceId("test", "overworld"));
|
||||
|
||||
assertEquals(List.of("empty-castle"), dispatcher.complete(sender, List.of("structure", "info", "pack=overworld", "e")));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user