From aa16aa78722c94eef80a7d2a357debe3db9d4f8b Mon Sep 17 00:00:00 2001 From: Jonathan Lagneaux Date: Fri, 20 Oct 2023 21:08:40 +0200 Subject: [PATCH] Refactor GuaranteeCaseTest to use kotlin test Closes #3190 --- .../fx/arrow-fx-coroutines/build.gradle.kts | 15 ++-- .../arrow/fx/coroutines/GuaranteeCaseTest.kt | 83 ++++++++++--------- gradle/libs.versions.toml | 1 + 3 files changed, 49 insertions(+), 50 deletions(-) diff --git a/arrow-libs/fx/arrow-fx-coroutines/build.gradle.kts b/arrow-libs/fx/arrow-fx-coroutines/build.gradle.kts index b365da6e4cb..d89c703e5c9 100644 --- a/arrow-libs/fx/arrow-fx-coroutines/build.gradle.kts +++ b/arrow-libs/fx/arrow-fx-coroutines/build.gradle.kts @@ -29,16 +29,11 @@ kotlin { commonTest { dependencies { implementation(projects.arrowCore) - implementation(libs.kotest.frameworkEngine) - implementation(libs.kotest.assertionsCore) - implementation(libs.kotest.property) - implementation(libs.coroutines.test) - } - } - jvmTest { - dependencies { - runtimeOnly(libs.kotest.runnerJUnit5) - + implementation(libs.kotest.frameworkEngine) + implementation(libs.kotest.assertionsCore) + implementation(libs.kotest.property) + implementation(libs.coroutines.test) + implementation(libs.kotlin.test) } } diff --git a/arrow-libs/fx/arrow-fx-coroutines/src/commonTest/kotlin/arrow/fx/coroutines/GuaranteeCaseTest.kt b/arrow-libs/fx/arrow-fx-coroutines/src/commonTest/kotlin/arrow/fx/coroutines/GuaranteeCaseTest.kt index 2f636a7b381..1795c98fc0f 100644 --- a/arrow-libs/fx/arrow-fx-coroutines/src/commonTest/kotlin/arrow/fx/coroutines/GuaranteeCaseTest.kt +++ b/arrow-libs/fx/arrow-fx-coroutines/src/commonTest/kotlin/arrow/fx/coroutines/GuaranteeCaseTest.kt @@ -1,7 +1,6 @@ package arrow.fx.coroutines import arrow.core.Either -import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe import io.kotest.matchers.types.shouldBeInstanceOf import io.kotest.property.Arb @@ -10,55 +9,59 @@ import io.kotest.property.checkAll import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.async import kotlinx.coroutines.awaitCancellation +import kotlinx.coroutines.test.runTest +import kotlin.test.Test -class GuaranteeCaseTest : StringSpec({ +class GuaranteeCaseTest { - "release for success was invoked" { - checkAll(Arb.int()) { i -> - val p = CompletableDeferred() - - val res = guaranteeCase( - fa = { i }, - finalizer = { ex -> require(p.complete(ex)) } - ) - - p.await() shouldBe ExitCase.Completed - res shouldBe i - } - } + @Test + fun releaseForSuccessWasInvoked() = runTest { + checkAll(Arb.int()) { i -> + val p = CompletableDeferred() - "release for error was invoked" { - checkAll(Arb.throwable()) { e -> - val p = CompletableDeferred() - val attempted = Either.catch { - guaranteeCase( - fa = { throw e }, - finalizer = { ex -> require(p.complete(ex)) } - ) - } + val res = guaranteeCase( + fa = { i }, + finalizer = { ex -> require(p.complete(ex)) } + ) - p.await() shouldBe ExitCase.Failure(e) - attempted shouldBe Either.Left(e) - } + p.await() shouldBe ExitCase.Completed + res shouldBe i } + } - "release for never was invoked" { + @Test + fun releaseForErrorWasInvoked() = runTest { + checkAll(Arb.throwable()) { e -> val p = CompletableDeferred() - val start = CompletableDeferred() - - val fiber = async { - guaranteeCase( - fa = { - start.complete(Unit) - awaitCancellation() - }, + val attempted = Either.catch { + guaranteeCase( + fa = { throw e }, finalizer = { ex -> require(p.complete(ex)) } ) } - start.await() - fiber.cancel() - p.await().shouldBeInstanceOf() + p.await() shouldBe ExitCase.Failure(e) + attempted shouldBe Either.Left(e) } } -) + + @Test + fun releaseForNeverWasInvoked() = runTest { + val p = CompletableDeferred() + val start = CompletableDeferred() + + val fiber = async { + guaranteeCase( + fa = { + start.complete(Unit) + awaitCancellation() + }, + finalizer = { ex -> require(p.complete(ex)) } + ) + } + + start.await() + fiber.cancel() + p.await().shouldBeInstanceOf() + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index a7a1200778b..8cd079241da 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -39,6 +39,7 @@ kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect" } kotlin-stdlibCommon = { module = "org.jetbrains.kotlin:kotlin-stdlib-common" } kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib" } kotlin-stdlibJS = { module = "org.jetbrains.kotlin:kotlin-stdlib-js" } +kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test" } kotlinx-knit = { module = "org.jetbrains.kotlinx:kotlinx-knit", version.ref = "knit" } kotlinx-serializationCore = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinxSerialization" } kotlinx-serializationJson = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerialization" }