Skip to content

Commit

Permalink
Merge branch 'master' into bump-version-to-1.1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkadii Ivanov authored Mar 20, 2020
2 parents 404ead0 + 0feb2ea commit 035ea9b
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import com.badoo.reaktive.base.SuccessCallback
import com.badoo.reaktive.base.subscribeSafe
import com.badoo.reaktive.disposable.Disposable
import com.badoo.reaktive.single.Single
import com.badoo.reaktive.single.asMaybe
import com.badoo.reaktive.single.SingleCallbacks
import com.badoo.reaktive.single.SingleObserver
import com.badoo.reaktive.single.single

fun <T> Maybe<T>.switchIfEmpty(other: Maybe<T>): Maybe<T> =
maybe { emitter ->
Expand All @@ -27,5 +29,19 @@ fun <T> Maybe<T>.switchIfEmpty(other: Maybe<T>): Maybe<T> =
}

fun <T> Maybe<T>.switchIfEmpty(other: Single<T>): Single<T> =
switchIfEmpty(other.asMaybe())
.asSingleOrError()
single { emitter ->
subscribe(
object : MaybeObserver<T>, SuccessCallback<T> by emitter, ErrorCallback by emitter {
override fun onSubscribe(disposable: Disposable) {
emitter.setDisposable(disposable)
}

override fun onComplete() {
other.subscribeSafe(
object : SingleObserver<T>, Observer by this, SingleCallbacks<T> by emitter {
}
)
}
}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.badoo.reaktive.maybe

import com.badoo.reaktive.single.singleOf
import com.badoo.reaktive.test.base.assertError
import com.badoo.reaktive.test.base.assertSubscribed
import com.badoo.reaktive.test.base.hasSubscribers
import com.badoo.reaktive.test.maybe.TestMaybe
import com.badoo.reaktive.test.single.TestSingle
import com.badoo.reaktive.test.single.assertSuccess
import com.badoo.reaktive.test.single.test
import kotlin.test.Test
import kotlin.test.assertFalse

class SwitchIfEmptySingleTest : MaybeToSingleTests by MaybeToSingleTestsImpl({ switchIfEmpty(singleOf(0)) }) {

private val upstream = TestMaybe<Int?>()
private val other = TestSingle<Int?>()
private val observer = upstream.switchIfEmpty(other).test()

@Test
fun succeeds_WHEN_upstream_succeeded_with_non_null_value() {
upstream.onSuccess(0)

observer.assertSuccess(0)
}

@Test
fun succeeds_WHEN_upstream_succeeded_with_null_value() {
upstream.onSuccess(null)

observer.assertSuccess(null)
}

@Test
fun does_not_subscribe_to_other_IF_upstream_not_finished() {
assertFalse(other.hasSubscribers)
}

@Test
fun does_not_subscribe_to_other_WHEN_upstream_succeeded() {
upstream.onSuccess(0)

assertFalse(other.hasSubscribers)
}

@Test
fun subscribes_to_other_WHEN_upstream_completed() {
upstream.onComplete()

observer.assertSubscribed()
}

@Test
fun succeeds_WHEN_upstream_completed_and_other_succeeded_with_non_null_value() {
upstream.onComplete()
other.onSuccess(0)

observer.assertSuccess(0)
}

@Test
fun succeeds_WHEN_upstream_completed_and_other_succeeded_with_null_value() {
upstream.onComplete()
other.onSuccess(null)

observer.assertSuccess(null)
}

@Test
fun produces_error_WHEN_upstream_completed_and_other_produced_error() {
val error = Exception()
upstream.onComplete()
other.onError(error)

observer.assertError(error)
}
}

0 comments on commit 035ea9b

Please sign in to comment.