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.
This commit is contained in:
@@ -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 <reified T : Throwable> 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
|
||||
|
||||
@@ -19,30 +19,27 @@ class BasicAssertionsTest {
|
||||
assertFailsWith<AssertionError> { 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<IllegalStateException> { throw IllegalArgumentException() }
|
||||
// }
|
||||
// catch (e: AssertionError) {
|
||||
// return@run
|
||||
// }
|
||||
// throw AssertionError("Expected to fail")
|
||||
// }
|
||||
//
|
||||
// withDefaultAsserter run@ {
|
||||
// try {
|
||||
// assertFailsWith<IllegalStateException> { }
|
||||
// }
|
||||
// catch (e: AssertionError) {
|
||||
// return@run
|
||||
// }
|
||||
// throw AssertionError("Expected to fail")
|
||||
// }
|
||||
// }
|
||||
@Test
|
||||
fun testAssertFailsWithFails() {
|
||||
withDefaultAsserter run@ {
|
||||
try {
|
||||
assertFailsWith<IllegalStateException> { throw IllegalArgumentException() }
|
||||
}
|
||||
catch (e: AssertionError) {
|
||||
return@run
|
||||
}
|
||||
throw AssertionError("Expected to fail")
|
||||
}
|
||||
withDefaultAsserter run@ {
|
||||
try {
|
||||
assertFailsWith<IllegalStateException> { }
|
||||
}
|
||||
catch (e: AssertionError) {
|
||||
return@run
|
||||
}
|
||||
throw AssertionError("Expected to fail")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertFailsWithClass() {
|
||||
|
||||
@@ -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 <reified T : Throwable> 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 <reified T : Throwable> 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 <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, block: () -> Unit): T = assertFailsWith(exceptionClass, null, block)
|
||||
fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, block: () -> Unit): T =
|
||||
assertFailsWith(exceptionClass, null, block)
|
||||
|
||||
|
||||
// From AssertionsH.kt
|
||||
@@ -151,9 +134,24 @@ inline fun <reified T : Throwable> assertFailsWith(message: String? = null, noin
|
||||
}
|
||||
|
||||
/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */
|
||||
//@FixmeMultiplatform
|
||||
//@FixmeReflection
|
||||
//header fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T
|
||||
@FixmeMultiplatform
|
||||
/* header */ fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, 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
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user