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

Add Assert<StateFlow>.value() #560

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>)` and `isNotIn(Iterable<T>)`

### Added
- `havingValue` for `StateFlow` assertions

## [0.28.1] 2024-04-17

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import kotlinx.coroutines.flow.*

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

/**
* Returns an assert that asserts on the value of the given [StateFlow]
*
* ```
* val person: StateFlow<Person> = _person
* assertThat(person).havingValue().having(Person::name).isEqualTo("Sue")
* ```
*/
fun <T> Assert<StateFlow<T>>.havingValue() = having(StateFlow<T>::value)

/**
* Asserts the flow is empty. Fails as soon as the flow delivers an element.
* @see isNotEmpty
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<AssertionError> {
assertThat(name, "name") { display }.havingValue().isEqualTo("John")
}
assertEquals("""expected [name.value]:<"[John]"> but was:<"[Sue]"> ($display)""", error.message)
}
//endregion
}

private fun <T> nonCompletingFlowOf(vararg elements: T) = callbackFlow {
Expand Down