Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

feat: make raise matchers inline #272

Merged
merged 3 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import io.kotest.assertions.print.print
* }
* ```
*/
public suspend inline fun <reified T> shouldRaise(noinline block: suspend Raise<Any?>.() -> Any?): T {
public inline fun <reified T> shouldRaise(block: Raise<Any?>.() -> 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) {
Expand All @@ -40,10 +40,8 @@ public suspend inline fun <reified T> shouldRaise(noinline block: suspend Raise<
* }
* ```
*/
public suspend fun <T> shouldNotRaise(block: suspend Raise<Any?>.() -> T): T {
return recover({
block()
}) { raised ->
public inline fun <T> shouldNotRaise(block: Raise<Any?>.() -> T): T {
return recover(block) { raised ->
throw failure("No raise expected, but ${raised.print().value} was raised.")
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<Int> {
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<String> { raise("failed") }
test() shouldBe "failed"
}
})
Loading