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
+3
View File
@@ -27,6 +27,9 @@ public class NumberFormatException(message: String? = null) : RuntimeException(m
@library @library
public class NullPointerException(message: String? = null) : RuntimeException(message) {} public class NullPointerException(message: String? = null) : RuntimeException(message) {}
@library
public class AssertionError(message: String? = null) : Error(message) {}
@library @library
public interface Runnable { public interface Runnable {
public open fun run() : Unit; public open fun run() : Unit;
+11 -2
View File
@@ -18,14 +18,23 @@ public var asserter: Asserter = QUnitAsserter()
public class QUnitAsserter(): Asserter { public class QUnitAsserter(): Asserter {
public override fun assertTrue(lazyMessage: () -> String?, actual: Boolean) { public override fun assertTrue(lazyMessage: () -> String?, actual: Boolean) {
QUnit.ok(actual, lazyMessage()) assertTrue(actual, lazyMessage())
} }
public override fun assertTrue(message: String?, actual: Boolean) { public override fun assertTrue(message: String?, actual: Boolean) {
QUnit.ok(actual, message) QUnit.ok(actual, message)
if (!actual) failWithMessage(message)
} }
public override fun fail(message: String?) { public override fun fail(message: String?): Nothing {
QUnit.ok(false, message) QUnit.ok(false, message)
failWithMessage(message)
}
private fun failWithMessage(message: String?): Nothing {
if (message == null)
throw AssertionError()
else
throw AssertionError(message)
} }
} }
+8 -3
View File
@@ -21,11 +21,15 @@ var JsTests = (function () {
var assert = function (isTrue, message) { var assert = function (isTrue, message) {
if (!isTrue) { if (!isTrue) {
reporter.reportError(message); fail(message);
throw failedTest;
} }
}; };
var fail = function (message) {
reporter.reportError(message);
throw failedTest;
};
var init = function () { var init = function () {
init = function() {}; init = function() {};
Kotlin.modules["JS_TESTS"].kotlin.test.init(); Kotlin.modules["JS_TESTS"].kotlin.test.init();
@@ -49,6 +53,7 @@ var JsTests = (function () {
}; };
return { return {
test: test, test: test,
assert: assert assert: assert,
fail: fail
} }
})(); })();
+4 -3
View File
@@ -7,10 +7,11 @@ fun init() {
} }
public class JsTestsAsserter() : Asserter { public class JsTestsAsserter() : Asserter {
public override fun fail(message: String?) { public override fun fail(message: String?): Nothing = failWithMessage(message)
assert(false, message)
}
} }
@native("JsTests.assert") @native("JsTests.assert")
public fun assert(value: Boolean, message: String?): Unit = noImpl public fun assert(value: Boolean, message: String?): Unit = noImpl
@native("JsTests.fail")
private fun failWithMessage(message: String?): Nothing = noImpl
+1
View File
@@ -200,6 +200,7 @@
Kotlin.UnsupportedOperationException = createClassNowWithMessage(Kotlin.RuntimeException); Kotlin.UnsupportedOperationException = createClassNowWithMessage(Kotlin.RuntimeException);
Kotlin.IndexOutOfBoundsException = createClassNowWithMessage(Kotlin.RuntimeException); Kotlin.IndexOutOfBoundsException = createClassNowWithMessage(Kotlin.RuntimeException);
Kotlin.IOException = createClassNowWithMessage(Kotlin.Exception); Kotlin.IOException = createClassNowWithMessage(Kotlin.Exception);
Kotlin.AssertionError = createClassNowWithMessage(Kotlin.Error);
Kotlin.throwNPE = function (message) { Kotlin.throwNPE = function (message) {
throw new Kotlin.NullPointerException(message); throw new Kotlin.NullPointerException(message);
@@ -35,7 +35,9 @@ object JUnitAsserter : Asserter {
Assert.assertNull(message ?: "actual value is not null", actual) Assert.assertNull(message ?: "actual value is not null", actual)
} }
override fun fail(message : String?) { override fun fail(message : String?): Nothing {
Assert.fail(message) 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 return e as T
} }
asserter.fail((message?.let { "$it. " } ?: "") + "Expected an exception of type $exceptionClass to be thrown, but was $e") asserter.fail((message?.let { "$it. " } ?: "") + "Expected an exception of type $exceptionClass to be thrown, but was $e")
throw e
} }
val msg = message?.let { "$it. " } ?: "" val msg = message?.let { "$it. " } ?: ""
asserter.fail(msg + "Expected an exception of type $exceptionClass to be thrown, but was completed successfully.") 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. */ /** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */
@@ -5,7 +5,7 @@ package kotlin.test
*/ */
class DefaultAsserter() : Asserter { class DefaultAsserter() : Asserter {
override fun fail(message: String?) { override fun fail(message: String?): Nothing {
if (message == null) if (message == null)
throw AssertionError() throw AssertionError()
else 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]. */ /** 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) asserter.fail(message)
} }
@@ -81,16 +81,13 @@ fun <@OnlyInputTypes T> expect(expected: T, message: String?, block: () -> T) {
fun fails(block: () -> Unit): Throwable? = assertFails(block) fun fails(block: () -> Unit): Throwable? = assertFails(block)
/** Asserts that given function [block] fails by throwing an exception. */ /** Asserts that given function [block] fails by throwing an exception. */
fun assertFails(block: () -> Unit): Throwable? { fun assertFails(block: () -> Unit): Throwable {
var thrown: Throwable? = null
try { try {
block() block()
} catch (e: Throwable) { } catch (e: Throwable) {
thrown = e return e
} }
if (thrown == null) asserter.fail("Expected an exception to be thrown")
asserter.fail("Expected an exception to be thrown")
return thrown
} }
/** /**
@@ -103,7 +100,7 @@ interface Asserter {
* *
* @param message the message to report. * @param message the message to report.
*/ */
fun fail(message: String?): Unit fun fail(message: String?): Nothing
/** /**
* Asserts that the specified value is `true`. * Asserts that the specified value is `true`.