Clarify the purpose of the message parameter of assertFailsWith

#KT-22869 Fixed
This commit is contained in:
Ilya Gorbunov
2018-09-25 09:01:34 +03:00
parent 9f9033870c
commit 21b71f3bb1
4 changed files with 22 additions and 5 deletions
@@ -102,7 +102,11 @@ fun <@OnlyInputTypes T> expect(expected: T, message: String?, block: () -> T) {
/** Asserts that given function [block] fails by throwing an exception. */ /** Asserts that given function [block] fails by throwing an exception. */
fun assertFails(block: () -> Unit): Throwable = assertFails(null, block) fun assertFails(block: () -> Unit): Throwable = assertFails(null, block)
/** Asserts that given function [block] fails by throwing an exception. */ /**
* Asserts that given function [block] fails by throwing an exception.
*
* If the assertion fails, the specified [message] is used unless it is null as a prefix for the failure message.
*/
@SinceKotlin("1.1") @SinceKotlin("1.1")
fun assertFails(message: String?, block: () -> Unit): Throwable { fun assertFails(message: String?, block: () -> Unit): Throwable {
try { try {
@@ -115,7 +119,8 @@ fun assertFails(message: String?, block: () -> Unit): Throwable {
} }
/** 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.
* Since inline method doesn't allow to trace where it was invoked, it is required to pass a [message] to distinguish this method call from others. *
* If the assertion fails, the specified [message] is used unless it is null as a prefix for the failure message.
*/ */
@InlineOnly @InlineOnly
inline fun <reified T : Throwable> assertFailsWith(message: String? = null, noinline block: () -> Unit): T = inline fun <reified T : Throwable> assertFailsWith(message: String? = null, noinline block: () -> Unit): T =
@@ -13,5 +13,9 @@ import kotlin.reflect.KClass
*/ */
expect fun todo(block: () -> Unit) expect fun todo(block: () -> Unit)
/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */ /**
* 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.
*/
expect fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T expect fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T
@@ -17,7 +17,11 @@ actual fun todo(block: () -> Unit) {
} }
/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */ /**
* 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.
*/
actual fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T { actual fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T {
val exception = assertFails(message, block) val exception = assertFails(message, block)
@Suppress("INVISIBLE_MEMBER") @Suppress("INVISIBLE_MEMBER")
@@ -32,7 +32,11 @@ private fun <T : Throwable> assertFailsWithImpl(exceptionClass: Class<T>, messag
} }
/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */ /**
* 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.
*/
actual fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T = actual fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T =
assertFailsWithImpl(exceptionClass.java, message, block) assertFailsWithImpl(exceptionClass.java, message, block)