kotlin.test.TestAssertionsJVM.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
@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()