From 8ad252775fc143d19de5eedc2cfb24d7b9ad27f4 Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Mon, 9 Dec 2024 14:20:06 +0100 Subject: [PATCH] Add contracts for executable parameters Follow-up on #3259 --- .../org/junit/jupiter/api/Assertions.kt | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt index 16ca2490a1c9..ad88c8c77e0f 100644 --- a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt +++ b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt @@ -107,7 +107,7 @@ fun assertAll(vararg executables: () -> Unit) = assertAll(executables.toList().s fun assertAll( heading: String?, vararg executables: () -> Unit -) = assertAll(heading, executables.toList().stream()) +) = assertAll(heading, executables.toList()) /** * Example usage: @@ -267,7 +267,11 @@ fun assertNotNull( * ``` * @see Assertions.assertThrows */ +@OptIn(ExperimentalContracts::class) inline fun assertThrows(executable: () -> Unit): T { + contract { + callsInPlace(executable, EXACTLY_ONCE) + } val throwable: Throwable? = try { executable() @@ -394,8 +398,8 @@ inline fun assertDoesNotThrow( executable: () -> R ): R { contract { - callsInPlace(executable, EXACTLY_ONCE) callsInPlace(message, AT_MOST_ONCE) + callsInPlace(executable, EXACTLY_ONCE) } return Assertions.assertDoesNotThrow( @@ -478,8 +482,8 @@ fun assertTimeout( executable: () -> R ): R { contract { - callsInPlace(executable, EXACTLY_ONCE) callsInPlace(message, AT_MOST_ONCE) + callsInPlace(executable, EXACTLY_ONCE) } return Assertions.assertTimeout(timeout, executable, message) @@ -495,11 +499,17 @@ fun assertTimeout( * @see Assertions.assertTimeoutPreemptively * @param R the result of the [executable]. */ +@OptIn(ExperimentalContracts::class) @API(status = STABLE, since = "5.11") fun assertTimeoutPreemptively( timeout: Duration, executable: () -> R -): R = Assertions.assertTimeoutPreemptively(timeout, executable) +): R { + contract { + callsInPlace(executable, EXACTLY_ONCE) + } + return Assertions.assertTimeoutPreemptively(timeout, executable) +} /** * Example usage: @@ -511,12 +521,18 @@ fun assertTimeoutPreemptively( * @see Assertions.assertTimeoutPreemptively * @param R the result of the [executable]. */ +@OptIn(ExperimentalContracts::class) @API(status = STABLE, since = "5.11") fun assertTimeoutPreemptively( timeout: Duration, message: String, executable: () -> R -): R = Assertions.assertTimeoutPreemptively(timeout, executable, message) +): R { + contract { + callsInPlace(executable, EXACTLY_ONCE) + } + return Assertions.assertTimeoutPreemptively(timeout, executable, message) +} /** * Example usage: @@ -537,6 +553,7 @@ fun assertTimeoutPreemptively( ): R { contract { callsInPlace(message, AT_MOST_ONCE) + callsInPlace(executable, EXACTLY_ONCE) } return Assertions.assertTimeoutPreemptively(timeout, executable, message)