#703: Fix/test yaml anchors and merge

This commit is contained in:
Wolf2323 2022-01-02 21:08:01 +01:00 committed by md_5
parent 129c3c540c
commit 9db6df7d09
No known key found for this signature in database
GPG key ID: E8E901AC7C617C11
3 changed files with 62 additions and 0 deletions

View file

@ -141,6 +141,7 @@ public class YamlConfiguration extends FileConfiguration {
} }
private void fromNodeTree(@NotNull MappingNode input, @NotNull ConfigurationSection section) { private void fromNodeTree(@NotNull MappingNode input, @NotNull ConfigurationSection section) {
constructor.flattenMapping(input);
for (NodeTuple nodeTuple : input.getValue()) { for (NodeTuple nodeTuple : input.getValue()) {
ScalarNode key = (ScalarNode) nodeTuple.getKeyNode(); ScalarNode key = (ScalarNode) nodeTuple.getKeyNode();
String keyString = key.getValue(); String keyString = key.getValue();

View file

@ -7,6 +7,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.yaml.snakeyaml.constructor.SafeConstructor; import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.error.YAMLException; import org.yaml.snakeyaml.error.YAMLException;
import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.nodes.Node; import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.Tag; import org.yaml.snakeyaml.nodes.Tag;
@ -16,6 +17,11 @@ public class YamlConstructor extends SafeConstructor {
this.yamlConstructors.put(Tag.MAP, new ConstructCustomObject()); this.yamlConstructors.put(Tag.MAP, new ConstructCustomObject());
} }
@Override
public void flattenMapping(@NotNull final MappingNode node) {
super.flattenMapping(node);
}
@NotNull @NotNull
public Object construct(@NotNull Node node) { public Object construct(@NotNull Node node) {
return constructObject(node); return constructObject(node);

View file

@ -4,6 +4,7 @@ import static org.junit.Assert.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.bukkit.configuration.InvalidConfigurationException;
import org.junit.Test; import org.junit.Test;
public class YamlConfigurationTest extends FileConfigurationTest { public class YamlConfigurationTest extends FileConfigurationTest {
@ -89,4 +90,58 @@ public class YamlConfigurationTest extends FileConfigurationTest {
assertEquals(expected, result); assertEquals(expected, result);
} }
@Test
public void testAnchorNode() throws InvalidConfigurationException {
YamlConfiguration config = getConfig();
String content = "effects:\n"
+ " eff1: &novaEarth\n"
+ " position: special\n"
+ " effect: nova\n"
+ " eff2: *novaEarth\n"
+ " eff3: *novaEarth";
config.loadFromString(content);
String expected = "effects:\n"
+ " eff1:\n"
+ " position: special\n"
+ " effect: nova\n"
+ " eff2:\n"
+ " position: special\n"
+ " effect: nova\n"
+ " eff3:\n"
+ " position: special\n"
+ " effect: nova\n";
assertEquals(expected, config.saveToString());
}
@Test
public void testMergeNode() throws InvalidConfigurationException {
YamlConfiguration config = getConfig();
String content = "effects:\n"
+ " eff1: &novaEarth\n"
+ " position: special\n"
+ " effect: nova\n"
+ " eff2: \n"
+ " <<: *novaEarth\n"
+ " height-offset: 0\n"
+ " eff3: \n"
+ " <<: *novaEarth\n"
+ " height-offset: 2";
config.loadFromString(content);
String expected = "effects:\n"
+ " eff1:\n"
+ " position: special\n"
+ " effect: nova\n"
+ " eff2:\n"
+ " position: special\n"
+ " effect: nova\n"
+ " height-offset: 0\n"
+ " eff3:\n"
+ " position: special\n"
+ " effect: nova\n"
+ " height-offset: 2\n";
assertEquals(expected, config.saveToString());
}
} }