kotlin-test: Make assertFailsWith<reified T: Throwable>() common

This commit is contained in:
Ilya Gorbunov
2017-04-19 08:13:52 +03:00
parent 307b132015
commit 2c32bae5ca
3 changed files with 1 additions and 25 deletions
@@ -114,13 +114,7 @@ fun assertFails(message: String?, block: () -> Unit): Throwable {
* 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.
*/
@InlineOnly
inline fun <reified T : Throwable> assertFailsWith(message: String? = null, noinline block: () -> Unit): T {
val exception = assertFails(message, block)
val messagePrefix = if (message == null) "" else "$message. "
assertTrue(exception is T, "${messagePrefix}An exception thrown is not of the expected type: $exception")
return exception as T
}
inline fun <reified T : Throwable> assertFailsWith(message: String? = null, noinline block: () -> Unit): T = assertFailsWith(T::class, message, block)
/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */
fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, block: () -> Unit): T = assertFailsWith(exceptionClass, null, block)
@@ -27,18 +27,7 @@ impl fun todo(block: () -> Unit) {
println("TODO at " + block)
}
/*
/** 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.
*/
inline fun <reified T : Throwable> assertFailsWith(message: String? = null, noinline block: () -> Unit): T {
val exception = assertFails(message, block)
val messagePrefix = if (message == null) "" else "$message. "
assertTrue(exception is T, "${messagePrefix}An exception thrown is not of the expected type: $exception")
return exception as T
}
*/
/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */
impl fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T {
val exception = assertFails(message, block)
@@ -45,13 +45,6 @@ private fun <T : Throwable> assertFailsWithImpl(exceptionClass: Class<T>, messag
/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */
impl fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T = assertFailsWithImpl(exceptionClass.java, message, block)
/*
/** 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.
*/
@InlineOnly
inline fun <reified T : Throwable> assertFailsWith(message: String? = null, noinline block: () -> Unit): T = assertFailsWith(T::class, message, block)
*/
/**
* Comments out a [block] of test code until it is implemented while keeping a link to the code