-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
FilterTest.kt
81 lines (70 loc) · 2.02 KB
/
FilterTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.flow
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlin.test.*
class FilterTest : TestBase() {
@Test
fun testFilter() = runTest {
val flow = flowOf(1, 2)
assertEquals(2, flow.filter { it % 2 == 0 }.sum())
assertEquals(3, flow.filter { true }.sum())
assertEquals(0, flow.filter { false }.sum())
}
@Test
fun testEmptyFlow() = runTest {
val sum = emptyFlow<Int>().filter { true }.sum()
assertEquals(0, sum)
}
@Test
fun testErrorCancelsUpstream() = runTest {
var cancelled = false
val latch = Channel<Unit>()
val flow = flow {
coroutineScope {
launch {
latch.send(Unit)
hang {cancelled = true}
}
emit(1)
}
}.filter {
latch.receive()
throw TestException()
}.catch { emit(42) }
assertEquals(42, flow.single())
assertTrue(cancelled)
}
@Test
fun testFilterNot() = runTest {
val flow = flowOf(1, 2)
assertEquals(0, flow.filterNot { true }.sum())
assertEquals(3, flow.filterNot { false }.sum())
}
@Test
fun testEmptyFlowFilterNot() = runTest {
val sum = emptyFlow<Int>().filterNot { true }.sum()
assertEquals(0, sum)
}
@Test
fun testErrorCancelsUpstreamwFilterNot() = runTest {
var cancelled = false
val latch = Channel<Unit>()
val flow = flow {
coroutineScope {
launch {
latch.send(Unit)
hang {cancelled = true}
}
emit(1)
}
}.filterNot {
latch.receive()
throw TestException()
}.catch { emit(42) }
assertEquals(42, flow.single())
assertTrue(cancelled)
}
}