Skip to content

Commit

Permalink
Adjust assertThrows contract
Browse files Browse the repository at this point in the history
Lambda invoked in assertThrows is marked as being called UNKNOWN number
of times, since contracts do nothing with exception suppression

Issue: junit-team#1866
  • Loading branch information
awelless committed Jun 3, 2023
1 parent 3c4788c commit e3900cf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 deletions.
Expand Up @@ -234,7 +234,7 @@ fun <T> assertNotNull(actual: T?, messageSupplier: () -> String): T {
*/
inline fun <reified T : Throwable> assertThrows(executable: () -> Unit): T {
contract {
callsInPlace(executable, EXACTLY_ONCE)
callsInPlace(executable)
}

val throwable: Throwable? = try {
Expand Down Expand Up @@ -262,7 +262,7 @@ inline fun <reified T : Throwable> assertThrows(executable: () -> Unit): T {
*/
inline fun <reified T : Throwable> assertThrows(message: String, executable: () -> Unit): T {
contract {
callsInPlace(executable, EXACTLY_ONCE)
callsInPlace(executable)
}

return assertThrows({ message }, executable)
Expand All @@ -280,7 +280,7 @@ inline fun <reified T : Throwable> assertThrows(message: String, executable: ()
*/
inline fun <reified T : Throwable> assertThrows(noinline message: () -> String, executable: () -> Unit): T {
contract {
callsInPlace(executable, EXACTLY_ONCE)
callsInPlace(executable)
callsInPlace(message, AT_MOST_ONCE)
}

Expand Down
Expand Up @@ -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
Expand All @@ -228,18 +228,6 @@ class KotlinAssertionsTests {
// nullableString?.isEmpty()
}

@Test
fun `assertThrows with value initialization in lambda`() {
val value: String

assertThrows<AssertionError> {
value = "string"
Assertions.fail("message")
}

assertEquals("string", value)
}

@Test
fun `assertDoesNotThrow with value initialization in lambda`() {
val value: Int
Expand Down

0 comments on commit e3900cf

Please sign in to comment.