From ae2e3f1811129f32b1054f1fcf2dd7ef1e095fc2 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Mon, 25 Apr 2016 19:44:10 +0300 Subject: [PATCH] kotlin-test: add assertFails overload with message. --- js/js.libraries/src/stdlib/testCode.kt | 18 +++++++++++++- .../main/kotlin/kotlin/test/TestAssertions.kt | 24 +++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/js/js.libraries/src/stdlib/testCode.kt b/js/js.libraries/src/stdlib/testCode.kt index 28dbef0291b..8c7fa5ece60 100644 --- a/js/js.libraries/src/stdlib/testCode.kt +++ b/js/js.libraries/src/stdlib/testCode.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package kotlin.test /** @@ -14,7 +30,7 @@ public fun todo(block: () -> Any) { */ @kotlin.internal.InlineOnly inline fun assertFailsWith(message: String? = null, noinline block: () -> Unit): T { - val exception = assertFails(block) // TODO: message (in 1.1) + val exception = assertFails(message, block) assertTrue(exception is T, (message?.let { "$it. " } ?: "") + "An exception thrown is not of the expected type: $exception") return exception as T } diff --git a/libraries/kotlin.test/shared/src/main/kotlin/kotlin/test/TestAssertions.kt b/libraries/kotlin.test/shared/src/main/kotlin/kotlin/test/TestAssertions.kt index 079eccaf2e3..bb714d706d7 100644 --- a/libraries/kotlin.test/shared/src/main/kotlin/kotlin/test/TestAssertions.kt +++ b/libraries/kotlin.test/shared/src/main/kotlin/kotlin/test/TestAssertions.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + @file:kotlin.jvm.JvmMultifileClass @file:kotlin.jvm.JvmName("AssertionsKt") @@ -69,13 +85,17 @@ fun <@OnlyInputTypes T> expect(expected: T, message: String?, block: () -> T) { } /** Asserts that given function [block] fails by throwing an exception. */ -fun assertFails(block: () -> Unit): Throwable { +fun assertFails(block: () -> Unit): Throwable = assertFails(null, block) + +/** Asserts that given function [block] fails by throwing an exception. */ +fun assertFails(message: String?, block: () -> Unit): Throwable { try { block() } catch (e: Throwable) { return e } - asserter.fail("Expected an exception to be thrown") + val msg = message?.let { "$it. " } ?: "" + asserter.fail(msg + "Expected an exception to be thrown, but was completed successfully.") } /**