-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #518 from arkivanov/add-Completable-testAwait
Add Completable.testAwait() testing function
- Loading branch information
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
reaktive-testing/src/commonMain/kotlin/com/badoo/reaktive/test/single/TestAwait.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.badoo.reaktive.test.single | ||
|
||
import com.badoo.reaktive.completable.blockingAwait | ||
import com.badoo.reaktive.single.Single | ||
|
||
/** | ||
* This method is used when you need to wait for an asynchronous operation to finish | ||
* and you can't use or don't want to use the [TestScheduler][com.badoo.reaktive.test.scheduler.TestScheduler]. | ||
* | ||
* It is JavaScript friendly, it returns `Promise` which, when returned from a test method, | ||
* causes the test to wait for completion. In all other targets [Completable.blockingAwait()][blockingAwait] is used. | ||
* | ||
* Please note the following factors: | ||
* - The [mainScheduler][com.badoo.reaktive.scheduler.mainScheduler] is not available in | ||
* Android unit tests and will crash if used; | ||
* - Darwin (Apple) tests are executed on the Main thread and so using the | ||
* [mainScheduler][com.badoo.reaktive.scheduler.mainScheduler] from this method will cause dead lock. | ||
* | ||
* To avoid the problems above use [overrideSchedulers][com.badoo.reaktive.scheduler.overrideSchedulers] | ||
* to replace schedulers with [TestScheduler][com.badoo.reaktive.test.scheduler.TestScheduler] or | ||
* with [TrampolineScheduler][com.badoo.reaktive.scheduler.TrampolineScheduler]. | ||
* | ||
* Usage example: | ||
* ``` | ||
* class MyLogic { | ||
* fun div(a: Int, b: Int): Single<Int> = | ||
* singleFromFunction { a / b } | ||
* .subscribeOn(computationScheduler) | ||
* } | ||
* | ||
* class MyLogicTest { | ||
* @Test | ||
* fun returns_2_WHEN_div_6_by_3() = | ||
* MyLogic() | ||
* .div(6, 3) | ||
* .testAwait { assertEquals(2, it) } | ||
* | ||
* @Test | ||
* fun throws_ArithmeticException_WHEN_div_by_0() = | ||
* MyLogic() | ||
* .div(6, 0) | ||
* .testAwait(assertError = { assertTrue(it is ArithmeticException) }, assertSuccess = { fail("Did not throw") }) | ||
* } | ||
* ``` | ||
* | ||
* @receiver a [Single] for which the test should wait before completing | ||
* @param assertError when provided, it will be called in case of [Single] error and | ||
* the test will fail only when this callback throws an exception. | ||
* This gives an opportunity to assert the error. | ||
* @param assertSuccess when provided, it will be called in case of [Single] success. | ||
* This gives an opportunity to assert the result. | ||
*/ | ||
expect fun <T> Single<T>.testAwait(assertError: ((Throwable) -> Unit)? = null, assertSuccess: (T) -> Unit = {}) |
28 changes: 28 additions & 0 deletions
28
reaktive-testing/src/commonTest/kotlin/com/badoo/reaktive/test/single/TestAwaitTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.badoo.reaktive.test.single | ||
|
||
import com.badoo.reaktive.single.singleOf | ||
import com.badoo.reaktive.single.singleOfError | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertSame | ||
|
||
class TestAwaitTest { | ||
|
||
@Test | ||
fun succeeds_WHEN_upstream_succeeded_and_no_assertSuccess() = | ||
singleOf(1) | ||
.testAwait() | ||
|
||
@Test | ||
fun succeeds_WHEN_upstream_succeeded_and_assertSuccess_did_not_throw() = | ||
singleOf(1) | ||
.testAwait(assertSuccess = { assertEquals(1, it) }) | ||
|
||
@Test | ||
fun succeeds_WHEN_upstream_failed_and_assertError_did_not_throw() { | ||
val error = Exception() | ||
|
||
return singleOfError<Nothing>(error) | ||
.testAwait(assertError = { assertSame(error, it) }) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
reaktive-testing/src/jsMain/kotlin/com/badoo/reaktive/test/single/TestAwait.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.badoo.reaktive.test.single | ||
|
||
import com.badoo.reaktive.single.Single | ||
import com.badoo.reaktive.single.asPromise | ||
import com.badoo.reaktive.single.doOnBeforeSuccess | ||
import com.badoo.reaktive.single.map | ||
import com.badoo.reaktive.single.onErrorReturn | ||
|
||
actual fun <T> Single<T>.testAwait(assertError: ((Throwable) -> Unit)?, assertSuccess: (T) -> Unit): dynamic = | ||
if (assertError == null) { | ||
doOnBeforeSuccess(assertSuccess) | ||
.asPromise() | ||
} else { | ||
map { TestAwaitResult.Success(it) } | ||
.onErrorReturn { TestAwaitResult.Error(it) } | ||
.doOnBeforeSuccess { result -> | ||
when (result) { | ||
is TestAwaitResult.Success -> assertSuccess(result.value) | ||
is TestAwaitResult.Error -> assertError(result.error) | ||
} | ||
} | ||
.asPromise() | ||
} | ||
|
||
private sealed class TestAwaitResult<out T> { | ||
class Success<out T>(val value: T) : TestAwaitResult<T>() | ||
class Error(val error: Throwable) : TestAwaitResult<Nothing>() | ||
} |
20 changes: 20 additions & 0 deletions
20
reaktive-testing/src/jvmNativeCommonMain/kotlin/com/badoo/reaktive/test/single/TestAwait.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.badoo.reaktive.test.single | ||
|
||
import com.badoo.reaktive.single.Single | ||
import com.badoo.reaktive.single.blockingGet | ||
|
||
actual fun <T> Single<T>.testAwait(assertError: ((Throwable) -> Unit)?, assertSuccess: (T) -> Unit) { | ||
if (assertError == null) { | ||
assertSuccess(blockingGet()) | ||
} else { | ||
val result = | ||
try { | ||
blockingGet() | ||
} catch (e: Throwable) { | ||
assertError(e) | ||
return | ||
} | ||
|
||
assertSuccess(result) | ||
} | ||
} |