Skip to content

Commit 8ad2527

Browse files
committed
Add contracts for executable parameters
Follow-up on #3259
1 parent e3715cf commit 8ad2527

File tree

1 file changed

+22
-5
lines changed
  • junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api

1 file changed

+22
-5
lines changed

junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt

+22-5
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fun assertAll(vararg executables: () -> Unit) = assertAll(executables.toList().s
107107
fun assertAll(
108108
heading: String?,
109109
vararg executables: () -> Unit
110-
) = assertAll(heading, executables.toList().stream())
110+
) = assertAll(heading, executables.toList())
111111

112112
/**
113113
* Example usage:
@@ -267,7 +267,11 @@ fun assertNotNull(
267267
* ```
268268
* @see Assertions.assertThrows
269269
*/
270+
@OptIn(ExperimentalContracts::class)
270271
inline fun <reified T : Throwable> assertThrows(executable: () -> Unit): T {
272+
contract {
273+
callsInPlace(executable, EXACTLY_ONCE)
274+
}
271275
val throwable: Throwable? =
272276
try {
273277
executable()
@@ -394,8 +398,8 @@ inline fun <R> assertDoesNotThrow(
394398
executable: () -> R
395399
): R {
396400
contract {
397-
callsInPlace(executable, EXACTLY_ONCE)
398401
callsInPlace(message, AT_MOST_ONCE)
402+
callsInPlace(executable, EXACTLY_ONCE)
399403
}
400404

401405
return Assertions.assertDoesNotThrow(
@@ -478,8 +482,8 @@ fun <R> assertTimeout(
478482
executable: () -> R
479483
): R {
480484
contract {
481-
callsInPlace(executable, EXACTLY_ONCE)
482485
callsInPlace(message, AT_MOST_ONCE)
486+
callsInPlace(executable, EXACTLY_ONCE)
483487
}
484488

485489
return Assertions.assertTimeout(timeout, executable, message)
@@ -495,11 +499,17 @@ fun <R> assertTimeout(
495499
* @see Assertions.assertTimeoutPreemptively
496500
* @param R the result of the [executable].
497501
*/
502+
@OptIn(ExperimentalContracts::class)
498503
@API(status = STABLE, since = "5.11")
499504
fun <R> assertTimeoutPreemptively(
500505
timeout: Duration,
501506
executable: () -> R
502-
): R = Assertions.assertTimeoutPreemptively(timeout, executable)
507+
): R {
508+
contract {
509+
callsInPlace(executable, EXACTLY_ONCE)
510+
}
511+
return Assertions.assertTimeoutPreemptively(timeout, executable)
512+
}
503513

504514
/**
505515
* Example usage:
@@ -511,12 +521,18 @@ fun <R> assertTimeoutPreemptively(
511521
* @see Assertions.assertTimeoutPreemptively
512522
* @param R the result of the [executable].
513523
*/
524+
@OptIn(ExperimentalContracts::class)
514525
@API(status = STABLE, since = "5.11")
515526
fun <R> assertTimeoutPreemptively(
516527
timeout: Duration,
517528
message: String,
518529
executable: () -> R
519-
): R = Assertions.assertTimeoutPreemptively(timeout, executable, message)
530+
): R {
531+
contract {
532+
callsInPlace(executable, EXACTLY_ONCE)
533+
}
534+
return Assertions.assertTimeoutPreemptively(timeout, executable, message)
535+
}
520536

521537
/**
522538
* Example usage:
@@ -537,6 +553,7 @@ fun <R> assertTimeoutPreemptively(
537553
): R {
538554
contract {
539555
callsInPlace(message, AT_MOST_ONCE)
556+
callsInPlace(executable, EXACTLY_ONCE)
540557
}
541558

542559
return Assertions.assertTimeoutPreemptively(timeout, executable, message)

0 commit comments

Comments
 (0)