|
| 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