Skip to content

Commit

Permalink
Merge pull request #451 from russellbanks/Flatten-Yaml-Node-Test
Browse files Browse the repository at this point in the history
Flatten Yaml Node Test
  • Loading branch information
charleskorn authored Jul 30, 2023
2 parents a9313dd + aa44795 commit d8ea070
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 52 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ kotlin {
implementation("io.kotest:kotest-assertions-core:5.6.2")
implementation("io.kotest:kotest-framework-api:5.6.2")
implementation("io.kotest:kotest-framework-engine:5.6.2")
implementation("io.kotest:kotest-framework-datatest:5.6.2")
}
}

Expand Down
99 changes: 47 additions & 52 deletions src/commonTest/kotlin/com/charleskorn/kaml/YamlNodeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,62 +21,57 @@ package com.charleskorn.kaml
import io.kotest.assertions.asClue
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe

class YamlNodeTest : DescribeSpec({
describe("converting from YamlNode") {
val path = YamlPath.root
class YamlNodeTest : FunSpec({
val path = YamlPath.root
val testScalar = YamlScalar("test", path)
val testNull = YamlNull(path)
val testList = YamlList(emptyList(), path)
val testMap = YamlMap(emptyMap(), path)
val testTaggedNode = YamlTaggedNode("tag", YamlScalar("tagged_scalar", path))

val testScalar = YamlScalar("test", path)
val testNull = YamlNull(path)
val testList = YamlList(emptyList(), path)
val testMap = YamlMap(emptyMap(), path)
val testTaggedNode = YamlTaggedNode("tag", YamlScalar("tagged_scalar", path))

listOf(
Triple("YamlScalar", YamlNode::yamlScalar, testScalar),
Triple("YamlNull", YamlNode::yamlNull, testNull),
Triple("YamlList", YamlNode::yamlList, testList),
Triple("YamlMap", YamlNode::yamlMap, testMap),
Triple("YamlTaggedNode", YamlNode::yamlTaggedNode, testTaggedNode),
).forEach { (type, method, value) ->
it("successfully converts to $type") {
shouldNotThrowAny { method(value) }
method(value) shouldBe value
}
}
withData(
YamlNode::yamlScalar to testScalar,
YamlNode::yamlNull to testNull,
YamlNode::yamlList to testList,
YamlNode::yamlMap to testMap,
YamlNode::yamlTaggedNode to testTaggedNode,
) { (method, value) ->
shouldNotThrowAny { method(value) }
method(value) shouldBe value
}

listOf(
Triple("YamlScalar", YamlNode::yamlScalar, testNull),
Triple("YamlScalar", YamlNode::yamlScalar, testList),
Triple("YamlScalar", YamlNode::yamlScalar, testMap),
Triple("YamlScalar", YamlNode::yamlScalar, testTaggedNode),
Triple("YamlNull", YamlNode::yamlNull, testScalar),
Triple("YamlNull", YamlNode::yamlNull, testList),
Triple("YamlNull", YamlNode::yamlNull, testMap),
Triple("YamlNull", YamlNode::yamlNull, testTaggedNode),
Triple("YamlList", YamlNode::yamlList, testScalar),
Triple("YamlList", YamlNode::yamlList, testNull),
Triple("YamlList", YamlNode::yamlList, testMap),
Triple("YamlList", YamlNode::yamlList, testTaggedNode),
Triple("YamlMap", YamlNode::yamlMap, testScalar),
Triple("YamlMap", YamlNode::yamlMap, testNull),
Triple("YamlMap", YamlNode::yamlMap, testList),
Triple("YamlMap", YamlNode::yamlMap, testTaggedNode),
Triple("YamlTaggedNode", YamlNode::yamlTaggedNode, testScalar),
Triple("YamlTaggedNode", YamlNode::yamlTaggedNode, testNull),
Triple("YamlTaggedNode", YamlNode::yamlTaggedNode, testList),
Triple("YamlTaggedNode", YamlNode::yamlTaggedNode, testMap),
).forEach { (type, method, value) ->
val fromType = value::class.simpleName
it("throws when converting from $fromType to $type") {
val exception = shouldThrow<IncorrectTypeException> { method(value) }
exception.asClue {
it.message shouldBe "Expected element to be $type but is $fromType"
it.path shouldBe path
}
}
withData(
YamlNode::yamlScalar to testNull,
YamlNode::yamlScalar to testList,
YamlNode::yamlScalar to testMap,
YamlNode::yamlScalar to testTaggedNode,
YamlNode::yamlNull to testScalar,
YamlNode::yamlNull to testList,
YamlNode::yamlNull to testMap,
YamlNode::yamlNull to testTaggedNode,
YamlNode::yamlList to testScalar,
YamlNode::yamlList to testNull,
YamlNode::yamlList to testMap,
YamlNode::yamlList to testTaggedNode,
YamlNode::yamlMap to testScalar,
YamlNode::yamlMap to testNull,
YamlNode::yamlMap to testList,
YamlNode::yamlMap to testTaggedNode,
YamlNode::yamlTaggedNode to testScalar,
YamlNode::yamlTaggedNode to testNull,
YamlNode::yamlTaggedNode to testList,
YamlNode::yamlTaggedNode to testMap,
) { (method, value) ->
val type = method.name.replaceFirstChar(Char::titlecase)
val fromType = value::class.simpleName
val exception = shouldThrow<IncorrectTypeException> { method(value) }
exception.asClue {
it.message shouldBe "Expected element to be $type but is $fromType"
it.path shouldBe path
}
}
})

0 comments on commit d8ea070

Please sign in to comment.