Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test suite for single and Singles #223

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/test/kotlin/io/reactivex/rxkotlin/SingleTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.reactivex.rxkotlin

import io.reactivex.Flowable
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.observers.LambdaConsumerIntrospection
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Test
import org.mockito.Mockito
import org.mockito.Mockito.verify
Expand Down Expand Up @@ -47,7 +50,43 @@ class SingleTest : KotlinTests() {
.concatAll()
.toList()
.subscribe { result ->
Assert.assertEquals((0 until 10).toList(), result)
assertEquals((0 until 10).toList(), result)
}
}

@Test fun testMergeAllSinglesForObservable() {
val initialData = Observable.just(
Single.just(1),
Single.just(2),
Single.just(3),
Single.just(4)
)

val expected = mutableListOf<Int>().let { list ->
initialData.flatMapSingle { it }.blockingIterable().forEach { list.add(it) }
}
val actual = mutableListOf<Int>().let {list ->
initialData.mergeAllSingles().blockingIterable().forEach { list.add(it) }
}

assertEquals(expected, actual)
}

@Test fun testMergeAllSinglesForFlowable() {
val initialData = Flowable.just(
Single.just(1),
Single.just(2),
Single.just(3),
Single.just(4)
)

val expected = mutableListOf<Int>().let { list ->
initialData.flatMapSingle { it }.blockingIterable().forEach { list.add(it) }
}
val actual = mutableListOf<Int>().let {list ->
initialData.mergeAllSingles().blockingIterable().forEach { list.add(it) }
}

assertEquals(expected, actual)
}
}
297 changes: 192 additions & 105 deletions src/test/kotlin/io/reactivex/rxkotlin/SinglesTest.kt
Original file line number Diff line number Diff line change
@@ -1,114 +1,201 @@
package io.reactivex.rxkotlin

import io.reactivex.Single
import io.reactivex.SingleSource
import io.reactivex.functions.BiFunction
import io.reactivex.functions.Function3
import io.reactivex.functions.Function4
import io.reactivex.functions.Function5
import io.reactivex.functions.Function6
import io.reactivex.functions.Function7
import io.reactivex.functions.Function8
import io.reactivex.functions.Function9
import org.junit.Assert.assertEquals
import org.junit.Test

class SinglesTest : KotlinTests() {

@Test fun testParameterOrder() {
Singles.zip(
SingleSourceInt(1), SingleSourceInt(2),
{one, two ->
assert(one == 1, { -> "Should equal one"})
assert(two == 2, { -> "Should equal two"})
}).blockingGet()

Singles.zip(
SingleSourceInt(1), SingleSourceInt(2),
SingleSourceInt(3),
{one, two, three ->
assert(one == 1, { -> "Should equal one"})
assert(two == 2, { -> "Should equal two"})
assert(three == 3, { -> "Should equal three"})
}).blockingGet()

Singles.zip(
SingleSourceInt(1), SingleSourceInt(2),
SingleSourceInt(3), SingleSourceInt(4),
{one, two, three, four ->
assert(one == 1, { -> "Should equal one"})
assert(two == 2, { -> "Should equal two"})
assert(three == 3, { -> "Should equal three"})
assert(four == 4, { -> "Should equal four"})
}).blockingGet()

Singles.zip(
SingleSourceInt(1), SingleSourceInt(2),
SingleSourceInt(3), SingleSourceInt(4),
SingleSourceInt(5),
{one, two, three, four, five ->
assert(one == 1, { -> "Should equal one"})
assert(two == 2, { -> "Should equal two"})
assert(three == 3, { -> "Should equal three"})
assert(four == 4, { -> "Should equal four"})
assert(five == 5, { -> "Should equal five"})
}).blockingGet()

Singles.zip(
SingleSourceInt(1), SingleSourceInt(2),
SingleSourceInt(3), SingleSourceInt(4),
SingleSourceInt(5), SingleSourceInt(6),
{one, two, three, four, five, six ->
assert(one == 1, { -> "Should equal one"})
assert(two == 2, { -> "Should equal two"})
assert(three == 3, { -> "Should equal three"})
assert(four == 4, { -> "Should equal four"})
assert(five == 5, { -> "Should equal five"})
assert(six == 6, { -> "Should equal six"})
}).blockingGet()

Singles.zip(
SingleSourceInt(1), SingleSourceInt(2),
SingleSourceInt(3), SingleSourceInt(4),
SingleSourceInt(5), SingleSourceInt(6),
SingleSourceInt(7),
{one, two, three, four, five, six, seven ->
assert(one == 1, { -> "Should equal one"})
assert(two == 2, { -> "Should equal two"})
assert(three == 3, { -> "Should equal three"})
assert(four == 4, { -> "Should equal four"})
assert(five == 5, { -> "Should equal five"})
assert(six == 6, { -> "Should equal six"})
assert(seven == 7, { -> "Should equal seven"})
}).blockingGet()

Singles.zip(
SingleSourceInt(1), SingleSourceInt(2),
SingleSourceInt(3), SingleSourceInt(4),
SingleSourceInt(5), SingleSourceInt(6),
SingleSourceInt(7), SingleSourceInt(8),
{one, two, three, four, five, six, seven, eight ->
assert(one == 1, { -> "Should equal one"})
assert(two == 2, { -> "Should equal two"})
assert(three == 3, { -> "Should equal three"})
assert(four == 4, { -> "Should equal four"})
assert(five == 5, { -> "Should equal five"})
assert(six == 6, { -> "Should equal six"})
assert(seven == 7, { -> "Should equal seven"})
assert(eight == 8, { -> "Should equal eight"})
}).blockingGet()

Singles.zip(
SingleSourceInt(1), SingleSourceInt(2),
SingleSourceInt(3), SingleSourceInt(4),
SingleSourceInt(5), SingleSourceInt(6),
SingleSourceInt(7), SingleSourceInt(8),
SingleSourceInt(9),
{one, two, three, four, five, six, seven, eight, nine ->
assert(one == 1, { -> "Should equal one"})
assert(two == 2, { -> "Should equal two"})
assert(three == 3, { -> "Should equal three"})
assert(four == 4, { -> "Should equal four"})
assert(five == 5, { -> "Should equal five"})
assert(six == 6, { -> "Should equal six"})
assert(seven == 7, { -> "Should equal seven"})
assert(eight == 8, { -> "Should equal eight"})
assert(nine == 9, { -> "Should equal nine"})
}).blockingGet()
@Test fun zipTwoSinglesWithExplicitZipper() {
val first = Single.just(1)
val second = Single.just(2)

val expected = Single.zip(first, second, BiFunction<Int, Int, Pair<Int, Int>> { f, s -> Pair(s, f)}).blockingGet()
val actual = Singles.zip(first, second) { f, s -> Pair(s, f) }.blockingGet()

assertEquals(expected, actual)
}
}

fun SingleSourceInt(i: Int): SingleSource<Int> {
return Single.create({ s -> s.onSuccess(i)})
}
@Test fun zipTwoSinglesWithNoExplicitZipper() {
val first = Single.just(1)
val second = Single.just(2)

val expected = Single.zip(first, second, BiFunction<Int, Int, Pair<Int, Int>> { f, s -> Pair(f, s)}).blockingGet()
val actual = Singles.zip(first, second).blockingGet()

assertEquals(expected, actual)
}

@Test fun zipThreeSinglesWithExplicitZipper() {
val first = Single.just(1)
val second = Single.just(2)
val third = Single.just(3)

val expected = Single.zip(first, second, third, Function3<Int, Int, Int, Triple<Int, Int, Int>> { f, s, t -> Triple(t, s, f)}).blockingGet()
val actual = Singles.zip(first, second, third) { f, s, t -> Triple(t, s, f) }.blockingGet()

assertEquals(expected, actual)
}

@Test fun zipThreeSinglesWithNoExplicitZipper() {
val first = Single.just(1)
val second = Single.just(2)
val third = Single.just(3)

val expected = Single.zip(first, second, third, Function3<Int, Int, Int, Triple<Int, Int, Int>> { f, s, t -> Triple(f, s, t)}).blockingGet()
val actual = Singles.zip(first, second, third).blockingGet()

assertEquals(expected, actual)
}

@Test fun zipFourSingles() {
val first = Single.just(1)
val second = Single.just(2)
val third = Single.just(3)
val fourth = Single.just(4)

val zipperFunction = Function4<Int, Int, Int, Int, String> { t1, t2, t3, t4 ->
"$t1$t2$t3$t4"
}

val expected = Single.zip(first, second, third, fourth, zipperFunction).blockingGet()
val actual = Singles.zip(first, second, third, fourth) { t1, t2, t3, t4 ->
"$t1$t2$t3$t4"
}.blockingGet()

assertEquals(expected, actual)
}

@Test fun zipFiveSingles() {
val first = Single.just(1)
val second = Single.just(2)
val third = Single.just(3)
val fourth = Single.just(2)
val fifth = Single.just(3)

val zipperFunction = Function5<Int, Int, Int, Int, Int, String> { t1, t2, t3, t4, t5 ->
"$t1$t2$t3$t4$t5"
}

val expected = Single.zip(first, second, third, fourth, fifth, zipperFunction).blockingGet()
val actual = Singles.zip(first, second, third, fourth, fifth) { t1, t2, t3, t4, t5 ->
"$t1$t2$t3$t4$t5"
}.blockingGet()

assertEquals(expected, actual)
}

@Test fun zipSixSingles() {
val first = Single.just(1)
val second = Single.just(2)
val third = Single.just(3)
val fourth = Single.just(4)
val fifth = Single.just(5)
val sixth = Single.just(6)

val zipperFunction = Function6<Int, Int, Int, Int, Int, Int, String> { t1, t2, t3, t4, t5, t6 ->
"$t1$t2$t3$t4$t5$t6"
}

val expected = Single.zip(first, second, third, fourth, fifth, sixth, zipperFunction).blockingGet()
val actual = Singles.zip(first, second, third, fourth, fifth, sixth) { t1, t2, t3, t4, t5, t6 ->
"$t1$t2$t3$t4$t5$t6"
}.blockingGet()

assertEquals(expected, actual)
}

@Test fun zipSevenSingles() {
val first = Single.just(1)
val second = Single.just(2)
val third = Single.just(3)
val fourth = Single.just(4)
val fifth = Single.just(5)
val sixth = Single.just(6)
val seventh = Single.just(7)

val zipperFunction = Function7<Int, Int, Int, Int, Int, Int, Int, String> { t1, t2, t3, t4, t5, t6, t7 ->
"$t1$t2$t3$t4$t5$t6$t7"
}

val expected = Single.zip(first, second, third, fourth, fifth, sixth, seventh, zipperFunction).blockingGet()
val actual = Singles.zip(first, second, third, fourth, fifth, sixth, seventh) { t1, t2, t3, t4, t5, t6, t7 ->
"$t1$t2$t3$t4$t5$t6$t7"
}.blockingGet()

assertEquals(expected, actual)
}

@Test fun zipEightSingles() {
val first = Single.just(1)
val second = Single.just(2)
val third = Single.just(3)
val fourth = Single.just(4)
val fifth = Single.just(5)
val sixth = Single.just(6)
val seventh = Single.just(7)
val eighth = Single.just(8)

val zipperFunction = Function8<Int, Int, Int, Int, Int, Int, Int, Int, String> { t1, t2, t3, t4, t5, t6, t7, t8 ->
"$t1$t2$t3$t4$t5$t6$t7$t8"
}

val expected = Single.zip(first, second, third, fourth, fifth, sixth, seventh, eighth, zipperFunction).blockingGet()
val actual = Singles.zip(first, second, third, fourth, fifth, sixth, seventh, eighth) { t1, t2, t3, t4, t5, t6, t7, t8 ->
"$t1$t2$t3$t4$t5$t6$t7$t8"
}.blockingGet()

assertEquals(expected, actual)
}

@Test fun zipNineSingles() {
val first = Single.just(1)
val second = Single.just(2)
val third = Single.just(3)
val fourth = Single.just(4)
val fifth = Single.just(5)
val sixth = Single.just(6)
val seventh = Single.just(7)
val eighth = Single.just(8)
val ninth = Single.just(9)

val zipperFunction = Function9<Int, Int, Int, Int, Int, Int, Int, Int, Int, String> { t1, t2, t3, t4, t5, t6, t7, t8, t9 ->
"$t1$t2$t3$t4$t5$t6$t7$t8$t9"
}

val expected = Single.zip(first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, zipperFunction).blockingGet()
val actual = Singles.zip(first, second, third, fourth, fifth, sixth, seventh, eighth, ninth) { t1, t2, t3, t4, t5, t6, t7, t8, t9 ->
"$t1$t2$t3$t4$t5$t6$t7$t8$t9"
}.blockingGet()

assertEquals(expected, actual)
}

@Test fun zipWith() {
val first = Single.just(1)
val second = Single.just(2)

val expected = first.zipWith(second, BiFunction<Int, Int, Pair<Int, Int>> { f, s -> Pair(f, s)}).blockingGet()
val actual = first.zipWith(second).blockingGet()

assertEquals(expected, actual)
}

@Test fun zipWithExplicitZipper() {
val first = Single.just(1)
val second = Single.just(2)

val expected = first.zipWith(second, BiFunction<Int, Int, Pair<Int, Int>> { f, s -> Pair(s, f)}).blockingGet()
val actual = first.zipWith(second) { f, s -> Pair(s, f) }.blockingGet()

assertEquals(expected, actual)
}
}