Skip to content

Commit d311e2f

Browse files
committed
format/yaml: Use Groovy for test execution
This lets us use nicer syntax features
1 parent adf6653 commit d311e2f

File tree

9 files changed

+330
-330
lines changed

9 files changed

+330
-330
lines changed

format/yaml/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import org.spongepowered.configurate.build.useAutoValue
44

55
plugins {
66
id("org.spongepowered.configurate.build.component")
7+
groovy // For writing tests
8+
}
9+
10+
java {
11+
sourceCompatibility = JavaVersion.VERSION_1_8
12+
targetCompatibility = JavaVersion.VERSION_1_8
713
}
814

915
description = "YAML format loader for Configurate"
@@ -13,6 +19,11 @@ dependencies {
1319
api(core())
1420
// When updating snakeyaml, check ConfigurateScanner for changes against upstream
1521
implementation("org.yaml:snakeyaml:1.+")
22+
23+
testImplementation("org.codehaus.groovy:groovy:3.+:indy")
24+
testImplementation("org.codehaus.groovy:groovy-nio:3.+:indy")
25+
testImplementation("org.codehaus.groovy:groovy-test-junit5:3.+:indy")
26+
testImplementation("org.codehaus.groovy:groovy-templates:3.+:indy")
1627
}
1728

1829
tasks.compileJava {

format/yaml/src/main/java/org/spongepowered/configurate/yaml/YamlParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void mapping(final ConfigurationNode node) throws ParsingException {
189189
if (!child.virtual()) { // duplicate keys are forbidden (3.2.1.3)
190190
throw makeError(node, "Duplicate key '" + child.key() + "' encountered!", null);
191191
}
192-
value(node.node(child));
192+
value(child);
193193
}
194194

195195
requireEvent(Event.ID.MappingEnd);
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Configurate
3+
* Copyright (C) zml and Configurate contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.spongepowered.configurate.yaml
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals
20+
import static org.junit.jupiter.api.Assertions.assertFalse
21+
import static org.junit.jupiter.api.Assertions.assertNull
22+
23+
import org.junit.jupiter.api.Disabled
24+
import org.junit.jupiter.api.Test
25+
import org.spongepowered.configurate.CommentedConfigurationNode
26+
27+
class CommentTest implements YamlTest {
28+
29+
@Test
30+
void testLoadScalarComment() {
31+
final CommentedConfigurationNode node = parseString("""
32+
# Hello world
33+
"i'm a string"
34+
""".stripIndent())
35+
36+
assertEquals("Hello world", node.comment())
37+
assertEquals("i'm a string", node.raw())
38+
}
39+
40+
@Test
41+
void testLoadBlockMappingComment() {
42+
final CommentedConfigurationNode node = parseString("""
43+
test:
44+
# meow
45+
cat: purrs
46+
""".stripIndent())
47+
48+
assertEquals("purrs", node.node("test", "cat").raw())
49+
assertEquals("meow", node.node("test", "cat").comment())
50+
}
51+
52+
@Test
53+
void testLoadBlockScalarSequenceComment() {
54+
final CommentedConfigurationNode test = parseString("""
55+
- first
56+
# i matter less
57+
- second
58+
- third
59+
# we skipped one
60+
- fourth
61+
""".stripIndent())
62+
63+
assertNull(test.node(0).comment())
64+
assertEquals("i matter less", test.node(1).comment())
65+
assertEquals("we skipped one", test.node(3).comment())
66+
}
67+
68+
@Test
69+
@Disabled("This doesn't seem to associate comments with the first map entry properly")
70+
void testLoadScalarCommentsInBlockMapping() {
71+
final CommentedConfigurationNode test = parseString("""
72+
blah:
73+
# beginning sequence
74+
- # first on map entry
75+
test: hello
76+
- # on second mapping
77+
test2: goodbye
78+
""".stripIndent(true))
79+
80+
final CommentedConfigurationNode child = test.node("blah", 0)
81+
assertFalse(child.virtual())
82+
assertEquals("beginning sequence", child.comment())
83+
assertEquals("first on map entry", child.node("test").comment())
84+
assertEquals("on second mapping", child.node("test2").comment())
85+
}
86+
87+
// flow collections are a bit trickier
88+
// we can't really do comments on one line, so these all have to have a line per element
89+
90+
@Test
91+
void testLoadCommentInFlowMapping() {
92+
final CommentedConfigurationNode test = parseString("""
93+
{
94+
# hello
95+
test: value,
96+
uncommented: thing,
97+
#hi there
98+
last: bye
99+
}
100+
""".stripIndent())
101+
102+
assertEquals("hello", test.node("test").comment())
103+
assertNull(test.node("uncommented").comment())
104+
assertEquals("hi there", test.node("last").comment())
105+
}
106+
107+
@Test
108+
void testLoadCommentInFlowSequence() {
109+
final CommentedConfigurationNode test = parseString("""
110+
# on list
111+
[
112+
# first
113+
'first entry',
114+
# second
115+
'second entry'
116+
]
117+
""".stripIndent())
118+
119+
assertEquals("on list", test.comment())
120+
assertEquals("first", test.node(0).comment())
121+
assertEquals("second", test.node(1).comment())
122+
}
123+
124+
@Test
125+
void testLoadMixedStructure() {
126+
final CommentedConfigurationNode test = parseResource(getClass().getResource("/comments-complex.yml"))
127+
128+
}
129+
130+
@Test
131+
void testWriteScalarCommented() {
132+
final CommentedConfigurationNode node = CommentedConfigurationNode.root()
133+
.raw("test")
134+
.comment("i have a comment")
135+
136+
assertEquals("""
137+
# i have a comment
138+
test
139+
""".stripIndent(),
140+
dump(node))
141+
}
142+
143+
@Test
144+
void testWriteBlockMappingCommented() {
145+
146+
}
147+
148+
@Test
149+
void testWriteBlockSequenceCommented() {
150+
151+
}
152+
153+
@Test
154+
void testWriteFlowMappingCommented() {
155+
156+
}
157+
158+
@Test
159+
void testWriteFlowSequenceCommented() {
160+
161+
}
162+
163+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Configurate
3+
* Copyright (C) zml and Configurate contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.spongepowered.configurate.yaml
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals
20+
21+
import io.leangen.geantyref.TypeToken
22+
import org.junit.jupiter.api.Test
23+
import org.junit.jupiter.api.io.TempDir
24+
import org.spongepowered.configurate.BasicConfigurationNode
25+
import org.spongepowered.configurate.CommentedConfigurationNode
26+
import org.spongepowered.configurate.ConfigurateException
27+
import org.spongepowered.configurate.ConfigurationNode
28+
import org.spongepowered.configurate.loader.ConfigurationLoader
29+
30+
import java.nio.charset.StandardCharsets
31+
import java.nio.file.Path
32+
33+
/**
34+
* Basic sanity checks for the loader.
35+
*/
36+
class YamlConfigurationLoaderTest {
37+
38+
@Test
39+
void testSimpleLoading() throws ConfigurateException {
40+
final def url = getClass().getResource("/example.yml")
41+
final def out = new StringWriter()
42+
final def loader = YamlConfigurationLoader.builder()
43+
.url(url)
44+
.sink(() -> new BufferedWriter(out)).build()
45+
final def node = loader.load()
46+
47+
assertEquals("unicorn", node.node("test", "op-level").raw())
48+
assertEquals("dragon", node.node("other", "op-level").raw())
49+
assertEquals("dog park", node.node("other", "location").raw())
50+
51+
loader.save(node)
52+
println(out.toString())
53+
54+
final def fooList = new ArrayList<>(node.node("foo")
55+
.getList(new TypeToken<Map<String, List<String>>>() {}))
56+
assertEquals(0, fooList.get(0).get("bar").size())
57+
58+
}
59+
60+
@Test
61+
void testReadWithTabs() throws ConfigurateException {
62+
final def expected = CommentedConfigurationNode.root(n -> {
63+
n.node("document").act(d -> {
64+
d.node("we").raw("support tabs")
65+
d.node("and").raw("literal tabs\tin strings")
66+
d.node("with").act(w -> {
67+
w.appendListNode().raw("more levels")
68+
w.appendListNode().raw("of indentation")
69+
})
70+
})
71+
})
72+
73+
final URL url = getClass().getResource("/tab-example.yml")
74+
final ConfigurationLoader<CommentedConfigurationNode> loader = YamlConfigurationLoader.builder()
75+
.url(url).build()
76+
final ConfigurationNode node = loader.load()
77+
assertEquals(expected, node)
78+
}
79+
80+
@Test
81+
void testWriteBasicFile(final @TempDir Path tempDir) throws ConfigurateException, IOException {
82+
final Path target = tempDir.resolve("write-basic.yml")
83+
final ConfigurationNode node = BasicConfigurationNode.root(n -> {
84+
n.node("mapping", "first").set("hello")
85+
n.node("mapping", "second").set("world")
86+
87+
n.node("list").act(c -> {
88+
c.appendListNode().set(1)
89+
c.appendListNode().set(2)
90+
c.appendListNode().set(3)
91+
c.appendListNode().set(4)
92+
})
93+
})
94+
95+
final YamlConfigurationLoader loader = YamlConfigurationLoader.builder()
96+
.path(target)
97+
.nodeStyle(NodeStyle.BLOCK)
98+
.build()
99+
100+
loader.save(node)
101+
102+
assertEquals(getClass().getResource("write-expected.yml").getText(StandardCharsets.UTF_8.name()), target.getText(StandardCharsets.UTF_8.name()))
103+
}
104+
}

format/yaml/src/test/java/org/spongepowered/configurate/yaml/YamlParserTest.java renamed to format/yaml/src/test/groovy/org/spongepowered/configurate/yaml/YamlParserTest.groovy

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,30 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.spongepowered.configurate.yaml;
17+
package org.spongepowered.configurate.yaml
1818

19-
import static org.junit.jupiter.api.Assertions.assertNull;
20-
import static org.junit.jupiter.api.Assertions.assertThrows;
21-
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertNull
20+
import static org.junit.jupiter.api.Assertions.assertThrows
21+
import static org.junit.jupiter.api.Assertions.assertTrue
2222

23-
import org.junit.jupiter.api.Test;
24-
import org.spongepowered.configurate.ConfigurationNode;
23+
import org.junit.jupiter.api.Test
24+
import org.spongepowered.configurate.ConfigurationNode
2525

26-
import java.io.IOException;
26+
import java.io.IOException
2727

28-
public class YamlParserTest implements YamlTest {
28+
class YamlParserTest implements YamlTest {
2929

3030
@Test
3131
void testEmptyDocument() throws IOException {
32-
final ConfigurationNode result = parseString("");
33-
assertTrue(result.empty());
34-
assertNull(result.raw());
32+
final ConfigurationNode result = parseString("")
33+
assertTrue(result.empty())
34+
assertNull(result.raw())
3535
}
3636

3737
@Test
3838
void testDuplicateKeysForbidden() throws IOException {
3939
assertTrue(assertThrows(IOException.class, () -> parseString("{duplicated: 1, duplicated: 2}"))
40-
.getMessage().contains("Duplicate key"));
40+
.getMessage().contains("Duplicate key"))
4141
}
4242

4343
}

0 commit comments

Comments
 (0)