Skip to content

Commit

Permalink
Improve & refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
krzema12 committed Jan 20, 2025
1 parent c7a44cb commit 65973e6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ val yamlTestSuite by configurations.registering {
asConsumer()

withDependencies {
// See [YAML Test Suite](https://github.com/yaml/yaml-test-suite
add(project.dependencies.create("yaml:yaml-test-suite:data-2022-01-17@zip"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import okio.FileSystem
import okio.Path
import okio.Path.Companion.toPath
import okio.Source
import okio.buffer

/**
* Represents a [FileSystem]-aware [Path]. Thanks to this, there's no need
Expand Down Expand Up @@ -35,9 +36,21 @@ class FSPath(val path: Path, val fs: FileSystem) {
fun resolve(child: String): FSPath =
FSPath(path.resolve(child), fs)

operator fun div(child: String): FSPath =
this.resolve(child)

fun source(): Source =
fs.source(path)

fun readUtf8(): String =
source().buffer().readUtf8()

/**
* Returns `null` if the file doesn't exist.
*/
fun readUtf8OrNull(): String? =
if (this.exists) this.source().buffer().readUtf8() else null

override fun toString(): String =
path.toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package it.krzeminski.snakeyaml.engine.kmp.test_suite
import it.krzeminski.snakeyaml.engine.kmp.buildFileSystem
import it.krzeminski.snakeyaml.engine.kmp.internal.FSPath
import it.krzeminski.snakeyaml.engine.kmp.internal.fsPath
import okio.buffer
import kotlin.jvm.JvmInline


/**
* Test data sourced from https://github.com/yaml/yaml-test-suite
*/
sealed interface YamlTestData {
internal sealed interface YamlTestData {
val id: Id
val label: String
val inYaml: String
Expand Down Expand Up @@ -38,9 +37,9 @@ sealed interface YamlTestData {

val YamlTestResourcesFileSystem = buildFileSystem(YamlTestSuiteResources.resourcesMap)

val YamlTestSuiteData: Map<YamlTestData.Id, YamlTestData> =
internal val YamlTestSuiteData: Map<YamlTestData.Id, YamlTestData> =
YamlTestResourcesFileSystem.fsPath("/").listRecursively()
.filter { it.isDirectory && it.resolve("===").exists }
.filter { it.isDirectory && (it / "===").exists }
.associate { dir ->
val id = YamlTestData.Id(dir.path.segments.joinToString(":"))
id to generateYamlTestDataObject(id, dir)
Expand All @@ -50,29 +49,23 @@ private fun generateYamlTestDataObject(
id: YamlTestData.Id,
path: FSPath,
): YamlTestData {
val isError = path.resolve("error").exists
val isError = (path / "error").exists
return if (isError) {
object : YamlTestData.Error {
override val id: YamlTestData.Id = id
override val label: String = path.resolve("===").source().buffer().readUtf8()
override val inYaml: String = path.resolve("in.yaml").source().buffer().readUtf8()
override val label: String = (path / "===").readUtf8()
override val inYaml: String = (path / "in.yaml").readUtf8()

}
} else {
object : YamlTestData.Success {
override val id: YamlTestData.Id = id
override val testEvent: String = path.resolve("test.event").source().buffer().readUtf8()
override val label: String = path.resolve("===").source().buffer().readUtf8()
override val inYaml: String = path.resolve("in.yaml").source().buffer().readUtf8()
override val outYaml: String? = path.resolve("out.yaml").let {
if (it.exists) it.source().buffer().readUtf8() else null
}
override val emitYaml: String? = path.resolve("emit.yaml").let {
if (it.exists) it.source().buffer().readUtf8() else null
}
override val inJson: String? = path.resolve("in.json").let {
if (it.exists) it.source().buffer().readUtf8() else null
}
override val label: String = (path / "===").readUtf8()
override val inYaml: String = (path / "in.yaml").readUtf8()
override val testEvent: String = (path / "test.event").readUtf8()
override val outYaml: String? = (path / "out.yaml").readUtf8OrNull()
override val emitYaml: String? = (path / "emit.yaml").readUtf8OrNull()
override val inJson: String? = (path / "in.json").readUtf8OrNull()
}
}
}

0 comments on commit 65973e6

Please sign in to comment.