Skip to content

Commit

Permalink
Merge pull request #584 from OptimumCode/enable-js-test
Browse files Browse the repository at this point in the history
Update tests to run for JS targets
  • Loading branch information
charleskorn authored Jun 30, 2024
2 parents b5d157f + 687bd33 commit c23a906
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 34 deletions.
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()) {
// 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
}
}

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

0 comments on commit c23a906

Please sign in to comment.