mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Let snippets hold several entries
A list that references a snippet file holding a JSON array now takes all of its entries in place, in both the pack Gson and the module settings decoder. A single-value reference to such a file logs an error and loads null in the pack Gson, and is a settings error in the module decoder. Snippet-file schemas accept one entry or a list, and the decorator-snippet validator checks each entry of an array file. No pins or goldens moved. v3: 8ba8c7b2d
This commit is contained in:
1 parent
3eb8566ef9
commit
ffbd0db03a
10 files changed
+393
-25
No files matched your search
@@ -13,11 +13,15 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
@@ -85,4 +89,65 @@ public class IrisDataSnippetListReadTest {
|
||||
assertTrue(error, error.contains("$[1]"));
|
||||
assertTrue(error, error.contains("not a number"));
|
||||
}
|
||||
|
||||
@Test(timeout = READ_TIMEOUT_MILLIS)
|
||||
public void listSnippetHoldingSeveralEntriesExpandsInPlace() throws Exception {
|
||||
writeSnippet("pair", "[{\"min\": 1, \"max\": 2}, {\"min\": 3, \"max\": 4}]");
|
||||
writeSnippet("single", "{\"min\": 5, \"max\": 6}");
|
||||
|
||||
List<IrisStyledRange> ranges = data.getGson().fromJson(
|
||||
"[{\"min\": 0, \"max\": 0}, \"snippet/pair\", \"snippet/style-range/single\"]", STYLED_RANGES);
|
||||
|
||||
assertEquals(List.of(0.0, 1.0, 3.0, 5.0), ranges.stream().map(IrisStyledRange::getMin).toList());
|
||||
assertEquals(4, ranges.get(2).getMax(), 0);
|
||||
assertTrue(errors.toString(), errors.isEmpty());
|
||||
}
|
||||
|
||||
@Test(timeout = READ_TIMEOUT_MILLIS)
|
||||
public void trailingCommaInASnippetListAddsNoEntry() throws Exception {
|
||||
writeSnippet("trailing", "[{\"min\": 1, \"max\": 2}, {\"min\": 3, \"max\": 4},]");
|
||||
|
||||
List<IrisStyledRange> ranges = data.getGson().fromJson("[\"snippet/trailing\"]", STYLED_RANGES);
|
||||
|
||||
assertEquals(List.of(1.0, 3.0), ranges.stream().map(IrisStyledRange::getMin).toList());
|
||||
}
|
||||
|
||||
@Test(timeout = READ_TIMEOUT_MILLIS)
|
||||
public void missingSnippetInAListKeepsItsSlotAsNullAndNamesThePath() {
|
||||
List<IrisStyledRange> ranges = data.getGson().fromJson("[{\"min\": 1}, \"snippet/absent\"]", STYLED_RANGES);
|
||||
|
||||
assertEquals(2, ranges.size());
|
||||
assertNull(ranges.get(1));
|
||||
String error = errors.getFirst();
|
||||
assertTrue(error, error.contains("snippet/style-range/absent"));
|
||||
assertTrue(error, error.contains("$[1]"));
|
||||
}
|
||||
|
||||
@Test(timeout = READ_TIMEOUT_MILLIS)
|
||||
public void singleSlotReferencingASnippetHoldingSeveralEntriesLogsAnErrorAndLoadsNull() throws Exception {
|
||||
writeSnippet("pair", "[{\"min\": 1, \"max\": 2}, {\"min\": 3, \"max\": 4}]");
|
||||
|
||||
IrisStyledRange range = data.getGson().fromJson("\"snippet/pair\"", IrisStyledRange.class);
|
||||
|
||||
assertNull(range);
|
||||
String error = errors.getFirst();
|
||||
assertTrue(error, error.contains("snippet/pair"));
|
||||
assertTrue(error, error.contains("only a list can take"));
|
||||
}
|
||||
|
||||
@Test(timeout = READ_TIMEOUT_MILLIS)
|
||||
public void singleSlotStillReadsASnippetHoldingOneEntry() throws Exception {
|
||||
writeSnippet("single", "{\"min\": 5, \"max\": 6}");
|
||||
|
||||
IrisStyledRange range = data.getGson().fromJson("\"snippet/single\"", IrisStyledRange.class);
|
||||
|
||||
assertEquals(5, range.getMin(), 0);
|
||||
assertEquals(6, range.getMax(), 0);
|
||||
}
|
||||
|
||||
private void writeSnippet(String name, String json) throws IOException {
|
||||
File file = new File(data.getDataFolder(), "snippet/style-range/" + name + ".json");
|
||||
Files.createDirectories(file.getParentFile().toPath());
|
||||
Files.writeString(file.toPath(), json);
|
||||
}
|
||||
}
|
||||
@@ -76,6 +76,7 @@ public class ModuleSettingsDecoderTest {
|
||||
private BlockState block = new BlockState("minecraft:moss_block");
|
||||
private ResourceId palette = ResourceId.parse("iris:moss/default");
|
||||
private Patch patch = new Patch();
|
||||
private List<Patch> patches = List.of();
|
||||
}
|
||||
|
||||
@Snippet("moss-patch")
|
||||
@@ -195,6 +196,38 @@ public class ModuleSettingsDecoderTest {
|
||||
assertEquals(9, moss("{\"moss\": {\"patch\": \"snippet/moss-patch/wide\"}}").patch.size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aListTakesASnippetHoldingSeveralEntriesInPlace() throws IOException {
|
||||
Path snippets = Files.createDirectories(packFolder.resolve("snippet/moss-patch"));
|
||||
Files.writeString(snippets.resolve("pair.json"), "[{\"size\": 4}, {\"size\": 5},]");
|
||||
Files.writeString(snippets.resolve("wide.json"), "{\"size\": 9}");
|
||||
|
||||
List<Integer> sizes = moss("{\"moss\": {\"patches\": [{\"size\": 2}, \"snippet/pair\", \"snippet/moss-patch/wide\"]}}")
|
||||
.patches.stream().map(patch -> patch.size).toList();
|
||||
|
||||
assertEquals(List.of(2, 4, 5, 9), sizes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aSingleValueReferencingASnippetHoldingSeveralEntriesIsAnError() throws IOException {
|
||||
Path snippets = Files.createDirectories(packFolder.resolve("snippet/moss-patch"));
|
||||
Files.writeString(snippets.resolve("pair.json"), "[{\"size\": 4}, {\"size\": 5}]");
|
||||
|
||||
IllegalArgumentException failure = assertThrows(IllegalArgumentException.class,
|
||||
() -> moss("{\"moss\": {\"patch\": \"snippet/pair\"}}"));
|
||||
|
||||
assertTrue(failure.getMessage(), failure.getMessage().contains(
|
||||
"Snippet snippet/pair holds several entries, which only a list can take"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aMissingSnippetInAListIsAnErrorNamingTheFile() {
|
||||
IllegalArgumentException failure = assertThrows(IllegalArgumentException.class,
|
||||
() -> moss("{\"moss\": {\"patches\": [\"snippet/absent\"]}}"));
|
||||
|
||||
assertTrue(failure.getMessage(), failure.getMessage().contains("Snippet snippet/absent has no file snippet/moss-patch/absent.json"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apiValueTypesReadTheJsonIrisClassesRead() throws IOException {
|
||||
Path styles = Files.createDirectories(packFolder.resolve("snippet/style"));
|
||||
|
||||
@@ -88,6 +88,18 @@ public class PackBiomeLayerValidatorTest {
|
||||
new File(pack, "biomes"), new File(pack, "snippet/iris/decorant/decorator")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checksEveryEntryOfADecoratorSnippetHoldingSeveral() throws Exception {
|
||||
File pack = temporaryFolder.newFolder("pack");
|
||||
write(pack, "snippet/iris/decorant/decorator/several.json",
|
||||
"[{\"palette\":[{\"block\":\"minecraft:magma_block\"}]}, {\"chance\":0.5}]");
|
||||
|
||||
assertEquals(List.of(
|
||||
"Decorator snippet 'several'[1] must declare a non-empty palette."
|
||||
), PackBiomeLayerValidator.validateDecoratorPalettes(
|
||||
new File(pack, "biomes"), new File(pack, "snippet/iris/decorant/decorator")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void acceptsDecoratorPalettesAndSnippetReferences() throws Exception {
|
||||
File pack = temporaryFolder.newFolder("pack");
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package art.arcane.iris.core.project;
|
||||
|
||||
import art.arcane.iris.engine.object.world.IrisWorldBoundary;
|
||||
import art.arcane.volmlib.util.json.JSONArray;
|
||||
import art.arcane.volmlib.util.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class SnippetFileSchemaTest {
|
||||
@Test
|
||||
public void snippetFileSchemaTakesOneEntryOrAListOfThem() {
|
||||
JSONObject entry = new SchemaBuilder(IrisWorldBoundary.class, null).construct();
|
||||
JSONObject schema = new SchemaBuilder(IrisWorldBoundary.class, null).constructSnippet();
|
||||
|
||||
JSONArray anyOf = schema.getJSONArray("anyOf");
|
||||
assertEquals(2, anyOf.length());
|
||||
assertEquals("object", anyOf.getJSONObject(0).getString("type"));
|
||||
assertEquals("array", anyOf.getJSONObject(1).getString("type"));
|
||||
assertEquals(anyOf.getJSONObject(0).toString(), anyOf.getJSONObject(1).getJSONObject("items").toString());
|
||||
assertEquals(entry.getJSONObject("properties").keySet(), anyOf.getJSONObject(0).getJSONObject("properties").keySet());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snippetFileSchemaKeepsTheFileLevelKeysAtTheRoot() {
|
||||
JSONObject schema = new SchemaBuilder(IrisWorldBoundary.class, null).constructSnippet();
|
||||
|
||||
assertTrue(schema.has("$schema"));
|
||||
assertTrue(schema.has("$id"));
|
||||
assertTrue(schema.has("definitions"));
|
||||
JSONObject single = schema.getJSONArray("anyOf").getJSONObject(0);
|
||||
assertFalse(single.has("$schema"));
|
||||
assertFalse(single.has("$id"));
|
||||
assertFalse(single.has("definitions"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user