From cbd11f7927a7d7ded1ef921867d98d58ba19ea24 Mon Sep 17 00:00:00 2001 From: Patrick Steiger Date: Thu, 9 Nov 2023 12:54:18 -0300 Subject: [PATCH] Make suspend functions callable inside `test(worker) { }` We don't need `crossinline` for `testBody`. Usage of cross-inline makes suspend functions non-callable in the test body, which forbids patterns like: ``` test(worker) { sharedFlow.emit(someValue) // assert worker processed it } ``` --- .../src/test/kotlin/com/uber/rib/core/RibCoroutineWorkerTest.kt | 2 ++ .../src/main/kotlin/com/uber/rib/core/TestRibCoroutineWorker.kt | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/android/libraries/rib-base/src/test/kotlin/com/uber/rib/core/RibCoroutineWorkerTest.kt b/android/libraries/rib-base/src/test/kotlin/com/uber/rib/core/RibCoroutineWorkerTest.kt index 89c4b3be3..5971fa9d7 100644 --- a/android/libraries/rib-base/src/test/kotlin/com/uber/rib/core/RibCoroutineWorkerTest.kt +++ b/android/libraries/rib-base/src/test/kotlin/com/uber/rib/core/RibCoroutineWorkerTest.kt @@ -190,6 +190,8 @@ class RibCoroutineWorkerTest { assertThat(worker.innerCoroutineCompleted).isFalse() assertThat(worker.onStopRan).isFalse() test(worker) { + // Quick check that suspend functions can be called inside this block + delay(0) // Assert onStart and inner coroutine started but have not finished (it has delays) assertThat(it.onStartStarted).isTrue() assertThat(it.innerCoroutineStarted).isTrue() diff --git a/android/libraries/rib-test/src/main/kotlin/com/uber/rib/core/TestRibCoroutineWorker.kt b/android/libraries/rib-test/src/main/kotlin/com/uber/rib/core/TestRibCoroutineWorker.kt index 13047244a..f441c0034 100644 --- a/android/libraries/rib-test/src/main/kotlin/com/uber/rib/core/TestRibCoroutineWorker.kt +++ b/android/libraries/rib-test/src/main/kotlin/com/uber/rib/core/TestRibCoroutineWorker.kt @@ -39,7 +39,7 @@ import kotlinx.coroutines.test.runCurrent @OptIn(ExperimentalCoroutinesApi::class) public inline fun TestScope.test( worker: T, - crossinline testBody: TestScope.(T) -> Unit, + testBody: TestScope.(T) -> Unit, ) { val dispatcher = StandardTestDispatcher(testScheduler) val handle = bind(worker, dispatcher)