From a44d2c942e722492c151583414c186ce1bff19ec Mon Sep 17 00:00:00 2001 From: Phil Davies Date: Wed, 22 Jan 2025 10:29:15 +0000 Subject: [PATCH 1/3] feat: make raise matchers inline avoids propagation of suspend when raise/recover no longer requires it --- .../kotlin/io/kotest/assertions/arrow/core/Raise.kt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/kotest-assertions-arrow/src/commonMain/kotlin/io/kotest/assertions/arrow/core/Raise.kt b/kotest-assertions-arrow/src/commonMain/kotlin/io/kotest/assertions/arrow/core/Raise.kt index 7ac8c48..246abee 100644 --- a/kotest-assertions-arrow/src/commonMain/kotlin/io/kotest/assertions/arrow/core/Raise.kt +++ b/kotest-assertions-arrow/src/commonMain/kotlin/io/kotest/assertions/arrow/core/Raise.kt @@ -17,10 +17,10 @@ import io.kotest.assertions.print.print * } * ``` */ -public suspend inline fun shouldRaise(noinline block: suspend Raise.() -> Any?): T { +public inline fun shouldRaise(block: Raise.() -> Any?): T { val expectedRaiseClass = T::class return recover({ - block() + block(this) throw failure("Expected to raise ${expectedRaiseClass.simpleName} but nothing was raised.") }) { raised -> when (raised) { @@ -40,10 +40,8 @@ public suspend inline fun shouldRaise(noinline block: suspend Raise< * } * ``` */ -public suspend fun shouldNotRaise(block: suspend Raise.() -> T): T { - return recover({ - block() - }) { raised -> +public inline fun shouldNotRaise(block: Raise.() -> T): T { + return recover(block) { raised -> throw failure("No raise expected, but ${raised.print().value} was raised.") } } From 6712cd010a0ed4b3b350c49db6d0c7ebe89b70a3 Mon Sep 17 00:00:00 2001 From: Phil Davies Date: Wed, 22 Jan 2025 10:35:59 +0000 Subject: [PATCH 2/3] test: suspend calls in shouldRaise/shouldNotRaise --- .../kotest/assertions/arrow/core/RaiseMatchers.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/kotest-assertions-arrow/src/commonTest/kotlin/io/kotest/assertions/arrow/core/RaiseMatchers.kt b/kotest-assertions-arrow/src/commonTest/kotlin/io/kotest/assertions/arrow/core/RaiseMatchers.kt index 59ccdea..0b1ce47 100644 --- a/kotest-assertions-arrow/src/commonTest/kotlin/io/kotest/assertions/arrow/core/RaiseMatchers.kt +++ b/kotest-assertions-arrow/src/commonTest/kotlin/io/kotest/assertions/arrow/core/RaiseMatchers.kt @@ -72,4 +72,18 @@ class RaiseMatchers : StringSpec({ } } } + + "shouldNotRaise: allows suspend call in block" { + val res = shouldNotRaise { + suspend { 42 }.invoke() + } + res shouldBe 42 + } + + "shouldRaise: allows suspend call in block" { + val res = shouldRaise { + raise(suspend { 42 }.invoke()) + } + res shouldBe 42 + } }) From c7bb0bc5c3f421a2f917433fbb37611149b309cb Mon Sep 17 00:00:00 2001 From: Phil Davies Date: Wed, 22 Jan 2025 10:44:07 +0000 Subject: [PATCH 3/3] test: shouldRaise/shouldNotRaise callable from non-suspend --- .../kotest/assertions/arrow/core/RaiseMatchers.kt | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/kotest-assertions-arrow/src/commonTest/kotlin/io/kotest/assertions/arrow/core/RaiseMatchers.kt b/kotest-assertions-arrow/src/commonTest/kotlin/io/kotest/assertions/arrow/core/RaiseMatchers.kt index 0b1ce47..99ad0cc 100644 --- a/kotest-assertions-arrow/src/commonTest/kotlin/io/kotest/assertions/arrow/core/RaiseMatchers.kt +++ b/kotest-assertions-arrow/src/commonTest/kotlin/io/kotest/assertions/arrow/core/RaiseMatchers.kt @@ -1,5 +1,6 @@ package io.kotest.assertions.arrow.core +import arrow.core.raise.Raise import io.kotest.assertions.arrow.shouldBe import io.kotest.assertions.throwables.shouldThrowWithMessage import io.kotest.core.spec.style.StringSpec @@ -75,15 +76,25 @@ class RaiseMatchers : StringSpec({ "shouldNotRaise: allows suspend call in block" { val res = shouldNotRaise { - suspend { 42 }.invoke() + suspend { 42 }() } res shouldBe 42 } "shouldRaise: allows suspend call in block" { val res = shouldRaise { - raise(suspend { 42 }.invoke()) + raise(suspend { 42 }()) } res shouldBe 42 } + + "shouldNotRaise: callable from non-suspend" { + fun test() = shouldNotRaise { "success" } + test() shouldBe "success" + } + + "shouldRaise: callable from non-suspend" { + fun test() = shouldRaise { raise("failed") } + test() shouldBe "failed" + } })