From 1d5811b8652bf0e79a1d3a8f17e2ff7858996917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alaksiej=20=C5=A0=C4=8Darbaty?= Date: Sat, 3 Jun 2023 15:10:54 +0200 Subject: [PATCH] Adjust assertThrows contract Lambda invoked in assertThrows is marked as being called UNKNOWN number of times, since contracts do nothing with exception suppression Issue: #1866 --- .../kotlin/org/junit/jupiter/api/Assertions.kt | 6 +++--- .../org/junit/jupiter/api/KotlinAssertionsTests.kt | 14 +------------- 2 files changed, 4 insertions(+), 16 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 366a42beea52..8cfaf6319164 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 @@ -234,7 +234,7 @@ fun assertNotNull(actual: T?, messageSupplier: () -> String): T { */ inline fun assertThrows(executable: () -> Unit): T { contract { - callsInPlace(executable, EXACTLY_ONCE) + callsInPlace(executable) } val throwable: Throwable? = try { @@ -262,7 +262,7 @@ inline fun assertThrows(executable: () -> Unit): T { */ inline fun assertThrows(message: String, executable: () -> Unit): T { contract { - callsInPlace(executable, EXACTLY_ONCE) + callsInPlace(executable) } return assertThrows({ message }, executable) @@ -280,7 +280,7 @@ inline fun assertThrows(message: String, executable: () */ inline fun assertThrows(noinline message: () -> String, executable: () -> Unit): T { contract { - callsInPlace(executable, EXACTLY_ONCE) + callsInPlace(executable) callsInPlace(message, AT_MOST_ONCE) } diff --git a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/KotlinAssertionsTests.kt b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/KotlinAssertionsTests.kt index 582d9a4adbb1..eedcaf6b10bd 100644 --- a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/KotlinAssertionsTests.kt +++ b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/KotlinAssertionsTests.kt @@ -216,7 +216,7 @@ class KotlinAssertionsTests { val nullableString: String? = "string" assertNotNull(nullableString) - assertFalse(nullableString.isEmpty()) // smart cast to non nullable object + assertFalse(nullableString.isEmpty()) // smart cast to a non nullable object } @Test @@ -228,18 +228,6 @@ class KotlinAssertionsTests { // nullableString?.isEmpty() } - @Test - fun `assertThrows with value initialization in lambda`() { - val value: String - - assertThrows { - value = "string" - Assertions.fail("message") - } - - assertEquals("string", value) - } - @Test fun `assertDoesNotThrow with value initialization in lambda`() { val value: Int