-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Entity getAsFlow (closes #108)
feat: Basic support for removing observers after adding
- Loading branch information
Showing
5 changed files
with
133 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
geary-core/src/jvmTest/kotlin/com/mineinabyss/geary/observers/EntityGetAsFlowTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.mineinabyss.geary.observers | ||
|
||
import com.mineinabyss.geary.helpers.entity | ||
import com.mineinabyss.geary.test.GearyTest | ||
import io.kotest.matchers.collections.shouldContainExactly | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
|
||
class EntityGetAsFlowTest : GearyTest() { | ||
@Test | ||
fun `getAsFlow should correctly listen to entity updates`() = runTest { | ||
val entity = entity() | ||
val collected = mutableListOf<Int>() | ||
|
||
backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { | ||
entity.getAsFlow<Int>().collect { | ||
if(it != null) collected.add(it) | ||
else collected.add(0) | ||
} | ||
} | ||
entity.set(1) | ||
entity.set("other component") | ||
entity.set(2) | ||
entity.remove<Int>() | ||
entity.set(3) | ||
|
||
collected shouldContainExactly listOf(0, 1, 2, 0, 3) | ||
} | ||
|
||
@Test | ||
fun `getAsFlow should unregister itself when cancelled`() = runTest { | ||
val entity = entity() | ||
val collected = mutableListOf<Int>() | ||
|
||
backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { | ||
entity.getAsFlow<Int>().collect { | ||
if(it != null) collected.add(it) | ||
else collected.add(0) | ||
} | ||
val collecting = launch(UnconfinedTestDispatcher(testScheduler)) { | ||
entity.getAsFlow<Int>().collect { | ||
if(it != null) collected.add(it) | ||
else collected.add(0) | ||
} | ||
} | ||
entity.set(1) | ||
collecting.cancel() | ||
entity.set(2) | ||
collected shouldContainExactly listOf(0, 1) | ||
} | ||
} | ||
} |