Skip to content

Commit

Permalink
test: Ensure all StringScalarStyle and ScalarStyle values match 1 on 1
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsbasjes committed Aug 7, 2024
1 parent 157730d commit c3aee00
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/commonMain/kotlin/com/charleskorn/kaml/YamlOutput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,6 @@ internal class YamlOutput(
private var forcedSingleLineScalarStyle: StringScalarStyle? = null
private var forcedMultiLineScalarStyle: StringScalarStyle? = null

private fun StringScalarStyle.toScalarStyle() =
when (this) {
StringScalarStyle.DOUBLE_QUOTED -> ScalarStyle.DOUBLE_QUOTED
StringScalarStyle.SINGLE_QUOTED -> ScalarStyle.SINGLE_QUOTED
StringScalarStyle.LITERAL -> ScalarStyle.LITERAL
StringScalarStyle.FOLDED -> ScalarStyle.FOLDED
StringScalarStyle.PLAIN -> ScalarStyle.PLAIN
}

override fun encodeString(value: String) {
if (shouldReadTypeName) {
currentTypeName = value
Expand Down Expand Up @@ -259,3 +250,12 @@ internal class YamlOutput(
private val ALL_EXPLICIT = ImplicitTuple(false, false)
}
}

internal fun StringScalarStyle.toScalarStyle(): ScalarStyle =
when (this) {
StringScalarStyle.DOUBLE_QUOTED -> ScalarStyle.DOUBLE_QUOTED
StringScalarStyle.SINGLE_QUOTED -> ScalarStyle.SINGLE_QUOTED
StringScalarStyle.LITERAL -> ScalarStyle.LITERAL
StringScalarStyle.FOLDED -> ScalarStyle.FOLDED
StringScalarStyle.PLAIN -> ScalarStyle.PLAIN
}
11 changes: 11 additions & 0 deletions src/jvmTest/kotlin/com/charleskorn/kaml/JvmYamlWritingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.charleskorn.kaml

import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
import io.kotest.matchers.shouldBe
import it.krzeminski.snakeyaml.engine.kmp.common.ScalarStyle
import kotlinx.serialization.Serializable
Expand Down Expand Up @@ -282,6 +283,16 @@ class JvmYamlWritingTest : DescribeSpec({
""".trimIndent()
}

describe("Ensure all StringScalarStyle and ScalarStyle values match 1 on 1") {
// Mapping the StringScalarStyles to ScalarStyle must result in 100% coverage
val mappedStringScalarStyles = StringScalarStyle.entries.map { it.toScalarStyle() }
ScalarStyle.entries.shouldContainExactlyInAnyOrder(mappedStringScalarStyles)

// The NAME of each of the enums in both must also be identical
StringScalarStyle.entries.forEach {
it.toScalarStyle().name shouldBe it.name
}
}
}
})

Expand Down

0 comments on commit c3aee00

Please sign in to comment.