1
1
package io.reactivex.rxkotlin
2
2
3
+ import io.reactivex.BackpressureStrategy
3
4
import io.reactivex.Flowable
4
- import org.junit.Assert
5
+ import io.reactivex.Flowable.create
6
+ import io.reactivex.FlowableEmitter
7
+ import org.junit.Assert.assertEquals
8
+ import org.junit.Assert.assertNotNull
5
9
import org.junit.Ignore
6
10
import org.junit.Test
7
11
import java.util.concurrent.atomic.AtomicInteger
8
12
9
13
class FlowableTest {
10
14
11
- private fun <T : Any > bufferedFlowable (source : (io.reactivex. FlowableEmitter <T >) -> Unit ) =
12
- io.reactivex. Flowable . create(source, io.reactivex. BackpressureStrategy .BUFFER )
15
+ private fun <T : Any > bufferedFlowable (source : (FlowableEmitter <T >) -> Unit ) =
16
+ create(source, BackpressureStrategy .BUFFER )
13
17
14
18
@org.junit.Test fun testCreation () {
15
- val o0: io.reactivex. Flowable <Int > = io.reactivex. Flowable .empty()
19
+ val o0: Flowable <Int > = Flowable .empty()
16
20
val list = bufferedFlowable<Int > { s ->
17
21
s.onNext(1 )
18
22
s.onNext(777 )
19
23
s.onComplete()
20
24
}.toList().blockingGet()
21
- org.junit. Assert . assertEquals(listOf (1 , 777 ), list)
22
- val o1: io.reactivex. Flowable <Int > = listOf (1 , 2 , 3 ).toFlowable()
23
- val o2: io.reactivex. Flowable <List <Int >> = io.reactivex. Flowable .just(listOf (1 , 2 , 3 ))
24
-
25
- val o3: io.reactivex. Flowable <Int > = io.reactivex. Flowable .defer { bufferedFlowable<Int > { s -> s.onNext(1 ) } }
26
- val o4: io.reactivex. Flowable <Int > = Array (3 ) { 0 }.toFlowable()
27
- val o5: io.reactivex. Flowable <Int > = IntArray (3 ).toFlowable()
28
-
29
- org.junit. Assert . assertNotNull(o0)
30
- org.junit. Assert . assertNotNull(o1)
31
- org.junit. Assert . assertNotNull(o2)
32
- org.junit. Assert . assertNotNull(o3)
33
- org.junit. Assert . assertNotNull(o4)
34
- org.junit. Assert . assertNotNull(o5)
25
+ assertEquals(listOf (1 , 777 ), list)
26
+ val o1: Flowable <Int > = listOf (1 , 2 , 3 ).toFlowable()
27
+ val o2: Flowable <List <Int >> = Flowable .just(listOf (1 , 2 , 3 ))
28
+
29
+ val o3: Flowable <Int > = Flowable .defer { bufferedFlowable<Int > { s -> s.onNext(1 ) } }
30
+ val o4: Flowable <Int > = Array (3 ) { 0 }.toFlowable()
31
+ val o5: Flowable <Int > = IntArray (3 ).toFlowable()
32
+
33
+ assertNotNull(o0)
34
+ assertNotNull(o1)
35
+ assertNotNull(o2)
36
+ assertNotNull(o3)
37
+ assertNotNull(o4)
38
+ assertNotNull(o5)
35
39
}
36
40
37
41
@org.junit.Test fun testExampleFromReadme () {
@@ -48,34 +52,34 @@ class FlowableTest {
48
52
map { it.toString() }.
49
53
blockingGet()
50
54
51
- Assert . assertEquals(" Hello" , result)
55
+ assertEquals(" Hello" , result)
52
56
}
53
57
54
58
@Test fun iteratorFlowable () {
55
- Assert . assertEquals(listOf (1 , 2 , 3 ), listOf (1 , 2 , 3 ).iterator().toFlowable().toList().blockingGet())
59
+ assertEquals(listOf (1 , 2 , 3 ), listOf (1 , 2 , 3 ).iterator().toFlowable().toList().blockingGet())
56
60
}
57
61
58
62
@Test fun intProgressionStep1Empty () {
59
- Assert . assertEquals(listOf (1 ), (1 .. 1 ).toFlowable().toList().blockingGet())
63
+ assertEquals(listOf (1 ), (1 .. 1 ).toFlowable().toList().blockingGet())
60
64
}
61
65
62
66
@Test fun intProgressionStep1 () {
63
- Assert . assertEquals((1 .. 10 ).toList(), (1 .. 10 ).toFlowable().toList().blockingGet())
67
+ assertEquals((1 .. 10 ).toList(), (1 .. 10 ).toFlowable().toList().blockingGet())
64
68
}
65
69
66
70
@Test fun intProgressionDownTo () {
67
- Assert . assertEquals((1 downTo 10 ).toList(), (1 downTo 10 ).toFlowable().toList().blockingGet())
71
+ assertEquals((1 downTo 10 ).toList(), (1 downTo 10 ).toFlowable().toList().blockingGet())
68
72
}
69
73
70
74
@Ignore(" Too slow" )
71
75
@Test fun intProgressionOverflow () {
72
- Assert . assertEquals((0 .. 10 ).toList().reversed(), (- 10 .. Integer .MAX_VALUE ).toFlowable().skip(Integer .MAX_VALUE .toLong()).map { Integer .MAX_VALUE - it }.toList().blockingGet())
76
+ assertEquals((0 .. 10 ).toList().reversed(), (- 10 .. Integer .MAX_VALUE ).toFlowable().skip(Integer .MAX_VALUE .toLong()).map { Integer .MAX_VALUE - it }.toList().blockingGet())
73
77
}
74
78
75
79
76
80
@Test fun testFold () {
77
81
val result = listOf (1 , 2 , 3 ).toFlowable().reduce(0 ) { acc, e -> acc + e }.blockingGet()
78
- Assert . assertEquals(6 , result)
82
+ assertEquals(6 , result)
79
83
}
80
84
81
85
@Test fun `kotlin sequence should produce expected items and flowable be able to handle em` () {
@@ -93,24 +97,24 @@ class FlowableTest {
93
97
toList().
94
98
subscribe()
95
99
96
- Assert . assertEquals(100 , generated.get())
100
+ assertEquals(100 , generated.get())
97
101
}
98
102
99
103
@Test fun testFlatMapSequence () {
100
- Assert . assertEquals(
104
+ assertEquals(
101
105
listOf (1 , 2 , 3 , 2 , 3 , 4 , 3 , 4 , 5 ),
102
106
listOf (1 , 2 , 3 ).toFlowable().flatMapSequence { listOf (it, it + 1 , it + 2 ).asSequence() }.toList().blockingGet()
103
107
)
104
108
}
105
109
106
110
@Test fun testCombineLatest () {
107
111
val list = listOf (1 , 2 , 3 , 2 , 3 , 4 , 3 , 4 , 5 )
108
- Assert . assertEquals(list, list.map { Flowable .just(it) }.combineLatest { it }.blockingFirst())
112
+ assertEquals(list, list.map { Flowable .just(it) }.combineLatest { it }.blockingFirst())
109
113
}
110
114
111
115
@Test fun testZip () {
112
116
val list = listOf (1 , 2 , 3 , 2 , 3 , 4 , 3 , 4 , 5 )
113
- Assert . assertEquals(list, list.map { Flowable .just(it) }.zip { it }.blockingFirst())
117
+ assertEquals(list, list.map { Flowable .just(it) }.zip { it }.blockingFirst())
114
118
}
115
119
116
120
@Test fun testCast () {
@@ -129,4 +133,20 @@ class FlowableTest {
129
133
flowable.test()
130
134
.assertError(ClassCastException ::class .java)
131
135
}
136
+
137
+ @Test fun combineLatestPair () {
138
+ Flowable .just(3 )
139
+ .combineLatest(Flowable .just(10 ))
140
+ .map { (x, y) -> x * y }
141
+ .test()
142
+ .assertValues(30 )
143
+ }
144
+
145
+ @Test fun combineLatestTriple () {
146
+ Flowable .just(3 )
147
+ .combineLatest(Flowable .just(10 ), Flowable .just(20 ))
148
+ .map { (x, y, z) -> x * y * z }
149
+ .test()
150
+ .assertValues(600 )
151
+ }
132
152
}
0 commit comments