Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline lambdas passed to assertDoesNotThrows to support suspending functions #2684

Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,12 @@ inline fun <reified T : Throwable> assertThrows(noinline message: () -> String,
* @param R the result type of the [executable]
*/
@API(status = EXPERIMENTAL, since = "5.5")
fun <R> assertDoesNotThrow(executable: () -> R): R =
Assertions.assertDoesNotThrow(ThrowingSupplier(executable))
inline fun <reified R> assertDoesNotThrow(executable: () -> R): R = try {
val result = executable()
Assertions.assertDoesNotThrow(ThrowingSupplier { result })
} catch (throwable: Throwable) {
Assertions.assertDoesNotThrow(ThrowingSupplier { throw throwable })
}
williamboman marked this conversation as resolved.
Show resolved Hide resolved

/**
* Example usage:
Expand All @@ -172,7 +176,7 @@ fun <R> assertDoesNotThrow(executable: () -> R): R =
* @param R the result type of the [executable]
*/
@API(status = EXPERIMENTAL, since = "5.5")
fun <R> assertDoesNotThrow(message: String, executable: () -> R): R =
inline fun <reified R> assertDoesNotThrow(message: String, executable: () -> R): R =
assertDoesNotThrow({ message }, executable)

/**
Expand All @@ -186,8 +190,18 @@ fun <R> assertDoesNotThrow(message: String, executable: () -> R): R =
* @param R the result type of the [executable]
*/
@API(status = EXPERIMENTAL, since = "5.5")
fun <R> assertDoesNotThrow(message: () -> String, executable: () -> R): R =
Assertions.assertDoesNotThrow(ThrowingSupplier(executable), Supplier(message))
inline fun <reified R> assertDoesNotThrow(noinline message: () -> String, executable: () -> R): R = try {
val result = executable()
Assertions.assertDoesNotThrow(
ThrowingSupplier { result },
Supplier(message)
)
} catch (throwable: Throwable) {
Assertions.assertDoesNotThrow(
ThrowingSupplier { throw throwable },
Supplier(message)
)
}

/**
* Example usage:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,21 @@ class KotlinAssertionsTests {
}

@Test
fun `expected context exception testing`() = runBlocking<Unit> {
fun `assertThrows with coroutines`() = runBlocking<Unit> {
assertThrows<AssertionError>("Should fail async") {
suspend { fail("Should fail async") }()
}
}

@Test
fun `assertDoesNotThrow with coroutines`() = runBlocking {
val result = assertDoesNotThrow {
suspend { "Result" }()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to have another test with a suspending function that actually throws an exception (thus making the test fail), since we want to make sure that the assertion is actually waiting for the "throwing"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed the test factory below, you could even add one dynamic test in each dynamicContainer for this new suspending function use case :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! d48eeb4

}

assertEquals("Result", result)
}

@TestFactory
fun assertDoesNotThrow(): Stream<out DynamicNode> = Stream.of(
dynamicContainer("succeeds when no exception thrown", Stream.of(
Expand All @@ -89,6 +98,8 @@ class KotlinAssertionsTests {
val exception = assertThrows<AssertionError> {
assertDoesNotThrow {
fail("fail")
@Suppress("UNREACHABLE_CODE")
""
}
}
assertMessageEquals(exception,
Expand All @@ -98,6 +109,8 @@ class KotlinAssertionsTests {
val exception = assertThrows<AssertionError> {
assertDoesNotThrow("Does not throw") {
fail("fail")
@Suppress("UNREACHABLE_CODE")
""
}
}
assertMessageEquals(exception,
Expand All @@ -107,6 +120,8 @@ class KotlinAssertionsTests {
val exception = assertThrows<AssertionError> {
assertDoesNotThrow({ "Does not throw" }) {
fail("fail")
@Suppress("UNREACHABLE_CODE")
""
}
}
assertMessageEquals(exception,
Expand Down