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.") } } 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..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 @@ -72,4 +73,28 @@ class RaiseMatchers : StringSpec({ } } } + + "shouldNotRaise: allows suspend call in block" { + val res = shouldNotRaise { + suspend { 42 }() + } + res shouldBe 42 + } + + "shouldRaise: allows suspend call in block" { + val res = shouldRaise { + 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" + } })