-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
dachlatten-flow/src/main/java/de/sipgate/waffeleisen/flow/PrintFlow.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,14 @@ | ||
package de.sipgate.waffeleisen.flow | ||
|
||
import android.util.Log | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
|
||
inline fun <T> Flow<T?>.printFlow( | ||
tag: String = "from flow", | ||
crossinline printFunc: (String) -> Unit = { content -> Log.d(tag, content) }, | ||
crossinline stringFunc: (T?) -> String? = { it.toString() } | ||
) = map { | ||
printFunc(stringFunc(it) ?: "null") | ||
it | ||
} |
76 changes: 76 additions & 0 deletions
76
dachlatten-flow/src/test/java/de/sipgate/waffeleisen/flow/PrintFlowKtTest.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,76 @@ | ||
package de.sipgate.waffeleisen.flow | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
class PrintFlowKtTest { | ||
|
||
@Test | ||
fun printFlowPassesThroughValuesTransparently() = runTest { | ||
val flow = flowOf("first", "second", "third") | ||
|
||
val result = flow | ||
.printFlow(printFunc = { println(it) }) | ||
.collectAsList() | ||
|
||
assertEquals(listOf("first", "second", "third"), result) | ||
} | ||
|
||
@Test | ||
fun printFlowExecutesPrintFuncOnceForEachEmittedValue() = runTest { | ||
val flow = flowOf("first", "second", "third") | ||
|
||
val result = mutableListOf<String>() | ||
flow.printFlow(printFunc = { result.add(it) }).collect() | ||
|
||
assertEquals(listOf("first", "second", "third"), result.toList()) | ||
} | ||
|
||
@Test | ||
fun printFlowExecutesStringFuncForEachEmittedValue() = runTest { | ||
val flow = flowOf("first", "second", "third") | ||
|
||
val result = mutableListOf<String>() | ||
flow.printFlow( | ||
printFunc = { /* dummy */ }, | ||
stringFunc = { result.add(it!!); it } | ||
).collect() | ||
|
||
assertEquals(listOf("first", "second", "third"), result.toList()) | ||
} | ||
|
||
@Test | ||
fun printFlowCallsToStringByDefault() = runTest { | ||
data class SomeData( | ||
val aProperty: String, | ||
val bProperty: Boolean | ||
) | ||
|
||
val flow = flowOf( | ||
SomeData("asdf", true), | ||
SomeData("wah", false) | ||
) | ||
|
||
val stringBuffer = StringBuffer() | ||
flow.printFlow(printFunc = { stringBuffer.append(it).append("\n") }) | ||
.collect() | ||
|
||
val expected = """ | ||
SomeData(aProperty=asdf, bProperty=true) | ||
SomeData(aProperty=wah, bProperty=false) | ||
""".trimIndent() | ||
|
||
assertEquals(expected, stringBuffer.toString()) | ||
} | ||
|
||
private suspend fun <T> Flow<T>.collectAsList() = | ||
mutableListOf<T>().apply { | ||
this@collectAsList.map(this::add).collect() | ||
} | ||
} |