kotlin.test: 'fail' returns Nothing, 'assertFails' returns Throwable (not nullable).

Js tests now fail at the first failed assertion.
#KT-10289 Fixed
#KT-10369 Fixed
This commit is contained in:
Ilya Gorbunov
2016-01-13 04:16:42 +03:00
parent 8fdd8179b9
commit f91c01919b
9 changed files with 36 additions and 20 deletions
@@ -35,7 +35,9 @@ object JUnitAsserter : Asserter {
Assert.assertNull(message ?: "actual value is not null", actual)
}
override fun fail(message : String?) {
override fun fail(message : String?): Nothing {
Assert.fail(message)
// should not get here
throw AssertionError(message)
}
}
@@ -22,11 +22,9 @@ fun <T : Throwable> assertFailsWith(exceptionClass: Class<T>, message: String?,
return e as T
}
asserter.fail((message?.let { "$it. " } ?: "") + "Expected an exception of type $exceptionClass to be thrown, but was $e")
throw e
}
val msg = message?.let { "$it. " } ?: ""
asserter.fail(msg + "Expected an exception of type $exceptionClass to be thrown, but was completed successfully.")
throw IllegalStateException(msg + "Should have failed.")
}
/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */
@@ -5,7 +5,7 @@ package kotlin.test
*/
class DefaultAsserter() : Asserter {
override fun fail(message: String?) {
override fun fail(message: String?): Nothing {
if (message == null)
throw AssertionError()
else
@@ -63,7 +63,7 @@ fun assertNull(actual: Any?, message: String? = null) {
}
/** Marks a test as having failed if this point in the execution path is reached, with an optional [message]. */
fun fail(message: String? = null) {
fun fail(message: String? = null): Nothing {
asserter.fail(message)
}
@@ -81,16 +81,13 @@ fun <@OnlyInputTypes T> expect(expected: T, message: String?, block: () -> T) {
fun fails(block: () -> Unit): Throwable? = assertFails(block)
/** Asserts that given function [block] fails by throwing an exception. */
fun assertFails(block: () -> Unit): Throwable? {
var thrown: Throwable? = null
fun assertFails(block: () -> Unit): Throwable {
try {
block()
} catch (e: Throwable) {
thrown = e
return e
}
if (thrown == null)
asserter.fail("Expected an exception to be thrown")
return thrown
asserter.fail("Expected an exception to be thrown")
}
/**
@@ -103,7 +100,7 @@ interface Asserter {
*
* @param message the message to report.
*/
fun fail(message: String?): Unit
fun fail(message: String?): Nothing
/**
* Asserts that the specified value is `true`.