Make more lambda-taking functions in kotlin-test inline-only

So that the lambda can contain non-local control flow, such as suspend
calls. Inline-only helps preserving line numbers in the failed assertion
stack traces.

KT-44717
This commit is contained in:
Ilya Gorbunov
2021-03-06 06:29:48 +03:00
committed by TeamCityServer
parent 66a29c70bf
commit ca99fc4fed
2 changed files with 57 additions and 11 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -29,7 +29,9 @@ val asserter: Asserter
internal var _asserter: Asserter? = null
/** Asserts that the given [block] returns `true`. */
fun assertTrue(message: String? = null, block: () -> Boolean) {
@JvmName("assertTrueInline")
@InlineOnly
inline fun assertTrue(message: String? = null, block: () -> Boolean) {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
assertTrue(block(), message)
}
@@ -41,7 +43,9 @@ fun assertTrue(actual: Boolean, message: String? = null) {
}
/** Asserts that the given [block] returns `false`. */
fun assertFalse(message: String? = null, block: () -> Boolean) {
@JvmName("assertFalseInline")
@InlineOnly
inline fun assertFalse(message: String? = null, block: () -> Boolean) {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
assertFalse(block(), message)
}
@@ -80,12 +84,11 @@ fun <T : Any> assertNotNull(actual: T?, message: String? = null): T {
}
/** Asserts that the [actual] value is not `null`, with an optional [message] and a function [block] to process the not-null value. */
fun <T : Any, R> assertNotNull(actual: T?, message: String? = null, block: (T) -> R) {
@JvmName("assertNotNullInline")
@InlineOnly
inline fun <T : Any, R> assertNotNull(actual: T?, message: String? = null, block: (T) -> R) {
contract { returns() implies (actual != null) }
asserter.assertNotNull(message, actual)
if (actual != null) {
block(actual)
}
block(assertNotNull(actual, message))
}
/** Asserts that the [actual] value is `null`, with an optional [message]. */
@@ -110,13 +113,17 @@ fun fail(message: String? = null, cause: Throwable? = null): Nothing {
}
/** Asserts that given function [block] returns the given [expected] value. */
fun <@OnlyInputTypes T> expect(expected: T, block: () -> T) {
@JvmName("expectInline")
@InlineOnly
inline fun <@OnlyInputTypes T> expect(expected: T, block: () -> T) {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
assertEquals(expected, block())
}
/** Asserts that given function [block] returns the given [expected] value and use the given [message] if it fails. */
fun <@OnlyInputTypes T> expect(expected: T, message: String?, block: () -> T) {
@JvmName("expectInline")
@InlineOnly
inline fun <@OnlyInputTypes T> expect(expected: T, message: String?, block: () -> T) {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
assertEquals(expected, block(), message)
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -8,6 +8,8 @@
package kotlin.test
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.internal.*
import kotlin.reflect.*
@@ -51,6 +53,43 @@ fun <T : Throwable> assertFailsWithNoInline(exceptionClass: KClass<T>, block: ()
fun <T : Throwable> assertFailsWithNoInline(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T =
assertFailsWith(exceptionClass, message, block)
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@JvmName("assertTrue")
fun assertTrueNoInline(message: String? = null, block: () -> Boolean) {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
assertTrue(block(), message)
}
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@JvmName("assertFalse")
fun assertFalseNoInline(message: String? = null, block: () -> Boolean) {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
assertFalse(block(), message)
}
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@JvmName("assertNotNull")
fun <T : Any, R> assertNotNullNoInline(actual: T?, message: String? = null, block: (T) -> R) {
contract { returns() implies (actual != null) }
asserter.assertNotNull(message, actual)
if (actual != null) {
block(actual)
}
}
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@JvmName("expect")
fun <T> expectNoInline(expected: T, block: () -> T) {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
assertEquals(expected, block())
}
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@JvmName("expect")
fun <T> expectNoInline(expected: T, message: String?, block: () -> T) {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
assertEquals(expected, block(), message)
}
/**
* Takes the given [block] of test code and _doesn't_ execute it.