diff --git a/CHANGELOG.md b/CHANGELOG.md index a47343b9..2e4f447b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - signature of `isIn` and `isNotIn` has been changed to ensure at least two items are supplied. - added assertions `isIn(Iterable)` and `isNotIn(Iterable)` +### Added +- `havingValue` for `StateFlow` assertions + ## [0.28.1] 2024-04-17 ### Added diff --git a/assertk-coroutines/src/commonMain/kotlin/assertk/coroutines/assertions/flow.kt b/assertk-coroutines/src/commonMain/kotlin/assertk/coroutines/assertions/flow.kt index 9115ee2f..01bd6ff0 100644 --- a/assertk-coroutines/src/commonMain/kotlin/assertk/coroutines/assertions/flow.kt +++ b/assertk-coroutines/src/commonMain/kotlin/assertk/coroutines/assertions/flow.kt @@ -11,6 +11,16 @@ import kotlinx.coroutines.flow.* suspend fun Assert>.count(): Assert = having("count()", Flow<*>::count) +/** + * Returns an assert that asserts on the value of the given [StateFlow] + * + * ``` + * val person: StateFlow = _person + * assertThat(person).havingValue().having(Person::name).isEqualTo("Sue") + * ``` + */ +fun Assert>.havingValue() = having(StateFlow::value) + /** * Asserts the flow is empty. Fails as soon as the flow delivers an element. * @see isNotEmpty diff --git a/assertk-coroutines/src/commonTest/kotlin/test/assertk/coroutines/assertions/FlowTest.kt b/assertk-coroutines/src/commonTest/kotlin/test/assertk/coroutines/assertions/FlowTest.kt index 063ae8d9..f4e471af 100644 --- a/assertk-coroutines/src/commonTest/kotlin/test/assertk/coroutines/assertions/FlowTest.kt +++ b/assertk-coroutines/src/commonTest/kotlin/test/assertk/coroutines/assertions/FlowTest.kt @@ -1,9 +1,11 @@ package test.assertk.coroutines.assertions import assertk.assertThat +import assertk.assertions.isEqualTo import assertk.assertions.support.show import assertk.coroutines.assertions.* import kotlinx.coroutines.channels.awaitClose +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.flowOf import kotlin.test.Test @@ -307,6 +309,18 @@ class FlowTest { ) } //endregion + + //region StateFlow value + @Test + fun value_transform() { + val name = MutableStateFlow("Sue") + val display = name::class.simpleName!! + val error = assertFailsWith { + assertThat(name, "name") { display }.havingValue().isEqualTo("John") + } + assertEquals("""expected [name.value]:<"[John]"> but was:<"[Sue]"> ($display)""", error.message) + } + //endregion } private fun nonCompletingFlowOf(vararg elements: T) = callbackFlow {