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. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@@ -107,7 +107,9 @@ fun <@OnlyInputTypes T> expect(expected: T, message: String?, block: () -> T) {
* @return An exception that was expected to be thrown and was successfully caught. * @return An exception that was expected to be thrown and was successfully caught.
* The returned exception can be inspected further, for example by asserting its property values. * The returned exception can be inspected further, for example by asserting its property values.
*/ */
fun assertFails(block: () -> Unit): Throwable = assertFails(null, block) @InlineOnly
inline fun assertFails(block: () -> Unit): Throwable =
checkResultIsFailure(null, runCatching(block))
/** /**
* Asserts that given function [block] fails by throwing an exception. * Asserts that given function [block] fails by throwing an exception.
@@ -118,14 +120,20 @@ fun assertFails(block: () -> Unit): Throwable = assertFails(null, block)
* The returned exception can be inspected further, for example by asserting its property values. * The returned exception can be inspected further, for example by asserting its property values.
*/ */
@SinceKotlin("1.1") @SinceKotlin("1.1")
fun assertFails(message: String?, block: () -> Unit): Throwable { @InlineOnly
try { inline fun assertFails(message: String?, block: () -> Unit): Throwable =
block() checkResultIsFailure(message, runCatching(block))
} catch (e: Throwable) {
assertEquals(e.message, e.message) // success path assertion for qunit @PublishedApi
return e internal fun checkResultIsFailure(message: String?, blockResult: Result<Unit>): Throwable {
} blockResult.fold(
asserter.fail(messagePrefix(message) + "Expected an exception to be thrown, but was completed successfully.") onSuccess = {
asserter.fail(messagePrefix(message) + "Expected an exception to be thrown, but was completed successfully.")
},
onFailure = { e ->
return e
}
)
} }
/** Asserts that a [block] fails with a specific exception of type [T] being thrown. /** Asserts that a [block] fails with a specific exception of type [T] being thrown.
@@ -136,7 +144,7 @@ fun assertFails(message: String?, block: () -> Unit): Throwable {
* The returned exception can be inspected further, for example by asserting its property values. * The returned exception can be inspected further, for example by asserting its property values.
*/ */
@InlineOnly @InlineOnly
inline fun <reified T : Throwable> assertFailsWith(message: String? = null, noinline block: () -> Unit): T = inline fun <reified T : Throwable> assertFailsWith(message: String? = null, block: () -> Unit): T =
assertFailsWith(T::class, message, block) assertFailsWith(T::class, message, block)
/** /**
@@ -145,7 +153,21 @@ inline fun <reified T : Throwable> assertFailsWith(message: String? = null, noin
* @return An exception of the expected exception type [T] that successfully caught. * @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. * The returned exception can be inspected further, for example by asserting its property values.
*/ */
fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, block: () -> Unit): T = assertFailsWith(exceptionClass, null, block) @InlineOnly
inline fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, block: () -> Unit): T = assertFailsWith(exceptionClass, null, block)
/**
* 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.
*/
@InlineOnly
inline fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T =
checkResultIsFailure(exceptionClass, message, runCatching(block))
/** /**
@@ -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. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@@ -14,12 +14,6 @@ import kotlin.reflect.KClass
*/ */
expect fun todo(block: () -> Unit) expect fun todo(block: () -> Unit)
/** /** Asserts that a [blockResult] is a failure with the specific exception type being thrown. */
* Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. @PublishedApi
* internal expect fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Unit>): T
* 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.
*/
expect fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T
@@ -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. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@@ -18,20 +18,20 @@ actual fun todo(block: () -> Unit) {
} }
/** @PublishedApi
* Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Unit>): T {
* blockResult.fold(
* If the assertion fails, the specified [message] is used unless it is null as a prefix for the failure message. onSuccess = {
* asserter.fail(messagePrefix(message) + "Expected an exception of $exceptionClass to be thrown, but was completed successfully.")
* @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. onFailure = { e ->
*/ if (exceptionClass.isInstance(e)) {
actual fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T { @Suppress("UNCHECKED_CAST")
val exception = assertFails(message, block) return e as T
assertTrue(exceptionClass.isInstance(exception), messagePrefix(message) + "Expected an exception of $exceptionClass to be thrown, but was $exception") }
asserter.fail(messagePrefix(message) + "Expected an exception of $exceptionClass to be thrown, but was $e")
@Suppress("UNCHECKED_CAST") }
return exception as T )
} }
@@ -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. * 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.internal.*
import kotlin.reflect.* import kotlin.reflect.*
/** Asserts that a [block] fails with a specific exception being thrown. */ /** Asserts that a [blockResult] is a failure with the specific exception type being thrown. */
private fun <T : Throwable> assertFailsWithImpl(exceptionClass: Class<T>, message: String?, block: () -> Unit): T { @PublishedApi
try { internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Unit>): T {
block() blockResult.fold(
} catch (e: Throwable) { onSuccess = {
if (exceptionClass.isInstance(e)) { val msg = messagePrefix(message)
@Suppress("UNCHECKED_CAST") asserter.fail(msg + "Expected an exception of ${exceptionClass.java} to be thrown, but was completed successfully.")
return e as T },
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. * Takes the given [block] of test code and _doesn't_ execute it.