From a199f9b78fd3978894ae2ab3bf36dae6e91068fa Mon Sep 17 00:00:00 2001 From: Oleg Date: Thu, 27 Jun 2024 20:07:59 +0400 Subject: [PATCH 1/3] Update tests to run for JS targets --- build.gradle.kts | 14 +--- .../com/charleskorn/kaml/YamlScalarTest.kt | 76 +++++++++++++------ 2 files changed, 56 insertions(+), 34 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 3f08da47..13fdd531 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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() } diff --git a/src/commonTest/kotlin/com/charleskorn/kaml/YamlScalarTest.kt b/src/commonTest/kotlin/com/charleskorn/kaml/YamlScalarTest.kt index 6c274760..12522cae 100644 --- a/src/commonTest/kotlin/com/charleskorn/kaml/YamlScalarTest.kt +++ b/src/commonTest/kotlin/com/charleskorn/kaml/YamlScalarTest.kt @@ -20,6 +20,11 @@ package com.charleskorn.kaml import io.kotest.assertions.asClue import io.kotest.assertions.throwables.shouldThrow +import io.kotest.assertions.withClue +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.shouldBe class YamlScalarTest : FlatFunSpec({ @@ -174,7 +179,16 @@ class YamlScalarTest : FlatFunSpec({ val result = scalar.toDouble() test("converts it to the expected double") { - result shouldBe expectedResult + if (expectedResult.isNaN()) { + // this is not correct to compare NaNs + // instead isNaN method should be invoked + // to check whether the value is NaN or not + withClue("$result should be NaN") { + result.isNaN() shouldBe true + } + } else { + result shouldBe expectedResult + } } } } @@ -210,7 +224,16 @@ class YamlScalarTest : FlatFunSpec({ val result = scalar.toFloat() test("converts it to the expected float") { - result shouldBe expectedResult + if (expectedResult.isNaN()) { + // this is not correct to compare NaNs + // instead isNaN method should be invoked + // to check whether the value is NaN or not + withClue("$result should be NaN") { + result.isNaN() shouldBe true + } + } else { + result shouldBe expectedResult + } } } } @@ -228,36 +251,45 @@ 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 { 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 { 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 { 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 { 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 + } } - } } } } From 3a0864cbd0a2a2efd91da61dfa05c237a6ff7639 Mon Sep 17 00:00:00 2001 From: Oleg Date: Thu, 27 Jun 2024 21:14:27 +0400 Subject: [PATCH 2/3] Use dedicated shouldBeNaN function. Correct comments. Add additional blank like to separate conditional block --- .../com/charleskorn/kaml/YamlScalarTest.kt | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/commonTest/kotlin/com/charleskorn/kaml/YamlScalarTest.kt b/src/commonTest/kotlin/com/charleskorn/kaml/YamlScalarTest.kt index 12522cae..dba7ed36 100644 --- a/src/commonTest/kotlin/com/charleskorn/kaml/YamlScalarTest.kt +++ b/src/commonTest/kotlin/com/charleskorn/kaml/YamlScalarTest.kt @@ -25,6 +25,8 @@ 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({ @@ -180,12 +182,9 @@ class YamlScalarTest : FlatFunSpec({ test("converts it to the expected double") { if (expectedResult.isNaN()) { - // this is not correct to compare NaNs - // instead isNaN method should be invoked - // to check whether the value is NaN or not - withClue("$result should be NaN") { - result.isNaN() shouldBe true - } + // comparing NaNs requires special code + // as they must not be compared via == / equals() + result.shouldBeNaN() } else { result shouldBe expectedResult } @@ -225,12 +224,9 @@ class YamlScalarTest : FlatFunSpec({ test("converts it to the expected float") { if (expectedResult.isNaN()) { - // this is not correct to compare NaNs - // instead isNaN method should be invoked - // to check whether the value is NaN or not - withClue("$result should be NaN") { - result.isNaN() shouldBe true - } + // comparing NaNs requires special code + // as they must not be compared via == / equals() + result.shouldBeNaN() } else { result shouldBe expectedResult } @@ -251,6 +247,7 @@ 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") @@ -258,6 +255,7 @@ class YamlScalarTest : FlatFunSpec({ Enabled.enabled } } + context("given a scalar with the content '$content'") { val path = YamlPath.root.withListEntry(1, Location(2, 4)) val scalar = YamlScalar(content, path) From 687bd332e2e5921bb506b393d558fb640c40b78f Mon Sep 17 00:00:00 2001 From: Oleg Date: Thu, 27 Jun 2024 21:19:46 +0400 Subject: [PATCH 3/3] Remove unused import --- src/commonTest/kotlin/com/charleskorn/kaml/YamlScalarTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/commonTest/kotlin/com/charleskorn/kaml/YamlScalarTest.kt b/src/commonTest/kotlin/com/charleskorn/kaml/YamlScalarTest.kt index dba7ed36..5ccb34b1 100644 --- a/src/commonTest/kotlin/com/charleskorn/kaml/YamlScalarTest.kt +++ b/src/commonTest/kotlin/com/charleskorn/kaml/YamlScalarTest.kt @@ -20,7 +20,6 @@ package com.charleskorn.kaml import io.kotest.assertions.asClue import io.kotest.assertions.throwables.shouldThrow -import io.kotest.assertions.withClue import io.kotest.common.Platform import io.kotest.common.platform import io.kotest.core.test.Enabled