mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-30 01:34:04 +00:00
Splice studio edits in core instead of the API
JsonSplicer holds the text edits JsonText used to carry; GraphEdit works on it with typed path steps. The bridge maps the API's Optionals to the wire the studio already reads (-1 for no part or block, a blank group), so the studio is unchanged.
This commit is contained in:
1 parent
c0ac9d7f4e
commit
f98d78a87b
9 files changed
+405
-83
No files matched your search
@@ -4,6 +4,7 @@ import com.volmit.tooling.JsonPath;
|
||||
import com.volmit.tooling.JsonText;
|
||||
import org.junit.Test;
|
||||
|
||||
import static art.arcane.iris.core.studio.scene.TestPaths.path;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
@@ -31,42 +32,42 @@ public class GraphEditTest {
|
||||
""";
|
||||
|
||||
private static JsonText edited(GraphEdit edit) {
|
||||
JsonText source = JsonText.parse(TREE);
|
||||
return JsonText.parse(source.apply(edit.splices(source)));
|
||||
JsonSplicer file = new JsonSplicer(JsonText.parse(TREE));
|
||||
return JsonText.parse(file.apply(edit.splices(file)));
|
||||
}
|
||||
|
||||
private static String textAt(JsonText source, Object... steps) {
|
||||
return source.textOf(source.at(JsonPath.of(steps)).orElseThrow());
|
||||
return source.textOf(source.at(path(steps)).orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aNewPartGrowsFromItsParentInAListWrittenForIt() {
|
||||
JsonText result = edited(new GraphEdit.Append(JsonPath.of("parts", 0, "children", 0, "children"), "{\"stem\": {\"length\": 6}}"));
|
||||
JsonText result = edited(new GraphEdit.Append(path("parts", 0, "children", 0, "children"), "{\"stem\": {\"length\": 6}}"));
|
||||
|
||||
assertEquals("6", textAt(result, "parts", 0, "children", 0, "children", 0, "stem", "length"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deletingTheOnlyChildDropsItsList() {
|
||||
JsonText result = edited(new GraphEdit.Delete(JsonPath.of("parts", 0, "children", 1, "children", 0)));
|
||||
JsonText result = edited(new GraphEdit.Delete(path("parts", 0, "children", 1, "children", 0)));
|
||||
|
||||
assertTrue(result.at(JsonPath.of("parts", 0, "children", 1, "children")).isEmpty());
|
||||
assertTrue(result.at(path("parts", 0, "children", 1, "children")).isEmpty());
|
||||
assertEquals("4", textAt(result, "parts", 0, "children", 1, "stem", "length"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aMovedPartTakesWhatGrowsFromItAlong() {
|
||||
JsonText result = edited(new GraphEdit.Move(JsonPath.of("parts", 0, "children", 1), JsonPath.of("parts", 0, "children", 0, "children")));
|
||||
JsonText result = edited(new GraphEdit.Move(path("parts", 0, "children", 1), path("parts", 0, "children", 0, "children")));
|
||||
|
||||
assertEquals(1, ((JsonText.Arr) result.at(JsonPath.of("parts", 0, "children")).orElseThrow()).elements().size());
|
||||
assertEquals(1, ((JsonText.Arr) result.at(path("parts", 0, "children")).orElseThrow()).elements().size());
|
||||
assertEquals("1", textAt(result, "parts", 0, "children", 0, "children", 0, "children", 0, "blob", "radius"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aMovedPartIsIndentedForItsNewPlace() {
|
||||
JsonText source = JsonText.parse(TREE);
|
||||
JsonSplicer file = new JsonSplicer(JsonText.parse(TREE));
|
||||
|
||||
String result = source.apply(new GraphEdit.Move(JsonPath.of("parts", 0, "children", 1), JsonPath.of("parts")).splices(source));
|
||||
String result = file.apply(new GraphEdit.Move(path("parts", 0, "children", 1), path("parts")).splices(file));
|
||||
|
||||
assertTrue(result, result.contains("""
|
||||
},
|
||||
@@ -82,16 +83,16 @@ public class GraphEditTest {
|
||||
|
||||
@Test
|
||||
public void aDuplicateJoinsTheSameListWithWhatGrowsFromIt() {
|
||||
JsonText result = edited(new GraphEdit.Duplicate(JsonPath.of("parts", 0, "children", 1)));
|
||||
JsonText result = edited(new GraphEdit.Duplicate(path("parts", 0, "children", 1)));
|
||||
|
||||
assertEquals(3, ((JsonText.Arr) result.at(JsonPath.of("parts", 0, "children")).orElseThrow()).elements().size());
|
||||
assertEquals(3, ((JsonText.Arr) result.at(path("parts", 0, "children")).orElseThrow()).elements().size());
|
||||
assertEquals("1", textAt(result, "parts", 0, "children", 2, "children", 0, "blob", "radius"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void settingWritesTheMemberAndAnObjectThatIsMissing() {
|
||||
JsonText lean = edited(new GraphEdit.Set(JsonPath.of("parts", 0, "stem"), "lean", "7"));
|
||||
JsonText offset = edited(new GraphEdit.Set(JsonPath.of("parts", 0, "offset"), "y", "2"));
|
||||
JsonText lean = edited(new GraphEdit.Set(path("parts", 0, "stem"), "lean", "7"));
|
||||
JsonText offset = edited(new GraphEdit.Set(path("parts", 0, "offset"), "y", "2"));
|
||||
|
||||
assertEquals("7", textAt(lean, "parts", 0, "stem", "lean"));
|
||||
assertEquals("2", textAt(offset, "parts", 0, "offset", "y"));
|
||||
@@ -99,19 +100,19 @@ public class GraphEditTest {
|
||||
|
||||
@Test
|
||||
public void unsettingWhatIsNotWrittenChangesNothing() {
|
||||
JsonText source = JsonText.parse(TREE);
|
||||
JsonSplicer file = new JsonSplicer(JsonText.parse(TREE));
|
||||
|
||||
assertTrue(new GraphEdit.Unset(JsonPath.of("parts", 0, "stem"), "lean").splices(source).isEmpty());
|
||||
assertTrue(edited(new GraphEdit.Unset(JsonPath.of("parts", 0, "stem"), "length")).at(JsonPath.of("parts", 0, "stem", "length")).isEmpty());
|
||||
assertTrue(new GraphEdit.Unset(path("parts", 0, "stem"), "lean").splices(file).isEmpty());
|
||||
assertTrue(edited(new GraphEdit.Unset(path("parts", 0, "stem"), "length")).at(path("parts", 0, "stem", "length")).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aPartCanNotMoveIntoItselfOrWhereItIs() {
|
||||
JsonPath part = JsonPath.of("parts", 0, "children", 1);
|
||||
JsonPath part = path("parts", 0, "children", 1);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> new GraphEdit.Move(part, part.member("children")));
|
||||
assertThrows(IllegalArgumentException.class, () -> new GraphEdit.Move(part, JsonPath.of("parts", 0, "children")));
|
||||
assertThrows(IllegalArgumentException.class, () -> new GraphEdit.Move(JsonPath.of("parts", 0, "branches", "part"), JsonPath.of("parts")));
|
||||
assertThrows(IllegalArgumentException.class, () -> new GraphEdit.Move(part, path("parts", 0, "children")));
|
||||
assertThrows(IllegalArgumentException.class, () -> new GraphEdit.Move(path("parts", 0, "branches", "part"), path("parts")));
|
||||
assertThrows(IllegalArgumentException.class, () -> new GraphEdit.Set(JsonPath.ROOT, "variants", "eight"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package art.arcane.iris.core.studio.scene;
|
||||
|
||||
import com.volmit.tooling.JsonText;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static art.arcane.iris.core.studio.scene.TestPaths.path;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class JsonSplicerTest {
|
||||
private static final String TREE = """
|
||||
{
|
||||
"variants": 8,
|
||||
"parts": [
|
||||
{
|
||||
"stem": {"length": [16.0, 26.0], "radius": 0.5},
|
||||
"children": [
|
||||
{
|
||||
"blob": {
|
||||
"radius": 3
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
""";
|
||||
|
||||
private final JsonSplicer file = new JsonSplicer(JsonText.parse(TREE));
|
||||
|
||||
@Test
|
||||
public void replacingAValueKeepsTheRestOfTheText() {
|
||||
JsonText.Value length = file.at(path("parts", 0, "stem", "length")).orElseThrow();
|
||||
|
||||
String edited = file.apply(List.of(file.replace(length, "[12, 20]")));
|
||||
|
||||
assertEquals(TREE.replace("[16.0, 26.0]", "[12, 20]"), edited);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aNewMemberFollowsAnInlineObjectsStyle() {
|
||||
JsonText.Obj stem = (JsonText.Obj) file.at(path("parts", 0, "stem")).orElseThrow();
|
||||
|
||||
String edited = file.apply(List.of(file.put(stem, "lean", "7")));
|
||||
|
||||
assertTrue(edited.contains("\"stem\": {\"length\": [16.0, 26.0], \"radius\": 0.5, \"lean\": 7},"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aNewMemberFollowsAMultilineObjectsStyle() {
|
||||
JsonText.Obj blob = (JsonText.Obj) file.at(path("parts", 0, "children", 0, "blob")).orElseThrow();
|
||||
|
||||
String edited = file.apply(List.of(file.put(blob, "depth", "0")));
|
||||
|
||||
assertTrue(edited.contains("\"radius\": 3,\n \"depth\": 0\n"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removingMembersLeavesValidJson() {
|
||||
JsonText.Obj stem = (JsonText.Obj) file.at(path("parts", 0, "stem")).orElseThrow();
|
||||
|
||||
String withoutFirst = file.apply(List.of(file.remove(stem, "length")));
|
||||
String withoutLast = file.apply(List.of(file.remove(stem, "radius")));
|
||||
|
||||
assertTrue(withoutFirst.contains("\"stem\": {\"radius\": 0.5},"));
|
||||
assertTrue(withoutLast.contains("\"stem\": {\"length\": [16.0, 26.0]},"));
|
||||
assertTrue(JsonText.isValue(withoutFirst));
|
||||
assertTrue(JsonText.isValue(withoutLast));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aMovedSubtreeIsIndentedForItsNewPlace() {
|
||||
JsonText.Arr children = (JsonText.Arr) file.at(path("parts", 0, "children")).orElseThrow();
|
||||
JsonText.Arr parts = (JsonText.Arr) file.at(path("parts")).orElseThrow();
|
||||
JsonText.Obj parent = (JsonText.Obj) file.at(path("parts", 0)).orElseThrow();
|
||||
|
||||
String edited = file.apply(List.of(file.remove(parent, "children"), file.append(parts, file.detachedTextOf(children.elements().getFirst()))));
|
||||
|
||||
JsonText moved = JsonText.parse(edited);
|
||||
assertEquals("3", moved.textOf(moved.at(path("parts", 1, "blob", "radius")).orElseThrow()));
|
||||
assertTrue(edited.contains("\n {\n \"blob\": {\n \"radius\": 3\n }\n }\n ]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void quotesTextAndRefusesOverlappingEdits() {
|
||||
JsonText.Value variants = file.at(path("variants")).orElseThrow();
|
||||
|
||||
assertEquals("\"a\\\"b\"", JsonSplicer.quote("a\"b"));
|
||||
assertThrows(IllegalArgumentException.class, () -> file.apply(List.of(file.replace(variants, "1"), file.replace(variants, "2"))));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package art.arcane.iris.core.studio.scene;
|
||||
|
||||
import com.volmit.tooling.JsonPath;
|
||||
|
||||
/** Paths written as tests read them: a {@link String} for a member, an {@link Integer} for an element. */
|
||||
final class TestPaths {
|
||||
private TestPaths() {
|
||||
}
|
||||
|
||||
static JsonPath path(Object... steps) {
|
||||
JsonPath path = JsonPath.ROOT;
|
||||
for (Object step : steps) {
|
||||
path = step instanceof Integer position ? path.element(position) : path.member((String) step);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user