Add test case for #96

This commit is contained in:
Michael Bull 2024-04-10 15:17:15 +01:00
parent 3a50dfa69d
commit c39888e01b
2 changed files with 21 additions and 1 deletions

View File

@ -33,7 +33,7 @@ public inline fun <V, E> binding(crossinline block: BindingScope<E>.() -> V): Re
return with(BindingScopeImpl<E>()) {
try {
Ok(block())
} catch (ex: BindException) {
} catch (_: BindException) {
result!!
}
}

View File

@ -78,4 +78,24 @@ class BindingTest {
actual = result,
)
}
@Test
fun runCatchingInsideBindingDoesNotSwallow() {
fun squareNumber(): Result<Int, BindingError> = throw RuntimeException()
val squaredNumbers = binding<List<Int>, BindingError> {
val result: Result<List<Int>, Throwable> = runCatching {
(0..<10).map { number ->
squareNumber().bind()
}
}
result.mapError { BindingError }.bind()
}
assertEquals(
expected = Err(BindingError),
actual = squaredNumbers,
)
}
}