Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sephiroth74 committed Dec 26, 2021
1 parent c37f712 commit 804d97a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package it.sephiroth.android.rxjava3.extensions

import android.os.Looper
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.schedulers.Schedulers
import it.sephiroth.android.rxjava3.extensions.observable.firstInList
import it.sephiroth.android.rxjava3.extensions.observers.AutoDisposableSingleObserver
import it.sephiroth.android.rxjava3.extensions.single.*
import org.junit.Assert
Expand Down Expand Up @@ -145,6 +143,14 @@ class SingleAndroidTest {
Single.just(listOf(null, null)).asObservable().test().await().assertComplete().assertNoValues()
}

@Test
fun test011() {
Single.just(listOf(1, 2, 3, 4, 5)).firstInList { it % 2 == 0 }.test().await().assertComplete().assertValue(2)
Single.just(emptyList<Int>()).firstInList { it % 2 == 0 }.test().await().assertComplete().assertNoValues()
Single.just(listOf(null, 1, 2)).firstInList { null != it && it % 2 == 0 }.test().await().assertComplete().assertValue(2)
Single.just(listOf<Int?>(null, null)).firstInList { null != it && it % 2 == 0 }.test().await().assertComplete().assertNoValues()
}

companion object {
private lateinit var ioThread: Thread
private lateinit var singleThread: Thread
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import it.sephiroth.android.rxjava3.extensions.observable.autoSubscribe
import it.sephiroth.android.rxjava3.extensions.observers.AutoDisposableObserver
import it.sephiroth.android.rxjava3.extensions.observers.AutoDisposableSubscriber
import it.sephiroth.android.rxjava3.extensions.operators.FlowableMapNotNull
import it.sephiroth.android.rxjava3.extensions.single.firstInList
import it.sephiroth.android.rxjava3.extensions.single.firstInListNotNull
import java.util.*
import java.util.concurrent.TimeUnit
import java.util.function.Predicate
Expand Down Expand Up @@ -236,7 +238,7 @@ fun <T> Flowable<T>.toSingle(): Single<T> where T : Any {
* @since 3.0.5
*/
fun <T> Flowable<List<T>>.firstInList(): Maybe<T> {
return this.firstOrError().filter { it.isNotEmpty() }.map { it.first() }
return this.toSingle().firstInList()
}


Expand All @@ -247,7 +249,7 @@ fun <T> Flowable<List<T>>.firstInList(): Maybe<T> {
* @since 3.0.5
*/
fun <T> Flowable<List<T>>.firstInListNotNull(): Maybe<T> {
return this.firstOrError().filter { it.isNotEmpty() }.mapOptional { Optional.ofNullable(it.firstOrNull { item -> item != null }) }
return this.toSingle().firstInListNotNull()
}


Expand All @@ -258,5 +260,5 @@ fun <T> Flowable<List<T>>.firstInListNotNull(): Maybe<T> {
* @since 3.0.5
*/
fun <T> Flowable<List<T>>.firstInList(predicate: Predicate<T>): Maybe<T> {
return this.firstOrError().mapOptional { list -> Optional.ofNullable(list.firstOrNull { item -> predicate.test(item) }) }
return this.toSingle().firstInList(predicate)
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import io.reactivex.rxjava3.schedulers.Schedulers
import it.sephiroth.android.rxjava3.extensions.MuteException
import it.sephiroth.android.rxjava3.extensions.observers.AutoDisposableObserver
import it.sephiroth.android.rxjava3.extensions.operators.ObservableMapNotNull
import it.sephiroth.android.rxjava3.extensions.single.firstInList
import it.sephiroth.android.rxjava3.extensions.single.firstInListNotNull
import java.util.*
import java.util.concurrent.TimeUnit
import java.util.function.Predicate
Expand All @@ -68,7 +70,7 @@ fun <T> Observable<T>.toSingle(): Single<T> where T : Any {
* if the list contains at least one element.
*/
fun <T> Observable<List<T>>.firstInList(): Maybe<T> {
return this.firstOrError().filter { it.isNotEmpty() }.map { it.first() }
return this.toSingle().firstInList()
}

/**
Expand All @@ -78,7 +80,7 @@ fun <T> Observable<List<T>>.firstInList(): Maybe<T> {
* @since 3.0.5
*/
fun <T> Observable<List<T>>.firstInListNotNull(): Maybe<T> {
return this.firstOrError().filter { it.isNotEmpty() }.mapOptional { Optional.ofNullable(it.firstOrNull { item -> item != null }) }
return this.toSingle().firstInListNotNull()
}

/**
Expand All @@ -88,7 +90,7 @@ fun <T> Observable<List<T>>.firstInListNotNull(): Maybe<T> {
* @since 3.0.5
*/
fun <T> Observable<List<T>>.firstInList(predicate: Predicate<T>): Maybe<T> {
return this.firstOrError().mapOptional { list -> Optional.ofNullable(list.firstOrNull { item -> predicate.test(item) }) }
return this.toSingle().firstInList(predicate)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.functions.Function
import it.sephiroth.android.rxjava3.extensions.observers.AutoDisposableSingleObserver
import java.util.*
import java.util.function.Predicate


/**
Expand All @@ -46,25 +47,34 @@ import java.util.*
*/

/**
* If the [Single] returns a [List] of items, this transformer will
* convert the source into a [Maybe] which will emit the very first item of the list,
* If the source [Single] returns a [List] of items, this transformer will
* convert the Single into a [Maybe] which emit the very first item of the list,
* if the list contains at least one element.
*/
fun <T> Single<List<T>>.firstInList(): Maybe<T> {
return this.filter { it.isNotEmpty() }.map { it.first() }
}


/**
* If the [Single] returns a [List] of items, this transformer will
* convert the source into a [Maybe] which will emit the very first non null item of the list.
* If the source [Single] returns a [List] of items, this transformer will
* convert the Single into a [Maybe] which emit the very first non null item of the list.
*
* @since 3.0.5
*/
fun <T> Single<List<T>>.firstInListNotNull(): Maybe<T> {
return this.filter { it.isNotEmpty() }.mapOptional { Optional.ofNullable(it.firstOrNull { item -> item != null }) }
}

/**
* If the source [Single] returns a [List] of items, this transformer will
* convert the Single into a [Maybe] which emit the very first item that match the predicate.
*
* @since 3.0.5
*/
fun <T> Single<List<T>>.firstInList(predicate: Predicate<T>): Maybe<T> {
return this.mapOptional { list -> Optional.ofNullable(list.firstOrNull { item -> predicate.test(item) }) }
}

/**
* Subscribe to this [Single] using an instance of the [AutoDisposableSingleObserver]
*/
Expand Down

0 comments on commit 804d97a

Please sign in to comment.