Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests to run for JS targets #584

Merged
merged 3 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,8 @@ kotlin {
}

js(IR) {
browser {
testTask {
// TODO: enable once the tests work with Kotlin/JS.
enabled = false
}
}
nodejs {
testTask {
// TODO: enable once the tests work with Kotlin/JS.
enabled = false
}
}
browser()
nodejs()
binaries.executable()
}

Expand Down
73 changes: 51 additions & 22 deletions src/commonTest/kotlin/com/charleskorn/kaml/YamlScalarTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ package com.charleskorn.kaml

import io.kotest.assertions.asClue
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.common.Platform
import io.kotest.common.platform
import io.kotest.core.test.Enabled
import io.kotest.core.test.EnabledOrReasonIf
import io.kotest.matchers.doubles.shouldBeNaN
import io.kotest.matchers.floats.shouldBeNaN
import io.kotest.matchers.shouldBe

class YamlScalarTest : FlatFunSpec({
Expand Down Expand Up @@ -174,7 +180,13 @@ class YamlScalarTest : FlatFunSpec({
val result = scalar.toDouble()

test("converts it to the expected double") {
result shouldBe expectedResult
if (expectedResult.isNaN()) {
// comparing NaNs requires special code
// as they must not be compared via == / equals()
result.shouldBeNaN()
} else {
result shouldBe expectedResult
}
}
}
}
Expand Down Expand Up @@ -210,7 +222,13 @@ class YamlScalarTest : FlatFunSpec({
val result = scalar.toFloat()

test("converts it to the expected float") {
result shouldBe expectedResult
if (expectedResult.isNaN()) {
OptimumCode marked this conversation as resolved.
Show resolved Hide resolved
// comparing NaNs requires special code
// as they must not be compared via == / equals()
result.shouldBeNaN()
} else {
result shouldBe expectedResult
}
}
}
}
Expand All @@ -228,36 +246,47 @@ class YamlScalarTest : FlatFunSpec({
"+",
"",
).forEach { content ->

val ignoreValidFloatingPointsForJs: EnabledOrReasonIf = {
if (platform == Platform.JS && content in setOf("0x2", "0o2")) {
Enabled.disabled("$content is a valid floating value for JS due to dynamic cast")
} else {
Enabled.enabled
}
}
OptimumCode marked this conversation as resolved.
Show resolved Hide resolved

context("given a scalar with the content '$content'") {
val path = YamlPath.root.withListEntry(1, Location(2, 4))
val scalar = YamlScalar(content, path)

context("retrieving the value as a float") {
test("throws an appropriate exception") {
val exception = shouldThrow<YamlScalarFormatException> { scalar.toFloat() }

exception.asClue {
it.message shouldBe "Value '$content' is not a valid floating point value."
it.line shouldBe 2
it.column shouldBe 4
it.path shouldBe path
it.originalValue shouldBe content
test("throws an appropriate exception")
.config(enabledOrReasonIf = ignoreValidFloatingPointsForJs) {
val exception = shouldThrow<YamlScalarFormatException> { scalar.toFloat() }

exception.asClue {
it.message shouldBe "Value '$content' is not a valid floating point value."
it.line shouldBe 2
it.column shouldBe 4
it.path shouldBe path
it.originalValue shouldBe content
}
}
}
}

context("retrieving the value as a double") {
test("throws an appropriate exception") {
val exception = shouldThrow<YamlScalarFormatException> { scalar.toDouble() }

exception.asClue {
it.message shouldBe "Value '$content' is not a valid floating point value."
it.line shouldBe 2
it.column shouldBe 4
it.path shouldBe path
it.originalValue shouldBe content
test("throws an appropriate exception")
.config(enabledOrReasonIf = ignoreValidFloatingPointsForJs) {
val exception = shouldThrow<YamlScalarFormatException> { scalar.toDouble() }

exception.asClue {
it.message shouldBe "Value '$content' is not a valid floating point value."
it.line shouldBe 2
it.column shouldBe 4
it.path shouldBe path
it.originalValue shouldBe content
}
}
}
}
}
}
Expand Down
Loading