Skip to content

Commit 6669b93

Browse files
committed
Relax Kotlin contracts for Executable parameters
Since `Executable` is allowed/expected to throw exceptions, the semantics of being called `EXACTLY_ONCE` are blurry because it might not be executed entirely when throwing an exception. Since the Kotlin 2.1 compiler emits a warning for these parameters, we're relaxing their contracts to be called `AT_MOST_ONCE` instead.
1 parent aa7d3eb commit 6669b93

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ inline fun <reified T : Throwable> assertThrows(
351351
@API(status = STABLE, since = "5.11")
352352
inline fun <R> assertDoesNotThrow(executable: () -> R): R {
353353
contract {
354-
callsInPlace(executable, EXACTLY_ONCE)
354+
callsInPlace(executable, AT_MOST_ONCE)
355355
}
356356

357357
return Assertions.assertDoesNotThrow(evaluateAndWrap(executable))
@@ -374,7 +374,7 @@ inline fun <R> assertDoesNotThrow(
374374
executable: () -> R
375375
): R {
376376
contract {
377-
callsInPlace(executable, EXACTLY_ONCE)
377+
callsInPlace(executable, AT_MOST_ONCE)
378378
}
379379

380380
return assertDoesNotThrow({ message }, executable)
@@ -397,7 +397,7 @@ inline fun <R> assertDoesNotThrow(
397397
executable: () -> R
398398
): R {
399399
contract {
400-
callsInPlace(executable, EXACTLY_ONCE)
400+
callsInPlace(executable, AT_MOST_ONCE)
401401
callsInPlace(message, AT_MOST_ONCE)
402402
}
403403

@@ -411,7 +411,7 @@ inline fun <R> assertDoesNotThrow(
411411
@PublishedApi
412412
internal inline fun <R> evaluateAndWrap(executable: () -> R): ThrowingSupplier<R> {
413413
contract {
414-
callsInPlace(executable, EXACTLY_ONCE)
414+
callsInPlace(executable, AT_MOST_ONCE)
415415
}
416416

417417
return try {
@@ -439,7 +439,7 @@ fun <R> assertTimeout(
439439
executable: () -> R
440440
): R {
441441
contract {
442-
callsInPlace(executable, EXACTLY_ONCE)
442+
callsInPlace(executable, AT_MOST_ONCE)
443443
}
444444

445445
return Assertions.assertTimeout(timeout, executable)
@@ -463,7 +463,7 @@ fun <R> assertTimeout(
463463
executable: () -> R
464464
): R {
465465
contract {
466-
callsInPlace(executable, EXACTLY_ONCE)
466+
callsInPlace(executable, AT_MOST_ONCE)
467467
}
468468

469469
return Assertions.assertTimeout(timeout, executable, message)
@@ -487,7 +487,7 @@ fun <R> assertTimeout(
487487
executable: () -> R
488488
): R {
489489
contract {
490-
callsInPlace(executable, EXACTLY_ONCE)
490+
callsInPlace(executable, AT_MOST_ONCE)
491491
callsInPlace(message, AT_MOST_ONCE)
492492
}
493493

jupiter-tests/src/test/kotlin/org/junit/jupiter/api/KotlinAssertTimeoutAssertionsTests.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -156,26 +156,26 @@ internal class KotlinAssertTimeoutAssertionsTests {
156156
}
157157

158158
@Test
159-
fun `assertTimeout with value initialization in lambda`() {
160-
val value: Int
159+
fun `assertTimeout with value assignment in lambda`() {
160+
var value = 0
161161

162162
assertTimeout(ofMillis(500)) { value = 10 }
163163

164164
assertEquals(10, value)
165165
}
166166

167167
@Test
168-
fun `assertTimeout with message and value initialization in lambda`() {
169-
val value: Int
168+
fun `assertTimeout with message and value assignment in lambda`() {
169+
var value = 0
170170

171171
assertTimeout(ofMillis(500), "message") { value = 10 }
172172

173173
assertEquals(10, value)
174174
}
175175

176176
@Test
177-
fun `assertTimeout with message supplier and value initialization in lambda`() {
178-
val value: Int
177+
fun `assertTimeout with message supplier and value assignment in lambda`() {
178+
var value = 0
179179

180180
@Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE")
181181
val valueInMessageSupplier: Int

0 commit comments

Comments
 (0)