All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlin.test.TestAssertionsJVM.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("AssertionsKt")
package kotlin.test

import kotlin.reflect.*

/** Asserts that a [block] fails with a specific exception being thrown. */
private fun  assertFailsWithImpl(exceptionClass: Class, message: String?, block: () -> Unit): T {
    try {
        block()
    } catch (e: Throwable) {
        if (exceptionClass.isInstance(e)) {
            @Suppress("UNCHECKED_CAST")
            return e as T
        }
        asserter.fail((message?.let { "$it. " } ?: "") + "Expected an exception of type $exceptionClass to be thrown, but was $e")
    }
    val msg = message?.let { "$it. " } ?: ""
    asserter.fail(msg + "Expected an exception of type $exceptionClass to be thrown, but was completed successfully.")
}

/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */
fun  assertFailsWith(exceptionClass: KClass, block: () -> Unit): T = assertFailsWith(exceptionClass, null, block)

/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */
fun  assertFailsWith(exceptionClass: KClass, message: String?, block: () -> Unit): T = assertFailsWithImpl(exceptionClass.java, message, block)

/** Asserts that a [block] fails with a specific exception of type [T] being thrown.
 *  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.
 */
@kotlin.internal.InlineOnly
inline fun  assertFailsWith(message: String? = null, noinline block: () -> Unit): T = assertFailsWith(T::class, message, block)


/**
 * Comments out a [block] of test code until it is implemented while keeping a link to the code
 * to implement in your unit test output
 */
@kotlin.internal.InlineOnly
inline fun todo(@Suppress("UNUSED_PARAMETER") block: () -> Unit) {
    System.out.println("TODO at " + currentStackTrace()[0])
}

/**
 * Returns an array of stack trace elements, each representing one stack frame.
 * The first element of the array (assuming the array is not empty) represents the top of the
 * stack, which is the place where [currentStackTrace] function was called from.
 */
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
@kotlin.internal.InlineOnly
inline fun currentStackTrace() = (java.lang.Exception() as java.lang.Throwable).stackTrace

/**
 * The active implementation of [Asserter]. An implementation of [Asserter] can be provided
 * using the [Java service loader](http://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html) mechanism.
 */
val asserter: Asserter
    get() = lookup()





© 2015 - 2024 Weber Informatics LLC | Privacy Policy