Skip to content

Commit

Permalink
Refactor GuaranteeCaseTest to use kotlin test
Browse files Browse the repository at this point in the history
  • Loading branch information
Gosunet committed Oct 20, 2023
1 parent 8bb1274 commit aa16aa7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 50 deletions.
15 changes: 5 additions & 10 deletions arrow-libs/fx/arrow-fx-coroutines/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<ExitCase>()

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<ExitCase>()

"release for error was invoked" {
checkAll(Arb.throwable()) { e ->
val p = CompletableDeferred<ExitCase>()
val attempted = Either.catch {
guaranteeCase<Int>(
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<ExitCase>()
val start = CompletableDeferred<Unit>()

val fiber = async {
guaranteeCase(
fa = {
start.complete(Unit)
awaitCancellation()
},
val attempted = Either.catch {
guaranteeCase<Int>(
fa = { throw e },
finalizer = { ex -> require(p.complete(ex)) }
)
}

start.await()
fiber.cancel()
p.await().shouldBeInstanceOf<ExitCase.Cancelled>()
p.await() shouldBe ExitCase.Failure(e)
attempted shouldBe Either.Left(e)
}
}
)

@Test
fun releaseForNeverWasInvoked() = runTest {
val p = CompletableDeferred<ExitCase>()
val start = CompletableDeferred<Unit>()

val fiber = async {
guaranteeCase(
fa = {
start.complete(Unit)
awaitCancellation()
},
finalizer = { ex -> require(p.complete(ex)) }
)
}

start.await()
fiber.cancel()
p.await().shouldBeInstanceOf<ExitCase.Cancelled>()
}
}
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down

0 comments on commit aa16aa7

Please sign in to comment.