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

This commit is contained in:
Ilya Gorbunov
2017-04-19 08:07:33 +03:00
parent ec8ead754f
commit 307b132015
6 changed files with 44 additions and 25 deletions
@@ -24,6 +24,7 @@
package kotlin.test
import kotlin.internal.*
import kotlin.reflect.KClass
/**
* Current adapter providing assertion implementations
@@ -121,6 +122,10 @@ inline fun <reified T : Throwable> assertFailsWith(message: String? = null, noin
return exception as T
}
/** 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)
/**
* Abstracts the logic for performing assertions. Specific implementations of [Asserter] can use JUnit
* or TestNG assertion facilities.
@@ -1,7 +1,12 @@
package kotlin.test
import kotlin.reflect.KClass
/**
* Comments out a block of test code until it is implemented while keeping a link to the code
* to implement in your unit test output
*/
header fun todo(block: () -> Unit)
header fun todo(block: () -> Unit)
/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */
header fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T
@@ -44,6 +44,24 @@ class BasicAssertionsTest {
}
}
@Test
fun testAssertFailsWithClass() {
assertFailsWith(IllegalArgumentException::class) {
throw IllegalArgumentException("This is illegal")
}
}
@Test
fun testAssertFailsWithClassFails() {
checkFailedAssertion {
assertFailsWith(IllegalArgumentException::class) { throw IllegalStateException() }
}
checkFailedAssertion {
assertFailsWith(Exception::class) { }
}
}
@Test
fun testAssertEqualsFails() {
checkFailedAssertion { assertEquals(1, 2) }