mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-30 01:34:04 +00:00
Add list snippets (#1271)
- Expand JSON array snippet files into list entries - Read snippet values as JSON trees and skip malformed ones once - Route snippet references through one SnippetFolder lookup - Reject references that leave snippet/<type>/, including symlinks
This commit is contained in:
1 parent
fc5db87514
commit
6e078ad4a0
14 files changed
+857
-112
No files matched your search
+111
@@ -0,0 +1,111 @@
|
||||
package art.arcane.iris.pack.loading;
|
||||
|
||||
import art.arcane.iris.configuration.IrisSettings;
|
||||
import art.arcane.iris.generation.runtime.PreservationRegistry;
|
||||
import art.arcane.iris.pack.value.IrisRange;
|
||||
import art.arcane.iris.spi.IrisServices;
|
||||
import art.arcane.iris.testsupport.PlatformLeakGuard;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
public class IrisDataSnippetMalformedValueTest {
|
||||
private static final long RUNAWAY_READ_TIMEOUT_MILLISECONDS = 20_000;
|
||||
private static final double DEFAULT_RANGE_MIN = 16;
|
||||
private static final double DEFAULT_RANGE_MAX = 32;
|
||||
|
||||
@ClassRule
|
||||
public static final PlatformLeakGuard PLATFORM_GUARD = PlatformLeakGuard.clean();
|
||||
|
||||
@Rule
|
||||
public final TemporaryFolder temporary = new TemporaryFolder();
|
||||
|
||||
private IrisSettings previousSettings;
|
||||
private Path pack;
|
||||
private IrisData data;
|
||||
|
||||
public static class RangeListHolder {
|
||||
public List<IrisRange> ranges;
|
||||
public KList<IrisRange> keyedRanges;
|
||||
}
|
||||
|
||||
public static class RangeHolder {
|
||||
public IrisRange range;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void createData() throws IOException {
|
||||
previousSettings = IrisSettings.settings;
|
||||
IrisSettings.settings = new IrisSettings();
|
||||
IrisServices.register(PreservationRegistry.class, mock(PreservationRegistry.class));
|
||||
pack = temporary.newFolder("pack").toPath();
|
||||
data = IrisData.get(new File(pack.toFile(), "."));
|
||||
}
|
||||
|
||||
@After
|
||||
public void closeData() {
|
||||
data.close();
|
||||
IrisServices.remove(PreservationRegistry.class);
|
||||
IrisSettings.settings = previousSettings;
|
||||
}
|
||||
|
||||
@Test(timeout = RUNAWAY_READ_TIMEOUT_MILLISECONDS)
|
||||
public void numberInAListOfSnippetTypesIsReplacedByTheDefaultAndReadingMovesOn() {
|
||||
RangeListHolder holder = data.getGson().fromJson(
|
||||
"{\"ranges\":[5,{\"min\":1,\"max\":2}],\"keyedRanges\":[true]}", RangeListHolder.class);
|
||||
|
||||
assertEquals(2, holder.ranges.size());
|
||||
assertDefaultRange(holder.ranges.get(0));
|
||||
assertEquals(1, holder.ranges.get(1).getMin(), 0);
|
||||
assertEquals(1, holder.keyedRanges.size());
|
||||
assertDefaultRange(holder.keyedRanges.get(0));
|
||||
}
|
||||
|
||||
@Test(timeout = RUNAWAY_READ_TIMEOUT_MILLISECONDS)
|
||||
public void nestedArrayInAListOfSnippetTypesIsReplacedByTheDefault() {
|
||||
RangeListHolder holder = data.getGson().fromJson("{\"ranges\":[[1,2],[[3]]]}", RangeListHolder.class);
|
||||
|
||||
assertEquals(2, holder.ranges.size());
|
||||
assertDefaultRange(holder.ranges.get(0));
|
||||
assertDefaultRange(holder.ranges.get(1));
|
||||
}
|
||||
|
||||
@Test(timeout = RUNAWAY_READ_TIMEOUT_MILLISECONDS)
|
||||
public void malformedFieldKeepsTheDefaultAndTheValuesAfterIt() {
|
||||
RangeHolder holder = data.getGson().fromJson("{\"range\":7}", RangeHolder.class);
|
||||
|
||||
assertDefaultRange(holder.range);
|
||||
}
|
||||
|
||||
@Test(timeout = RUNAWAY_READ_TIMEOUT_MILLISECONDS)
|
||||
public void wrongTypedEntryOfAListSnippetIsReplacedByTheDefault() throws IOException {
|
||||
Path snippet = pack.resolve("snippet/range/broken.json");
|
||||
Files.createDirectories(snippet.getParent());
|
||||
Files.writeString(snippet, "[[1]]");
|
||||
|
||||
RangeListHolder holder = data.getGson().fromJson("{\"ranges\":[\"snippet/broken\"]}", RangeListHolder.class);
|
||||
|
||||
assertEquals(1, holder.ranges.size());
|
||||
assertDefaultRange(holder.ranges.get(0));
|
||||
}
|
||||
|
||||
private static void assertDefaultRange(IrisRange range) {
|
||||
assertNotNull(range);
|
||||
assertEquals(DEFAULT_RANGE_MIN, range.getMin(), 0);
|
||||
assertEquals(DEFAULT_RANGE_MAX, range.getMax(), 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
package art.arcane.iris.pack.loading;
|
||||
|
||||
import art.arcane.iris.generation.biome.IrisBiome;
|
||||
import art.arcane.iris.generation.noise.IrisExpression;
|
||||
import art.arcane.iris.generation.noise.IrisGenerator;
|
||||
import art.arcane.iris.generation.terrain.IrisDimension;
|
||||
import art.arcane.iris.generation.terrain.IrisRegion;
|
||||
import art.arcane.iris.pack.mod.IrisMod;
|
||||
import art.arcane.iris.pack.schema.annotation.Snippet;
|
||||
import art.arcane.iris.structure.jigsaw.IrisJigsawPiece;
|
||||
import art.arcane.iris.structure.jigsaw.IrisJigsawPool;
|
||||
import art.arcane.iris.structure.placement.IrisStructure;
|
||||
import art.arcane.iris.world.entity.IrisEntity;
|
||||
import art.arcane.iris.world.entity.IrisMarker;
|
||||
import art.arcane.iris.world.entity.IrisSpawner;
|
||||
import art.arcane.iris.world.loot.IrisLootTable;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assume.assumeTrue;
|
||||
|
||||
/**
|
||||
* Walks every snippet reference of a real pack, set through IRIS_TEST_PACK, and checks it resolves for the type of the
|
||||
* field it sits in. Reads the pack only; skipped when the variable is unset.
|
||||
*/
|
||||
public class IrisDataSnippetPackWalkTest {
|
||||
private static final String PACK_ENVIRONMENT_VARIABLE = "IRIS_TEST_PACK";
|
||||
private static final String ENGINE_PACKAGE_PREFIX = "art.arcane.iris";
|
||||
private static final List<Class<? extends IrisRegistrant>> PACK_ROOT_TYPES = List.of(
|
||||
IrisBiome.class, IrisRegion.class, IrisDimension.class, IrisGenerator.class, IrisEntity.class,
|
||||
IrisLootTable.class, IrisSpawner.class, IrisMarker.class, IrisExpression.class, IrisMod.class,
|
||||
IrisStructure.class, IrisJigsawPool.class, IrisJigsawPiece.class);
|
||||
|
||||
private File pack;
|
||||
private final Map<String, Class<?>> snippetTypesByFolder = new HashMap<>();
|
||||
private final List<String> unresolved = new ArrayList<>();
|
||||
private int resolved;
|
||||
private int outsideSnippetFields;
|
||||
private int snippetFilesWalked;
|
||||
private final List<String> untypedSnippetFiles = new ArrayList<>();
|
||||
|
||||
@Test
|
||||
public void everySnippetReferenceInThePackResolves() throws Exception {
|
||||
String packPath = System.getenv(PACK_ENVIRONMENT_VARIABLE);
|
||||
assumeTrue(PACK_ENVIRONMENT_VARIABLE + " is not set", packPath != null && !packPath.isBlank());
|
||||
pack = new File(packPath);
|
||||
assumeTrue(pack + " is not a folder", pack.isDirectory());
|
||||
|
||||
Set<Class<?>> visitedTypes = new HashSet<>();
|
||||
PACK_ROOT_TYPES.forEach(root -> collectSnippetTypes(root, visitedTypes));
|
||||
for (Class<? extends IrisRegistrant> root : PACK_ROOT_TYPES) {
|
||||
walkRootFolder(root);
|
||||
}
|
||||
walkSnippetFiles();
|
||||
|
||||
System.out.println("snippet pack walk: resolved=" + resolved + " unresolved=" + unresolved.size()
|
||||
+ " outsideSnippetFields=" + outsideSnippetFields + " snippetFilesWalked=" + snippetFilesWalked
|
||||
+ " untypedSnippetFiles=" + untypedSnippetFiles.size());
|
||||
untypedSnippetFiles.forEach(file -> System.out.println("untyped snippet file: " + file));
|
||||
assertTrue("unresolved snippet references:\n" + String.join("\n", unresolved), unresolved.isEmpty());
|
||||
assertTrue("the pack holds no snippet references", resolved > 0);
|
||||
}
|
||||
|
||||
private void walkRootFolder(Class<? extends IrisRegistrant> root) throws Exception {
|
||||
String folderName = root.getConstructor().newInstance().getFolderName();
|
||||
for (File file : jsonFilesUnder(new File(pack, folderName))) {
|
||||
walk(parse(file), root, relative(file));
|
||||
}
|
||||
}
|
||||
|
||||
private void walkSnippetFiles() throws IOException {
|
||||
for (File file : jsonFilesUnder(new File(pack, "snippet"))) {
|
||||
Class<?> type = typeOfSnippetFile(file);
|
||||
if (type == null) {
|
||||
untypedSnippetFiles.add(relative(file));
|
||||
continue;
|
||||
}
|
||||
snippetFilesWalked++;
|
||||
JsonElement content = parse(file);
|
||||
if (content != null && content.isJsonArray()) {
|
||||
content.getAsJsonArray().forEach(entry -> walk(entry, type, relative(file)));
|
||||
} else {
|
||||
walk(content, type, relative(file));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Class<?> typeOfSnippetFile(File file) {
|
||||
String[] segments = relative(file).split("/");
|
||||
return snippetTypesByFolder.get(segments[1]);
|
||||
}
|
||||
|
||||
private void walk(JsonElement value, Type type, String path) {
|
||||
if (value == null || value.isJsonNull()) {
|
||||
return;
|
||||
}
|
||||
Class<?> raw = rawClass(type);
|
||||
if (value.isJsonPrimitive()) {
|
||||
checkReference(value, raw, path);
|
||||
return;
|
||||
}
|
||||
if (value.isJsonArray()) {
|
||||
walkElements(value.getAsJsonArray(), elementTypeOf(type), path);
|
||||
return;
|
||||
}
|
||||
walkObject(value.getAsJsonObject(), type, raw, path);
|
||||
}
|
||||
|
||||
private void walkElements(JsonArray elements, Type elementType, String path) {
|
||||
for (int index = 0; index < elements.size(); index++) {
|
||||
walk(elements.get(index), elementType, path + "[" + index + "]");
|
||||
}
|
||||
}
|
||||
|
||||
private void walkObject(JsonObject object, Type type, Class<?> raw, String path) {
|
||||
if (raw == null) {
|
||||
return;
|
||||
}
|
||||
if (Map.class.isAssignableFrom(raw)) {
|
||||
Type valueType = typeArgument(type, 1);
|
||||
object.entrySet().forEach(entry -> walk(entry.getValue(), valueType, path + "." + entry.getKey()));
|
||||
return;
|
||||
}
|
||||
Map<String, Type> fields = fieldTypesOf(raw);
|
||||
object.entrySet().forEach(entry -> {
|
||||
Type fieldType = fields.get(entry.getKey());
|
||||
if (fieldType != null) {
|
||||
walk(entry.getValue(), fieldType, path + "." + entry.getKey());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void checkReference(JsonElement value, Class<?> raw, String path) {
|
||||
if (!value.getAsJsonPrimitive().isString() || !value.getAsString().startsWith(SnippetFolder.PREFIX)) {
|
||||
return;
|
||||
}
|
||||
String reference = value.getAsString();
|
||||
SnippetFolder folder = raw == null ? null : folderOf(raw);
|
||||
if (folder == null) {
|
||||
outsideSnippetFields++;
|
||||
return;
|
||||
}
|
||||
if (folder.find(pack, reference).found()) {
|
||||
resolved++;
|
||||
return;
|
||||
}
|
||||
unresolved.add(path + " -> " + reference + " (tried " + folder.keyOf(reference) + ")");
|
||||
}
|
||||
|
||||
private SnippetFolder folderOf(Class<?> type) {
|
||||
Snippet snippet = type.getDeclaredAnnotation(Snippet.class);
|
||||
return snippet == null ? null : new SnippetFolder(snippet.value());
|
||||
}
|
||||
|
||||
private void collectSnippetTypes(Type type, Set<Class<?>> visited) {
|
||||
if (type instanceof ParameterizedType parameterized) {
|
||||
for (Type argument : parameterized.getActualTypeArguments()) {
|
||||
collectSnippetTypes(argument, visited);
|
||||
}
|
||||
}
|
||||
Class<?> raw = rawClass(type);
|
||||
if (raw == null) {
|
||||
return;
|
||||
}
|
||||
if (raw.isArray()) {
|
||||
collectSnippetTypes(raw.getComponentType(), visited);
|
||||
return;
|
||||
}
|
||||
if (!raw.getName().startsWith(ENGINE_PACKAGE_PREFIX) || raw.isEnum() || raw.isInterface() || !visited.add(raw)) {
|
||||
return;
|
||||
}
|
||||
Snippet snippet = raw.getDeclaredAnnotation(Snippet.class);
|
||||
if (snippet != null) {
|
||||
snippetTypesByFolder.putIfAbsent(snippet.value(), raw);
|
||||
}
|
||||
fieldTypesOf(raw).values().forEach(fieldType -> collectSnippetTypes(fieldType, visited));
|
||||
}
|
||||
|
||||
private static Map<String, Type> fieldTypesOf(Class<?> type) {
|
||||
Map<String, Type> fields = new HashMap<>();
|
||||
for (Class<?> current = type; current != null && current != Object.class; current = current.getSuperclass()) {
|
||||
for (Field field : current.getDeclaredFields()) {
|
||||
if (!Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers())) {
|
||||
fields.putIfAbsent(field.getName(), field.getGenericType());
|
||||
}
|
||||
}
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
private static Class<?> rawClass(Type type) {
|
||||
if (type instanceof Class<?> raw) {
|
||||
return raw;
|
||||
}
|
||||
if (type instanceof ParameterizedType parameterized && parameterized.getRawType() instanceof Class<?> raw) {
|
||||
return raw;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Type elementTypeOf(Type type) {
|
||||
if (type instanceof Class<?> raw && raw.isArray()) {
|
||||
return raw.getComponentType();
|
||||
}
|
||||
return typeArgument(type, 0);
|
||||
}
|
||||
|
||||
private static Type typeArgument(Type type, int index) {
|
||||
if (type instanceof ParameterizedType parameterized && index < parameterized.getActualTypeArguments().length) {
|
||||
return parameterized.getActualTypeArguments()[index];
|
||||
}
|
||||
return Object.class;
|
||||
}
|
||||
|
||||
private static JsonElement parse(File file) {
|
||||
try {
|
||||
return JsonParser.parseString(Files.readString(file.toPath()));
|
||||
} catch (Exception unreadable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static List<File> jsonFilesUnder(File folder) {
|
||||
if (!folder.isDirectory()) {
|
||||
return List.of();
|
||||
}
|
||||
try (Stream<Path> files = Files.walk(folder.toPath())) {
|
||||
return files.filter(Files::isRegularFile)
|
||||
.filter(file -> file.getFileName().toString().endsWith(".json"))
|
||||
.map(Path::toFile)
|
||||
.sorted()
|
||||
.toList();
|
||||
} catch (IOException unreadable) {
|
||||
throw new IllegalStateException(unreadable);
|
||||
}
|
||||
}
|
||||
|
||||
private String relative(File file) {
|
||||
return pack.toPath().relativize(file.toPath()).toString().replace('\\', '/');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
package art.arcane.iris.pack.loading;
|
||||
|
||||
import art.arcane.iris.configuration.IrisSettings;
|
||||
import art.arcane.iris.generation.runtime.PreservationRegistry;
|
||||
import art.arcane.iris.pack.value.IrisRate;
|
||||
import art.arcane.iris.spi.IrisServices;
|
||||
import art.arcane.iris.testsupport.PlatformLeakGuard;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
public class IrisDataSnippetTest {
|
||||
@ClassRule
|
||||
public static final PlatformLeakGuard PLATFORM_GUARD = PlatformLeakGuard.clean();
|
||||
|
||||
@Rule
|
||||
public final TemporaryFolder temporary = new TemporaryFolder();
|
||||
|
||||
private IrisSettings previousSettings;
|
||||
private Path pack;
|
||||
private IrisData data;
|
||||
|
||||
public static class RateHolder {
|
||||
public IrisRate rate;
|
||||
}
|
||||
|
||||
public static class RateListHolder {
|
||||
public List<IrisRate> rates;
|
||||
public KList<IrisRate> keyedRates;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void createPack() throws IOException {
|
||||
previousSettings = IrisSettings.settings;
|
||||
IrisSettings.settings = new IrisSettings();
|
||||
IrisServices.register(PreservationRegistry.class, mock(PreservationRegistry.class));
|
||||
pack = temporary.newFolder("pack").toPath();
|
||||
}
|
||||
|
||||
@After
|
||||
public void closeData() {
|
||||
if (data != null) {
|
||||
data.close();
|
||||
}
|
||||
IrisServices.remove(PreservationRegistry.class);
|
||||
IrisSettings.settings = previousSettings;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedPathResolves() throws IOException {
|
||||
write("snippet/rate/groups/fast.json", "{\"amount\":8}");
|
||||
|
||||
assertEquals(8, rateOf("\"snippet/groups/fast\"").getAmount());
|
||||
assertEquals(8, rateOf("\"snippet/rate/groups/fast\"").getAmount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shortAndFullReferencesResolve() throws IOException {
|
||||
write("snippet/rate/slow.json", "{\"amount\":3}");
|
||||
|
||||
assertEquals(3, rateOf("\"snippet/slow\"").getAmount());
|
||||
assertEquals(3, rateOf("\"snippet/rate/slow\"").getAmount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snippetsOfAnotherTypeAreNotUsed() throws IOException {
|
||||
write("snippet/duration/fast.json", "{\"amount\":7}");
|
||||
|
||||
assertNull(rateOf("\"snippet/fast\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void referenceCannotLeaveItsSnippetFolder() throws IOException {
|
||||
write("snippet/duration/fast.json", "{\"amount\":7}");
|
||||
write("snippet/rate/other.json", "{\"amount\":1}");
|
||||
|
||||
assertNull(rateOf("\"snippet/rate/../duration/fast\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void symlinkLeadingOutOfItsSnippetFolderIsNotFound() throws IOException {
|
||||
write("snippet/duration/fast.json", "{\"amount\":7}");
|
||||
link("snippet/rate/fast.json", "snippet/duration/fast.json");
|
||||
|
||||
assertNull(rateOf("\"snippet/fast\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void symlinkStayingInsideItsSnippetFolderResolves() throws IOException {
|
||||
write("snippet/rate/real.json", "{\"amount\":4}");
|
||||
link("snippet/rate/alias.json", "snippet/rate/real.json");
|
||||
|
||||
assertEquals(4, rateOf("\"snippet/alias\"").getAmount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void missingSnippetYieldsNull() throws IOException {
|
||||
write("snippet/rate/other.json", "{\"amount\":1}");
|
||||
|
||||
assertNull(rateOf("\"snippet/absent\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void arraySnippetInAListStandsForAllItsEntries() throws IOException {
|
||||
write("snippet/rate/many.json", "[{\"amount\":1},{\"amount\":2},]");
|
||||
write("snippet/rate/one.json", "{\"amount\":5}");
|
||||
|
||||
RateListHolder holder = data().getGson().fromJson(
|
||||
"{\"rates\":[\"snippet/many\",\"snippet/one\",{\"amount\":9}],"
|
||||
+ "\"keyedRates\":[\"snippet/many\",{\"amount\":9}]}",
|
||||
RateListHolder.class);
|
||||
|
||||
assertEquals(List.of(1, 2, 5, 9), amountsOf(holder.rates));
|
||||
assertEquals(List.of(1, 2, 9), amountsOf(holder.keyedRates));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unreadableSnippetKeepsItsSlotInAList() throws IOException {
|
||||
write("snippet/rate/one.json", "{\"amount\":5}");
|
||||
|
||||
RateListHolder holder = data().getGson().fromJson(
|
||||
"{\"rates\":[\"snippet/missing\",\"snippet/one\"]}", RateListHolder.class);
|
||||
|
||||
assertEquals(2, holder.rates.size());
|
||||
assertNull(holder.rates.get(0));
|
||||
assertEquals(5, holder.rates.get(1).getAmount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleReferenceToAnArraySnippetYieldsNull() throws IOException {
|
||||
write("snippet/rate/many.json", "[{\"amount\":1},{\"amount\":2}]");
|
||||
|
||||
assertNull(rateOf("\"snippet/many\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listsWithoutReferencesStillLoad() {
|
||||
RateListHolder holder = data().getGson().fromJson(
|
||||
"{\"rates\":[{\"amount\":4},{\"amount\":6}]}", RateListHolder.class);
|
||||
|
||||
assertEquals(List.of(4, 6), amountsOf(holder.rates));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void possibleSnippetsListTheTypeFolderAsShortReferences() throws IOException {
|
||||
write("snippet/rate/a.json", "{}");
|
||||
write("snippet/rate/nested/c.json", "{}");
|
||||
write("snippet/duration/other.json", "{}");
|
||||
|
||||
assertEquals(List.of("snippet/a", "snippet/nested/c"),
|
||||
data().getPossibleSnippets("rate").stream().sorted().toList());
|
||||
}
|
||||
|
||||
private IrisRate rateOf(String jsonValue) {
|
||||
return data().getGson().fromJson("{\"rate\":" + jsonValue + "}", RateHolder.class).rate;
|
||||
}
|
||||
|
||||
private IrisData data() {
|
||||
if (data == null) {
|
||||
data = IrisData.get(new File(pack.toFile(), "."));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private void write(String relative, String content) throws IOException {
|
||||
Path file = pack.resolve(relative);
|
||||
Files.createDirectories(file.getParent());
|
||||
Files.writeString(file, content);
|
||||
}
|
||||
|
||||
private void link(String relative, String target) throws IOException {
|
||||
Path link = pack.resolve(relative);
|
||||
Files.createDirectories(link.getParent());
|
||||
Files.createSymbolicLink(link, pack.resolve(target));
|
||||
}
|
||||
|
||||
private static List<Integer> amountsOf(List<IrisRate> rates) {
|
||||
assertNotNull(rates);
|
||||
List<Integer> amounts = new ArrayList<>();
|
||||
for (IrisRate rate : rates) {
|
||||
assertNotNull(rate);
|
||||
amounts.add(rate.getAmount());
|
||||
}
|
||||
return amounts;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user