From 92e666f023c70203431e02ba9d0546096b7b585d Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Mon, 18 Dec 2017 15:14:11 +0700 Subject: [PATCH] stdlib: Enable assertFailsWith function The function was disabled due to an issue with catch serialization. Now the issue is fixed and the function can be re-enabled. --- backend.native/tests/testUtils.kt | 19 -------- backend.native/tests/testing/assertions.kt | 45 ++++++++--------- .../src/main/kotlin/kotlin/test/Assertions.kt | 48 +++++++++---------- 3 files changed, 44 insertions(+), 68 deletions(-) diff --git a/backend.native/tests/testUtils.kt b/backend.native/tests/testUtils.kt index 83269481ad3..1767c88f411 100644 --- a/backend.native/tests/testUtils.kt +++ b/backend.native/tests/testUtils.kt @@ -1,24 +1,5 @@ package kotlin.test -// TODO: Remove it when such method from library is available. -/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */ -inline fun assertFailsWith(message: String? = null, block: () -> Unit): T { - try { - block() - } catch (e: Throwable) { - if (e is T) { - @Suppress("UNCHECKED_CAST") - return e - } - - @Suppress("INVISIBLE_MEMBER") - fail(message + ". Expected an exception of type //TODO: add type!// to be thrown, but was $e") - } - - @Suppress("INVISIBLE_MEMBER") - fail(message + ". Expected an exception of type //TODO: add type!// to be thrown, but was completed successfully.") -} - @Suppress("UNUSED_PARAMETER") public fun assertTypeEquals(expected: Any?, actual: Any?) { //TODO: find analogue diff --git a/backend.native/tests/testing/assertions.kt b/backend.native/tests/testing/assertions.kt index 0983dfe243f..9b9ff16560c 100644 --- a/backend.native/tests/testing/assertions.kt +++ b/backend.native/tests/testing/assertions.kt @@ -19,30 +19,27 @@ class BasicAssertionsTest { assertFailsWith { throw AssertionError() } } -// TODO: Uncomment with the catch serialization bug is fixed. -// @Test fun testAssertFailsWithFails() { -// assertTrue(true) // at least one assertion required for qunit -// -// withDefaultAsserter run@ { -// try { -// assertFailsWith { throw IllegalArgumentException() } -// } -// catch (e: AssertionError) { -// return@run -// } -// throw AssertionError("Expected to fail") -// } -// -// withDefaultAsserter run@ { -// try { -// assertFailsWith { } -// } -// catch (e: AssertionError) { -// return@run -// } -// throw AssertionError("Expected to fail") -// } -// } + @Test + fun testAssertFailsWithFails() { + withDefaultAsserter run@ { + try { + assertFailsWith { throw IllegalArgumentException() } + } + catch (e: AssertionError) { + return@run + } + throw AssertionError("Expected to fail") + } + withDefaultAsserter run@ { + try { + assertFailsWith { } + } + catch (e: AssertionError) { + return@run + } + throw AssertionError("Expected to fail") + } + } @Test fun testAssertFailsWithClass() { diff --git a/runtime/src/main/kotlin/kotlin/test/Assertions.kt b/runtime/src/main/kotlin/kotlin/test/Assertions.kt index 7404079a7c9..2477eda0ef7 100644 --- a/runtime/src/main/kotlin/kotlin/test/Assertions.kt +++ b/runtime/src/main/kotlin/kotlin/test/Assertions.kt @@ -22,9 +22,9 @@ package kotlin.test import konan.FixmeMultiplatform -import konan.FixmeReflection import kotlin.internal.InlineOnly import kotlin.internal.OnlyInputTypes +import kotlin.reflect.KClass /** * Current adapter providing assertion implementations @@ -114,29 +114,12 @@ fun assertFails(message: String?, block: () -> Unit): Throwable { * Since inline method doesn't allow to trace where it was invoked, it is required to pass a [message] to distinguish this method call from others. */ @InlineOnly -@FixmeReflection // TODO: Implement as in Kotlin/JVM when KClass is available. -// TODO: An incorrect llvm module is generated if this method is called. Uncomment it when -@Suppress("UNUSED_PARAMETER") -inline fun assertFailsWith(message: String? = null, noinline block: () -> Unit) /*: T*/ { -// try { -// block() -// } catch (e: Throwable) { -// if (e is T) { -// return e -// } -// -// @Suppress("INVISIBLE_MEMBER") -// asserter.fail(messagePrefix(message) + "Unxepected exception type: $e") -// } -// -// @Suppress("INVISIBLE_MEMBER") -// val msg = messagePrefix(message) -// asserter.fail(msg + "Expected an exception to be thrown, but was completed successfully.") -} +inline fun assertFailsWith(message: String? = null, noinline block: () -> Unit) : T = + assertFailsWith(T::class, message, block) /** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */ -//@FixmeReflection -//fun assertFailsWith(exceptionClass: KClass, block: () -> Unit): T = assertFailsWith(exceptionClass, null, block) +fun assertFailsWith(exceptionClass: KClass, block: () -> Unit): T = + assertFailsWith(exceptionClass, null, block) // From AssertionsH.kt @@ -151,9 +134,24 @@ inline fun assertFailsWith(message: String? = null, noin } /** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */ -//@FixmeMultiplatform -//@FixmeReflection -//header fun assertFailsWith(exceptionClass: KClass, message: String?, block: () -> Unit): T +@FixmeMultiplatform +/* header */ fun assertFailsWith(exceptionClass: KClass, message: String?, block: () -> Unit): T { + try { + block() + } catch (e: Throwable) { + if (exceptionClass.isInstance(e)) { + @Suppress("UNCHECKED_CAST") + return e as T + } + + @Suppress("INVISIBLE_MEMBER") + asserter.fail(messagePrefix(message) + "Expected an exception of ${exceptionClass.qualifiedName} to be thrown, but was $e") + } + + @Suppress("INVISIBLE_MEMBER") + val msg = messagePrefix(message) + asserter.fail(msg + "Expected an exception of ${exceptionClass.qualifiedName} to be thrown, but was completed successfully.") +} // From Assertions.kt /**