Skip to content

Commit

Permalink
format/yaml: Use Groovy for test execution
Browse files Browse the repository at this point in the history
This lets us use nicer syntax features
  • Loading branch information
zml2008 committed Dec 15, 2020
1 parent adf6653 commit d311e2f
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 330 deletions.
11 changes: 11 additions & 0 deletions format/yaml/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import org.spongepowered.configurate.build.useAutoValue

plugins {
id("org.spongepowered.configurate.build.component")
groovy // For writing tests
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

description = "YAML format loader for Configurate"
Expand All @@ -13,6 +19,11 @@ dependencies {
api(core())
// When updating snakeyaml, check ConfigurateScanner for changes against upstream
implementation("org.yaml:snakeyaml:1.+")

testImplementation("org.codehaus.groovy:groovy:3.+:indy")
testImplementation("org.codehaus.groovy:groovy-nio:3.+:indy")
testImplementation("org.codehaus.groovy:groovy-test-junit5:3.+:indy")
testImplementation("org.codehaus.groovy:groovy-templates:3.+:indy")
}

tasks.compileJava {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ void mapping(final ConfigurationNode node) throws ParsingException {
if (!child.virtual()) { // duplicate keys are forbidden (3.2.1.3)
throw makeError(node, "Duplicate key '" + child.key() + "' encountered!", null);
}
value(node.node(child));
value(child);
}

requireEvent(Event.ID.MappingEnd);
Expand Down
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() {

}

}
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()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.spongepowered.configurate.yaml;
package org.spongepowered.configurate.yaml

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNull
import static org.junit.jupiter.api.Assertions.assertThrows
import static org.junit.jupiter.api.Assertions.assertTrue

import org.junit.jupiter.api.Test;
import org.spongepowered.configurate.ConfigurationNode;
import org.junit.jupiter.api.Test
import org.spongepowered.configurate.ConfigurationNode

import java.io.IOException;
import java.io.IOException

public class YamlParserTest implements YamlTest {
class YamlParserTest implements YamlTest {

@Test
void testEmptyDocument() throws IOException {
final ConfigurationNode result = parseString("");
assertTrue(result.empty());
assertNull(result.raw());
final ConfigurationNode result = parseString("")
assertTrue(result.empty())
assertNull(result.raw())
}

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

}
Loading

0 comments on commit d311e2f

Please sign in to comment.