diff --git a/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Assertions.kt b/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Assertions.kt index dac22928c8c..352a8886a0d 100644 --- a/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Assertions.kt +++ b/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Assertions.kt @@ -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 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 assertNotNull(actual: T?, message: String? = null, block: (T) -> R) { +@JvmName("assertNotNullInline") +@InlineOnly +inline fun 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) } diff --git a/libraries/kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt b/libraries/kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt index 8efe94454ab..741ddbf7744 100644 --- a/libraries/kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt +++ b/libraries/kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt @@ -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 assertFailsWithNoInline(exceptionClass: KClass, block: () fun assertFailsWithNoInline(exceptionClass: KClass, 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 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 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 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.