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

Commit 881a3bb

Browse files
authored
feat: make raise matchers inline (#272)
* feat: make raise matchers inline avoids propagation of suspend when raise/recover no longer requires it * test: suspend calls in shouldRaise/shouldNotRaise * test: shouldRaise/shouldNotRaise callable from non-suspend
1 parent 4212298 commit 881a3bb

File tree

2 files changed

+29
-6
lines changed
  • kotest-assertions-arrow/src
    • commonMain/kotlin/io/kotest/assertions/arrow/core
    • commonTest/kotlin/io/kotest/assertions/arrow/core

2 files changed

+29
-6
lines changed

kotest-assertions-arrow/src/commonMain/kotlin/io/kotest/assertions/arrow/core/Raise.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import io.kotest.assertions.print.print
1717
* }
1818
* ```
1919
*/
20-
public suspend inline fun <reified T> shouldRaise(noinline block: suspend Raise<Any?>.() -> Any?): T {
20+
public inline fun <reified T> shouldRaise(block: Raise<Any?>.() -> Any?): T {
2121
val expectedRaiseClass = T::class
2222
return recover({
23-
block()
23+
block(this)
2424
throw failure("Expected to raise ${expectedRaiseClass.simpleName} but nothing was raised.")
2525
}) { raised ->
2626
when (raised) {
@@ -40,10 +40,8 @@ public suspend inline fun <reified T> shouldRaise(noinline block: suspend Raise<
4040
* }
4141
* ```
4242
*/
43-
public suspend fun <T> shouldNotRaise(block: suspend Raise<Any?>.() -> T): T {
44-
return recover({
45-
block()
46-
}) { raised ->
43+
public inline fun <T> shouldNotRaise(block: Raise<Any?>.() -> T): T {
44+
return recover(block) { raised ->
4745
throw failure("No raise expected, but ${raised.print().value} was raised.")
4846
}
4947
}

kotest-assertions-arrow/src/commonTest/kotlin/io/kotest/assertions/arrow/core/RaiseMatchers.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.kotest.assertions.arrow.core
22

3+
import arrow.core.raise.Raise
34
import io.kotest.assertions.arrow.shouldBe
45
import io.kotest.assertions.throwables.shouldThrowWithMessage
56
import io.kotest.core.spec.style.StringSpec
@@ -72,4 +73,28 @@ class RaiseMatchers : StringSpec({
7273
}
7374
}
7475
}
76+
77+
"shouldNotRaise: allows suspend call in block" {
78+
val res = shouldNotRaise {
79+
suspend { 42 }()
80+
}
81+
res shouldBe 42
82+
}
83+
84+
"shouldRaise: allows suspend call in block" {
85+
val res = shouldRaise<Int> {
86+
raise(suspend { 42 }())
87+
}
88+
res shouldBe 42
89+
}
90+
91+
"shouldNotRaise: callable from non-suspend" {
92+
fun test() = shouldNotRaise { "success" }
93+
test() shouldBe "success"
94+
}
95+
96+
"shouldRaise: callable from non-suspend" {
97+
fun test() = shouldRaise<String> { raise("failed") }
98+
test() shouldBe "failed"
99+
}
75100
})

0 commit comments

Comments
 (0)