Skip to content

Commit 7884585

Browse files
committed
Initial Kotlin support
1 parent 5657920 commit 7884585

File tree

5 files changed

+176
-1
lines changed

5 files changed

+176
-1
lines changed

gradle/buildscript.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ dependencies {
88
classpath 'com.mapvine:gradle-cobertura-plugin:0.1'
99
classpath 'gradle-release:gradle-release:1.1.5'
1010
classpath 'org.ajoberstar:gradle-git:0.5.0'
11-
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.6.+'
1211
}

language-adaptors/rxjava-kotlin/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
buildscript {
2+
repositories() {
3+
mavenCentral()
4+
}
5+
6+
dependencies {
7+
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.6.+'
8+
}
9+
}
10+
111
apply plugin: 'kotlin'
212
apply plugin: 'osgi'
313

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package rx.lang.kotlin
2+
3+
import rx.Subscription
4+
import rx.Observer
5+
import rx.Observable
6+
7+
public fun<T> Function1<Observer<in T>, Subscription>.asObservable(): Observable<T> {
8+
return Observable.create{ this(it!!) }!!
9+
}
10+
11+
public fun<T> Iterable<T>.asObservable(): Observable<T> {
12+
return Observable.from(this)!!
13+
}
14+
15+
public fun<T> T.asObservable(): Observable<T> {
16+
return Observable.from(this)!!
17+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package rx.lang.kotlin
2+
3+
import org.junit.Test
4+
5+
6+
public class SchedulersTests {
7+
[Test]
8+
public fun testComputationThreadPool(){
9+
10+
}
11+
}

0 commit comments

Comments
 (0)