-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
format/yaml: Use Groovy for test execution
This lets us use nicer syntax features
- Loading branch information
Showing
9 changed files
with
330 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
format/yaml/src/test/groovy/org/spongepowered/configurate/yaml/CommentTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* Configurate | ||
* Copyright (C) zml and Configurate contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.spongepowered.configurate.yaml | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals | ||
import static org.junit.jupiter.api.Assertions.assertFalse | ||
import static org.junit.jupiter.api.Assertions.assertNull | ||
|
||
import org.junit.jupiter.api.Disabled | ||
import org.junit.jupiter.api.Test | ||
import org.spongepowered.configurate.CommentedConfigurationNode | ||
|
||
class CommentTest implements YamlTest { | ||
|
||
@Test | ||
void testLoadScalarComment() { | ||
final CommentedConfigurationNode node = parseString(""" | ||
# Hello world | ||
"i'm a string" | ||
""".stripIndent()) | ||
|
||
assertEquals("Hello world", node.comment()) | ||
assertEquals("i'm a string", node.raw()) | ||
} | ||
|
||
@Test | ||
void testLoadBlockMappingComment() { | ||
final CommentedConfigurationNode node = parseString(""" | ||
test: | ||
# meow | ||
cat: purrs | ||
""".stripIndent()) | ||
|
||
assertEquals("purrs", node.node("test", "cat").raw()) | ||
assertEquals("meow", node.node("test", "cat").comment()) | ||
} | ||
|
||
@Test | ||
void testLoadBlockScalarSequenceComment() { | ||
final CommentedConfigurationNode test = parseString(""" | ||
- first | ||
# i matter less | ||
- second | ||
- third | ||
# we skipped one | ||
- fourth | ||
""".stripIndent()) | ||
|
||
assertNull(test.node(0).comment()) | ||
assertEquals("i matter less", test.node(1).comment()) | ||
assertEquals("we skipped one", test.node(3).comment()) | ||
} | ||
|
||
@Test | ||
@Disabled("This doesn't seem to associate comments with the first map entry properly") | ||
void testLoadScalarCommentsInBlockMapping() { | ||
final CommentedConfigurationNode test = parseString(""" | ||
blah: | ||
# beginning sequence | ||
- # first on map entry | ||
test: hello | ||
- # on second mapping | ||
test2: goodbye | ||
""".stripIndent(true)) | ||
|
||
final CommentedConfigurationNode child = test.node("blah", 0) | ||
assertFalse(child.virtual()) | ||
assertEquals("beginning sequence", child.comment()) | ||
assertEquals("first on map entry", child.node("test").comment()) | ||
assertEquals("on second mapping", child.node("test2").comment()) | ||
} | ||
|
||
// flow collections are a bit trickier | ||
// we can't really do comments on one line, so these all have to have a line per element | ||
|
||
@Test | ||
void testLoadCommentInFlowMapping() { | ||
final CommentedConfigurationNode test = parseString(""" | ||
{ | ||
# hello | ||
test: value, | ||
uncommented: thing, | ||
#hi there | ||
last: bye | ||
} | ||
""".stripIndent()) | ||
|
||
assertEquals("hello", test.node("test").comment()) | ||
assertNull(test.node("uncommented").comment()) | ||
assertEquals("hi there", test.node("last").comment()) | ||
} | ||
|
||
@Test | ||
void testLoadCommentInFlowSequence() { | ||
final CommentedConfigurationNode test = parseString(""" | ||
# on list | ||
[ | ||
# first | ||
'first entry', | ||
# second | ||
'second entry' | ||
] | ||
""".stripIndent()) | ||
|
||
assertEquals("on list", test.comment()) | ||
assertEquals("first", test.node(0).comment()) | ||
assertEquals("second", test.node(1).comment()) | ||
} | ||
|
||
@Test | ||
void testLoadMixedStructure() { | ||
final CommentedConfigurationNode test = parseResource(getClass().getResource("/comments-complex.yml")) | ||
|
||
} | ||
|
||
@Test | ||
void testWriteScalarCommented() { | ||
final CommentedConfigurationNode node = CommentedConfigurationNode.root() | ||
.raw("test") | ||
.comment("i have a comment") | ||
|
||
assertEquals(""" | ||
# i have a comment | ||
test | ||
""".stripIndent(), | ||
dump(node)) | ||
} | ||
|
||
@Test | ||
void testWriteBlockMappingCommented() { | ||
|
||
} | ||
|
||
@Test | ||
void testWriteBlockSequenceCommented() { | ||
|
||
} | ||
|
||
@Test | ||
void testWriteFlowMappingCommented() { | ||
|
||
} | ||
|
||
@Test | ||
void testWriteFlowSequenceCommented() { | ||
|
||
} | ||
|
||
} |
104 changes: 104 additions & 0 deletions
104
...aml/src/test/groovy/org/spongepowered/configurate/yaml/YamlConfigurationLoaderTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Configurate | ||
* Copyright (C) zml and Configurate contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.spongepowered.configurate.yaml | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals | ||
|
||
import io.leangen.geantyref.TypeToken | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.io.TempDir | ||
import org.spongepowered.configurate.BasicConfigurationNode | ||
import org.spongepowered.configurate.CommentedConfigurationNode | ||
import org.spongepowered.configurate.ConfigurateException | ||
import org.spongepowered.configurate.ConfigurationNode | ||
import org.spongepowered.configurate.loader.ConfigurationLoader | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Path | ||
|
||
/** | ||
* Basic sanity checks for the loader. | ||
*/ | ||
class YamlConfigurationLoaderTest { | ||
|
||
@Test | ||
void testSimpleLoading() throws ConfigurateException { | ||
final def url = getClass().getResource("/example.yml") | ||
final def out = new StringWriter() | ||
final def loader = YamlConfigurationLoader.builder() | ||
.url(url) | ||
.sink(() -> new BufferedWriter(out)).build() | ||
final def node = loader.load() | ||
|
||
assertEquals("unicorn", node.node("test", "op-level").raw()) | ||
assertEquals("dragon", node.node("other", "op-level").raw()) | ||
assertEquals("dog park", node.node("other", "location").raw()) | ||
|
||
loader.save(node) | ||
println(out.toString()) | ||
|
||
final def fooList = new ArrayList<>(node.node("foo") | ||
.getList(new TypeToken<Map<String, List<String>>>() {})) | ||
assertEquals(0, fooList.get(0).get("bar").size()) | ||
|
||
} | ||
|
||
@Test | ||
void testReadWithTabs() throws ConfigurateException { | ||
final def expected = CommentedConfigurationNode.root(n -> { | ||
n.node("document").act(d -> { | ||
d.node("we").raw("support tabs") | ||
d.node("and").raw("literal tabs\tin strings") | ||
d.node("with").act(w -> { | ||
w.appendListNode().raw("more levels") | ||
w.appendListNode().raw("of indentation") | ||
}) | ||
}) | ||
}) | ||
|
||
final URL url = getClass().getResource("/tab-example.yml") | ||
final ConfigurationLoader<CommentedConfigurationNode> loader = YamlConfigurationLoader.builder() | ||
.url(url).build() | ||
final ConfigurationNode node = loader.load() | ||
assertEquals(expected, node) | ||
} | ||
|
||
@Test | ||
void testWriteBasicFile(final @TempDir Path tempDir) throws ConfigurateException, IOException { | ||
final Path target = tempDir.resolve("write-basic.yml") | ||
final ConfigurationNode node = BasicConfigurationNode.root(n -> { | ||
n.node("mapping", "first").set("hello") | ||
n.node("mapping", "second").set("world") | ||
|
||
n.node("list").act(c -> { | ||
c.appendListNode().set(1) | ||
c.appendListNode().set(2) | ||
c.appendListNode().set(3) | ||
c.appendListNode().set(4) | ||
}) | ||
}) | ||
|
||
final YamlConfigurationLoader loader = YamlConfigurationLoader.builder() | ||
.path(target) | ||
.nodeStyle(NodeStyle.BLOCK) | ||
.build() | ||
|
||
loader.save(node) | ||
|
||
assertEquals(getClass().getResource("write-expected.yml").getText(StandardCharsets.UTF_8.name()), target.getText(StandardCharsets.UTF_8.name())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.