Introduce 'fail' method to throw AssertionError with cause

#KT-37804
This commit is contained in:
Ilya Gorbunov
2020-01-26 00:02:43 +03:00
parent d2ff98fcb1
commit 2bb36899da
11 changed files with 95 additions and 13 deletions
@@ -57,7 +57,7 @@ internal object DefaultJsAsserter : Asserter {
override fun assertTrue(lazyMessage: () -> String?, actual: Boolean) {
if (!actual) {
failWithMessage(lazyMessage)
failWithMessage(lazyMessage, null)
} else {
invokeHook(true, lazyMessage)
}
@@ -68,16 +68,18 @@ internal object DefaultJsAsserter : Asserter {
}
override fun fail(message: String?): Nothing {
failWithMessage { message }
fail(message, null)
}
private fun failWithMessage(lazyMessage: () -> String?): Nothing {
@SinceKotlin("1.4")
override fun fail(message: String?, cause: Throwable?): Nothing {
failWithMessage({ message }, cause)
}
private inline fun failWithMessage(lazyMessage: () -> String?, cause: Throwable?): Nothing {
val message = lazyMessage()
invokeHook(false) { message }
if (message == null)
throw AssertionError()
else
throw AssertionError(message)
throw AssertionErrorWithCause(message, cause)
}
private fun invokeHook(result: Boolean, lazyMessage: () -> String?) {
@@ -17,6 +17,11 @@ actual fun todo(block: () -> Unit) {
println("TODO at " + block)
}
/** Platform-specific construction of AssertionError with cause */
@Suppress("NOTHING_TO_INLINE")
internal actual inline fun AssertionErrorWithCause(message: String?, cause: Throwable?): AssertionError =
AssertionError(message, cause)
@PublishedApi
internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Unit>): T {