From c39888e01b084e1a30ce221c336240c459cf00bc Mon Sep 17 00:00:00 2001 From: Michael Bull Date: Wed, 10 Apr 2024 15:17:15 +0100 Subject: [PATCH] Add test case for #96 --- .../com/github/michaelbull/result/Binding.kt | 2 +- .../github/michaelbull/result/BindingTest.kt | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt index b2e4533..64ce3a6 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt @@ -33,7 +33,7 @@ public inline fun binding(crossinline block: BindingScope.() -> V): Re return with(BindingScopeImpl()) { try { Ok(block()) - } catch (ex: BindException) { + } catch (_: BindException) { result!! } } diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/BindingTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/BindingTest.kt index ebbfb18..06ff2ab 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/BindingTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/BindingTest.kt @@ -78,4 +78,24 @@ class BindingTest { actual = result, ) } + + @Test + fun runCatchingInsideBindingDoesNotSwallow() { + fun squareNumber(): Result = throw RuntimeException() + + val squaredNumbers = binding, BindingError> { + val result: Result, Throwable> = runCatching { + (0..<10).map { number -> + squareNumber().bind() + } + } + + result.mapError { BindingError }.bind() + } + + assertEquals( + expected = Err(BindingError), + actual = squaredNumbers, + ) + } }