Skip to content

Commit

Permalink
Rename suspendCall to having
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Cooper committed Mar 21, 2024
1 parent b3441bc commit 761c293
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ import assertk.assertions.support.appendName
* assertThat(person).suspendCall("name()", { it.name() }).isEqualTo("Sue")
* ```
*/
suspend fun <T, P> Assert<T>.suspendCall(name: String, extract: suspend (T) -> P): Assert<P> =
suspend fun <T, P> Assert<T>.having(name: String, extract: suspend (T) -> P): Assert<P> =
transform(appendName(name, separator = ".")) { extract(it) }
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import assertk.assertions.support.expected
import assertk.assertions.support.expectedListDiff
import assertk.assertions.support.show
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*

suspend fun Assert<Flow<*>>.count(): Assert<Int> = suspendCall("count()", Flow<*>::count)
suspend fun Assert<Flow<*>>.count(): Assert<Int> = having("count()", Flow<*>::count)

/**
* Asserts the flow is empty. Fails as soon as the flow delivers an element.
Expand Down Expand Up @@ -190,4 +189,4 @@ suspend fun Assert<Flow<*>>.containsExactly(vararg elements: Any?) = given { act
}

private class AbortFlowException :
CancellationException("Flow was aborted, no more elements needed")
CancellationException("Flow was aborted, no more elements needed")
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import assertk.assertThat
import assertk.assertions.isEmpty
import assertk.assertions.isEqualTo
import assertk.assertions.isNotNull
import assertk.coroutines.assertions.suspendCall
import assertk.coroutines.assertions.having
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
Expand All @@ -15,21 +15,21 @@ class AnyTest {

@Test
fun suspendCall_passes() = runTest {
assertThat(subject).suspendCall("str") { it.str() }.isEqualTo("test")
assertThat(subject).having("str") { it.str() }.isEqualTo("test")
}

@Test
fun suspendCall_includes_name_in_failure_message() = runTest {
val error = assertFailsWith<AssertionError> {
assertThat(subject).suspendCall("str") { it.str() }.isEmpty()
assertThat(subject).having("str") { it.str() }.isEmpty()
}
assertEquals("expected [str] to be empty but was:<\"test\"> (test)", error.message)
}

@Test
fun nested_suspendCall_include_names_in_failure_message() = runTest {
val error = assertFailsWith<AssertionError> {
assertThat(subject).suspendCall("other") { it.other() }.suspendCall("str") { it?.str() }.isNotNull()
assertThat(subject).having("other") { it.other() }.having("str") { it?.str() }.isNotNull()
}
assertEquals("expected [other.str] to not be null (test)", error.message)
}
Expand Down

0 comments on commit 761c293

Please sign in to comment.