CLOVIS
04/11/2023, 7:36 PMRaise
? It's really tempting to create a
object TestRaise : Raise<Any> {
override suspend fun <B> raise(r: Any): Nothing {
throw AssertionError("Unexpected result from $r")
}
}
to be able to write tests like
runTest {
val result = expensiveOperation().bind()
result shouldBe 42
}
instead of
runTest {
val result = expensiveOperation()
result shouldBe 42.right()
}
gpopides
04/11/2023, 8:06 PMresult.shouldBeRight(42)
CLOVIS
04/11/2023, 8:10 PMYoussef Shoaib [MOD]
04/11/2023, 8:43 PMrunTest
be:
inline fun runTest(block: Raise<Any>().() -> Unit) {
either {
block().mapLeft { throw AssertionError("Unexpected result from $it") }
}
}
Youssef Shoaib [MOD]
04/11/2023, 8:49 PMRaise
, and the only reason it's an interface is because of NullableRaise
, OptionRaise
, etc, which will disappear when context receivers are stablesimon.vergauwen
04/11/2023, 10:04 PMP A
04/12/2023, 6:16 AMsimon.vergauwen
04/12/2023, 6:37 AMCLOVIS
04/12/2023, 7:27 AMYoussef Shoaib [MOD]
04/12/2023, 7:29 AMraise
Youssef Shoaib [MOD]
04/12/2023, 7:31 AMrespond
function does something similar internally where it takes care of throwing an exception (likely one with an HTTP status code or something) in case of a raised value, so it's a similar pattern as what you're doing for testsCLOVIS
04/12/2023, 7:46 AMRaise
receiver in tests, keep the implementation that throws an AssertionError for now, and replace it with the tracing API whenever that reaches my projects.simon.vergauwen
04/12/2023, 10:09 AMOptIn
on it though 😅
So please test and use it, so we can remove it before 2.0 or refine the API first.CLOVIS
04/12/2023, 12:42 PMYoussef Shoaib [MOD]
04/12/2023, 5:45 PMeither<RuntimeException, Nothing> {
traced({ raise(RuntimeException("")) }) { traced, raised ->
// Remove first 2 lines:
// arrow.core.raise.RaiseCancellationException
// at arrow.core.raise.DefaultRaise.raise(Fold.kt:187)
val trace = traced.stackTraceToString().lines().drop(2)
// Remove first line:
// java.lang.RuntimeException:
val exceptionTrace = raised.stackTraceToString().lines().drop(1)
trace shouldBe exceptionTrace
}
}
Shows how to get the stack trace out of it.