// COMMON_COROUTINES_TEST // WITH_RUNTIME // WITH_COROUTINES // CHECK_STATE_MACHINE // In this test the following transformation are occuring: // flow$1 -> flowWith$$inlined$flow$1 // flow$1 -> check$$inlined$flow$1 // flow$1 -> flowWith$$inlined$flow$2 // flowWith$$inlined$flow$2 -> check$$inlined$flowWith$1 // All thansformations, except the third, shall generate state-machine. // The third shall not generate state-machine, since it is retransformed. // FILE: inline.kt package flow interface FlowCollector { suspend fun emit(value: T) } interface Flow { suspend fun collect(collector: FlowCollector) } public inline fun flow(crossinline block: suspend FlowCollector.() -> Unit) = object : Flow { override suspend fun collect(collector: FlowCollector) { collector.block() collector.block() } } suspend inline fun Flow.collect(crossinline action: suspend (T) -> Unit) { collect(object : FlowCollector { override suspend fun emit(value: T) { action(value) action(value) } }) } inline fun Flow.flowWith(crossinline builderBlock: suspend Flow.() -> Flow): Flow = flow { builderBlock() builderBlock() } // FILE: box.kt // COMMON_COROUTINES_TEST import flow.* import helpers.* import COROUTINES_PACKAGE.* fun builder(c: suspend () -> Unit) { c.startCoroutine(CheckStateMachineContinuation) } suspend fun check() { val f: Unit = flow { emit(1) }.flowWith { StateMachineChecker.suspendHere() StateMachineChecker.suspendHere() this }.collect { // In this test collect is just terminating operation, which just runs the lazy computations } } fun box(): String { builder { check() } StateMachineChecker.check(numberOfSuspensions = 8) return "OK" }