diff --git a/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Assertions.kt b/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Assertions.kt index 76e2d1ab331..57f0ce05ebb 100644 --- a/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Assertions.kt +++ b/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Assertions.kt @@ -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 assertFailsWith(message: String? = null, noin return exception as T } +/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */ +fun assertFailsWith(exceptionClass: KClass, block: () -> Unit): T = assertFailsWith(exceptionClass, null, block) + + /** * Abstracts the logic for performing assertions. Specific implementations of [Asserter] can use JUnit * or TestNG assertion facilities. diff --git a/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/AssertionsH.kt b/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/AssertionsH.kt index 4ac7e787d4c..5c1cd282408 100644 --- a/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/AssertionsH.kt +++ b/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/AssertionsH.kt @@ -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) \ No newline at end of file +header fun todo(block: () -> Unit) + +/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */ +header fun assertFailsWith(exceptionClass: KClass, message: String?, block: () -> Unit): T diff --git a/libraries/kotlin.test/common/src/test/kotlin/kotlin/test/tests/BasicAssertionsTest.kt b/libraries/kotlin.test/common/src/test/kotlin/kotlin/test/tests/BasicAssertionsTest.kt index c177b72cee1..00948a945cd 100644 --- a/libraries/kotlin.test/common/src/test/kotlin/kotlin/test/tests/BasicAssertionsTest.kt +++ b/libraries/kotlin.test/common/src/test/kotlin/kotlin/test/tests/BasicAssertionsTest.kt @@ -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) } diff --git a/libraries/kotlin.test/js/src/main/kotlin/JsImpl.kt b/libraries/kotlin.test/js/src/main/kotlin/JsImpl.kt index b8f2732fa6b..41f5372d404 100644 --- a/libraries/kotlin.test/js/src/main/kotlin/JsImpl.kt +++ b/libraries/kotlin.test/js/src/main/kotlin/JsImpl.kt @@ -16,6 +16,8 @@ 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 @@ -37,6 +39,16 @@ inline fun assertFailsWith(message: String? = null, noin return exception as T } */ +/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */ +impl fun assertFailsWith(exceptionClass: KClass, message: String?, block: () -> Unit): T { + val exception = assertFails(message, block) + @Suppress("INVISIBLE_MEMBER") + assertTrue(exceptionClass.isInstance(exception), messagePrefix(message) + "Expected an exception of $exceptionClass to be thrown, but was $exception") + + @Suppress("UNCHECKED_CAST") + return exception as T +} + /** * Provides the JS implementation of asserter using [QUnit](http://QUnitjs.com/) diff --git a/libraries/kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt b/libraries/kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt index 32f07aacfba..d3aa1df5cc8 100644 --- a/libraries/kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt +++ b/libraries/kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt @@ -33,19 +33,17 @@ private fun assertFailsWithImpl(exceptionClass: Class, messag } @Suppress("INVISIBLE_MEMBER") - asserter.fail(messagePrefix(message) + "Expected an exception of type $exceptionClass to be thrown, but was $e") + asserter.fail(messagePrefix(message) + "Expected an exception of $exceptionClass to be thrown, but was $e") } @Suppress("INVISIBLE_MEMBER") val msg = messagePrefix(message) - asserter.fail(msg + "Expected an exception of type $exceptionClass to be thrown, but was completed successfully.") + 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. */ -fun assertFailsWith(exceptionClass: KClass, block: () -> Unit): T = assertFailsWith(exceptionClass, null, block) /** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */ -fun assertFailsWith(exceptionClass: KClass, message: String?, block: () -> Unit): T = assertFailsWithImpl(exceptionClass.java, message, block) +impl fun assertFailsWith(exceptionClass: KClass, message: String?, block: () -> Unit): T = assertFailsWithImpl(exceptionClass.java, message, block) /* /** Asserts that a [block] fails with a specific exception of type [T] being thrown. diff --git a/libraries/kotlin.test/jvm/src/test/kotlin/BasicAssertionsJVMTest.kt b/libraries/kotlin.test/jvm/src/test/kotlin/BasicAssertionsJVMTest.kt index 1c417f6283f..765a6a59d0c 100644 --- a/libraries/kotlin.test/jvm/src/test/kotlin/BasicAssertionsJVMTest.kt +++ b/libraries/kotlin.test/jvm/src/test/kotlin/BasicAssertionsJVMTest.kt @@ -4,26 +4,7 @@ import kotlin.test.* import org.junit.* class BasicAssertionsJVMTest { - @Test - fun testFailsWith() { - assertFailsWith(IllegalArgumentException::class) { - throw IllegalArgumentException() - } - } - @Test(expected = AssertionError::class) - fun testFailsWithFails() { - assertFailsWith(IllegalArgumentException::class) { - throw IllegalStateException() - } - } - - @Test - fun testFailsWithMessage() { - assertFailsWith() { - throw IllegalArgumentException() - } - } @Test fun testFailsWithClassMessage() { @Suppress("UNCHECKED_CAST")