Skip to content

Commit

Permalink
kotlin-test: Make assertFailsWith(KClass<T: Throwable>) common
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-g committed Apr 21, 2017
1 parent ec8ead7 commit 307b132
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package kotlin.test

import kotlin.internal.*
import kotlin.reflect.KClass

/**
* Current adapter providing assertion implementations
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
12 changes: 12 additions & 0 deletions libraries/kotlin.test/js/src/main/kotlin/JsImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,6 +39,16 @@ 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. */
impl fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, 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/)
Expand Down
8 changes: 3 additions & 5 deletions libraries/kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,17 @@ private fun <T : Throwable> assertFailsWithImpl(exceptionClass: Class<T>, 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 <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. */
fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T = assertFailsWithImpl(exceptionClass.java, message, block)
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IllegalArgumentException>() {
throw IllegalArgumentException()
}
}
@Test
fun testFailsWithClassMessage() {
@Suppress("UNCHECKED_CAST")
Expand Down

0 comments on commit 307b132

Please sign in to comment.