Skip to content

Commit

Permalink
Make suspend functions callable inside test(worker) { }
Browse files Browse the repository at this point in the history
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
}
```
  • Loading branch information
psteiger committed Nov 9, 2023
1 parent 6847098 commit cbd11f7
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import kotlinx.coroutines.test.runCurrent
@OptIn(ExperimentalCoroutinesApi::class)
public inline fun <T : RibCoroutineWorker> TestScope.test(
worker: T,
crossinline testBody: TestScope.(T) -> Unit,
testBody: TestScope.(T) -> Unit,
) {
val dispatcher = StandardTestDispatcher(testScheduler)
val handle = bind(worker, dispatcher)
Expand Down

0 comments on commit cbd11f7

Please sign in to comment.