Skip to content

Commit be678ca

Browse files
authored
feat: add difference between Flow takeWhile and transformWhile tests [KTLN-870] (#1133)
* feat: add difference between Flow `takeWhile` and `transformWhile` tests * update val name * syntax fix * add new line at EOF * rename file according to convention * fix typo * fix typo * demo transformation * Trigger Build * Trigger Build * simplify second transformWhile example to be more like takeWhile * fix test
1 parent 9345a00 commit be678ca

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.concatenateTwoFlows
2+
3+
import kotlinx.coroutines.flow.*
4+
import kotlinx.coroutines.runBlocking
5+
import org.junit.jupiter.api.Assertions
6+
import org.junit.jupiter.api.Test
7+
8+
class TakeWhileVsTransformWhileUnitTest {
9+
10+
@Test
11+
fun `takeWhile less than 3 returns 1 and 2`() {
12+
runBlocking {
13+
val numbersFlow = flowOf(1, 2, 3, 4, 5)
14+
15+
val taken = numbersFlow.takeWhile { it < 3 }.toList()
16+
17+
Assertions.assertEquals(listOf(1, 2), taken)
18+
}
19+
}
20+
21+
@Test
22+
fun `transformWhile less than 3 can return 1, 2 and 3`() {
23+
runBlocking {
24+
val numbersFlow = flowOf(1, 2, 3, 4, 5)
25+
26+
val transformed = numbersFlow.transformWhile {
27+
emit(it)
28+
it < 3
29+
}.toList()
30+
31+
Assertions.assertEquals(listOf(1, 2, 3), transformed)
32+
}
33+
}
34+
35+
@Test
36+
fun `transformWhile can transform elements`() {
37+
runBlocking {
38+
val numbersFlow = flowOf(1, 2, 3, 4, 5)
39+
40+
val transformed = numbersFlow.transformWhile {
41+
emit("" + it)
42+
emit("" + it * 2)
43+
it < 3
44+
}.toList()
45+
46+
Assertions.assertEquals(listOf("1", "2", "2", "4", "3", "6"), transformed)
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)