1
+ package rx.lang.kotlin
2
+
3
+ import org.mockito.Mock
4
+ import rx.Observable
5
+ import org.junit.Before
6
+ import org.mockito.MockitoAnnotations
7
+ import org.junit.Test
8
+ import rx.subscriptions.Subscriptions
9
+ import org.mockito.Mockito.*
10
+ import org.mockito.Matchers.*
11
+ import rx.Observer
12
+ import org.junit.Assert.*
13
+ import rx.Notification
14
+
15
+ public class BasicKotlinTests {
16
+
17
+ [Mock ] var a: ScriptAssertion ? = null
18
+ [Mock ] var w: Observable <Int >? = null
19
+
20
+ [Before ]
21
+ public fun before () {
22
+ MockitoAnnotations .initMocks(this )
23
+ }
24
+
25
+ [Test ]
26
+ public fun testCreate () {
27
+ Observable .create<String >{
28
+ it!! .onNext(" Hello" )
29
+ it.onCompleted()
30
+ Subscriptions .empty()
31
+ }!! .subscribe { result ->
32
+ a!! .received(result)
33
+ }
34
+
35
+ verify(a, times(1 ))!! .received(" Hello" )
36
+ }
37
+ [Test ]
38
+ public fun testCreateEx () {
39
+
40
+ {(observer: Observer <in String >) ->
41
+ observer.onNext(" Hello" )
42
+ observer.onCompleted()
43
+ Subscriptions .empty()!!
44
+ }.asObservable().subscribe { result ->
45
+ a!! .received(result)
46
+ }
47
+
48
+ verify(a, times(1 ))!! .received(" Hello" )
49
+ }
50
+
51
+ [Test ]
52
+ public fun testFilter () {
53
+ Observable .from(1 , 2 , 3 )!! .filter { it!! >= 2 }!! .subscribe { result ->
54
+ a!! .received(result)
55
+ }
56
+ verify(a, times(0 ))!! .received(1 );
57
+ verify(a, times(1 ))!! .received(2 );
58
+ verify(a, times(1 ))!! .received(3 );
59
+ }
60
+
61
+ [Test ]
62
+ public fun testFilterEx () {
63
+ listOf (1 , 2 , 3 ).asObservable().filter { it!! >= 2 }!! .subscribe { result ->
64
+ a!! .received(result)
65
+ }
66
+ verify(a, times(0 ))!! .received(1 );
67
+ verify(a, times(1 ))!! .received(2 );
68
+ verify(a, times(1 ))!! .received(3 );
69
+ }
70
+
71
+ [Test ]
72
+ public fun testLast () {
73
+ assertEquals(" three" , Observable .from(" one" , " two" , " three" )!! .toBlockingObservable()!! .last())
74
+ }
75
+
76
+ [Test ]
77
+ public fun testLastEx () {
78
+ assertEquals(" three" , listOf (" one" , " two" , " three" ).asObservable().toBlockingObservable()!! .last())
79
+ }
80
+
81
+ [Test ]
82
+ public fun testLastWithPredicate () {
83
+ assertEquals(" two" , Observable .from(" one" , " two" , " three" )!! .toBlockingObservable()!! .last { x -> x!! .length == 3 })
84
+ }
85
+
86
+ [Test ]
87
+ public fun testLastWithPredicateEx () {
88
+ assertEquals(" two" , listOf (" one" , " two" , " three" ).asObservable().toBlockingObservable()!! .last { x -> x!! .length == 3 })
89
+ }
90
+
91
+ [Test ]
92
+ public fun testMap1 () {
93
+ Observable .from(1 )!! .map { v -> " hello_$v " }!! .subscribe { result -> a!! .received(result) }
94
+ verify(a, times(1 ))!! .received(" hello_1" )
95
+ }
96
+
97
+ [Test ]
98
+ public fun testMap1Ex () {
99
+ 1 .asObservable().map { v -> " hello_$v " }!! .subscribe { result -> a!! .received(result) }
100
+ verify(a, times(1 ))!! .received(" hello_1" )
101
+ }
102
+
103
+ [Test ]
104
+ public fun testMap2 () {
105
+ Observable .from(1 , 2 , 3 )!! .map { v -> " hello_$v " }!! .subscribe { result -> a!! .received(result) }
106
+ verify(a, times(1 ))!! .received(" hello_1" )
107
+ verify(a, times(1 ))!! .received(" hello_2" )
108
+ verify(a, times(1 ))!! .received(" hello_3" )
109
+ }
110
+
111
+ [Test ]
112
+ public fun testMap2Ex () {
113
+ listOf (1 , 2 , 3 ).asObservable().map { v -> " hello_$v " }!! .subscribe { result -> a!! .received(result) }
114
+ verify(a, times(1 ))!! .received(" hello_1" )
115
+ verify(a, times(1 ))!! .received(" hello_2" )
116
+ verify(a, times(1 ))!! .received(" hello_3" )
117
+ }
118
+
119
+ [Test ]
120
+ public fun testMaterialize () {
121
+ Observable .from(1 , 2 , 3 )!! .materialize()!! .subscribe { result -> a!! .received(result) }
122
+ verify(a, times(4 ))!! .received(any(javaClass<Notification <Int >>()))
123
+ verify(a, times(0 ))!! .error(any(javaClass<Exception >()))
124
+ }
125
+
126
+ [Test ]
127
+ public fun testMaterializeEx () {
128
+ listOf (1 , 2 , 3 ).asObservable().materialize()!! .subscribe { result -> a!! .received(result) }
129
+ verify(a, times(4 ))!! .received(any(javaClass<Notification <Int >>()))
130
+ verify(a, times(0 ))!! .error(any(javaClass<Exception >()))
131
+ }
132
+
133
+ public trait ScriptAssertion {
134
+ fun error (e : Exception ? )
135
+
136
+ fun received (e : Any? )
137
+ }
138
+ }
0 commit comments