Make assertFails(With) inline-only functions

So that the lambda passed to these functions can capture suspend
function calls.

#KT-31194 Fixed
This commit is contained in:
Ilya Gorbunov
2019-05-01 08:54:43 +03:00
parent 6b2d874ccc
commit 22694fa6b0
4 changed files with 70 additions and 63 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -11,35 +11,26 @@ package kotlin.test
import kotlin.internal.*
import kotlin.reflect.*
/** Asserts that a [block] fails with a specific exception being thrown. */
private fun <T : Throwable> assertFailsWithImpl(exceptionClass: Class<T>, message: String?, block: () -> Unit): T {
try {
block()
} catch (e: Throwable) {
if (exceptionClass.isInstance(e)) {
@Suppress("UNCHECKED_CAST")
return e as T
/** Asserts that a [blockResult] is a failure with the specific exception type being thrown. */
@PublishedApi
internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Unit>): T {
blockResult.fold(
onSuccess = {
val msg = messagePrefix(message)
asserter.fail(msg + "Expected an exception of ${exceptionClass.java} to be thrown, but was completed successfully.")
},
onFailure = { e ->
if (exceptionClass.java.isInstance(e)) {
@Suppress("UNCHECKED_CAST")
return e as T
}
asserter.fail(messagePrefix(message) + "Expected an exception of ${exceptionClass.java} to be thrown, but was $e")
}
asserter.fail(messagePrefix(message) + "Expected an exception of $exceptionClass to be thrown, but was $e")
}
val msg = messagePrefix(message)
asserter.fail(msg + "Expected an exception of $exceptionClass to be thrown, but was completed successfully.")
)
}
/**
* Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown.
*
* If the assertion fails, the specified [message] is used unless it is null as a prefix for the failure message.
*
* @return An exception of the expected exception type [T] that successfully caught.
* The returned exception can be inspected further, for example by asserting its property values.
*/
actual fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T =
assertFailsWithImpl(exceptionClass.java, message, block)
/**
* Takes the given [block] of test code and _doesn't_ execute it.