Skip to content

Commit 8ec0b3f

Browse files
Merge pull request #1489 from square/sedwards/testparams-launchfortesting-overloads
Add WorkflowTestParams overloads to launchForTestingFromStateWith
2 parents fba3733 + dbd2c5b commit 8ec0b3f

3 files changed

Lines changed: 112 additions & 2 deletions

File tree

workflow-testing/api/workflow-testing.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,12 @@ public final class com/squareup/workflow1/testing/WorkflowTestRuntimeKt {
202202
public static final fun launchForTestingFromStartWith (Lcom/squareup/workflow1/Workflow;Ljava/lang/Object;Lcom/squareup/workflow1/testing/WorkflowTestParams;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
203203
public static synthetic fun launchForTestingFromStartWith$default (Lcom/squareup/workflow1/Workflow;Lcom/squareup/workflow1/testing/WorkflowTestParams;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Ljava/lang/Object;
204204
public static synthetic fun launchForTestingFromStartWith$default (Lcom/squareup/workflow1/Workflow;Ljava/lang/Object;Lcom/squareup/workflow1/testing/WorkflowTestParams;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Ljava/lang/Object;
205+
public static final fun launchForTestingFromStateWith (Lcom/squareup/workflow1/StatefulWorkflow;Ljava/lang/Object;Lcom/squareup/workflow1/testing/WorkflowTestParams;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;)V
206+
public static final fun launchForTestingFromStateWith (Lcom/squareup/workflow1/StatefulWorkflow;Ljava/lang/Object;Ljava/lang/Object;Lcom/squareup/workflow1/testing/WorkflowTestParams;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
205207
public static final fun launchForTestingFromStateWith (Lcom/squareup/workflow1/StatefulWorkflow;Ljava/lang/Object;Ljava/lang/Object;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
206208
public static final fun launchForTestingFromStateWith (Lcom/squareup/workflow1/StatefulWorkflow;Ljava/lang/Object;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;)V
209+
public static synthetic fun launchForTestingFromStateWith$default (Lcom/squareup/workflow1/StatefulWorkflow;Ljava/lang/Object;Lcom/squareup/workflow1/testing/WorkflowTestParams;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
210+
public static synthetic fun launchForTestingFromStateWith$default (Lcom/squareup/workflow1/StatefulWorkflow;Ljava/lang/Object;Ljava/lang/Object;Lcom/squareup/workflow1/testing/WorkflowTestParams;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Ljava/lang/Object;
207211
public static synthetic fun launchForTestingFromStateWith$default (Lcom/squareup/workflow1/StatefulWorkflow;Ljava/lang/Object;Ljava/lang/Object;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Ljava/lang/Object;
208212
public static synthetic fun launchForTestingFromStateWith$default (Lcom/squareup/workflow1/StatefulWorkflow;Ljava/lang/Object;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
209213
public static final fun launchForTestingWith (Lcom/squareup/workflow1/StatefulWorkflow;Ljava/lang/Object;Lcom/squareup/workflow1/testing/WorkflowTestParams;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;

workflow-testing/src/main/java/com/squareup/workflow1/testing/WorkflowTestRuntime.kt

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,36 @@ public fun <T, PropsT, StateT, OutputT, RenderingT>
230230
initialState: StateT,
231231
context: CoroutineContext = EmptyCoroutineContext,
232232
block: suspend WorkflowTestRuntime<PropsT, OutputT, RenderingT>.() -> T
233+
): T = launchForTestingFromStateWith(
234+
props = props,
235+
initialState = initialState,
236+
testParams = WorkflowTestParams(),
237+
context = context,
238+
block = block,
239+
)
240+
241+
/**
242+
* Creates a [WorkflowTestRuntime] to run this workflow for unit testing.
243+
* If the workflow is [stateful][StatefulWorkflow], [initialState][StatefulWorkflow.initialState]
244+
* is not called. Instead, the workflow is started from the given [initialState].
245+
*
246+
* The [startFrom][WorkflowTestParams.startFrom] value in [testParams] is ignored. This function
247+
* always starts from [initialState].
248+
*
249+
* All workflow-related coroutines are cancelled when the block exits.
250+
*/
251+
@Deprecated("Use renderForTest and WorkflowTurbine instead.")
252+
@TestOnly
253+
public fun <T, PropsT, StateT, OutputT, RenderingT>
254+
StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>.launchForTestingFromStateWith(
255+
props: PropsT,
256+
initialState: StateT,
257+
testParams: WorkflowTestParams<StateT>,
258+
context: CoroutineContext = EmptyCoroutineContext,
259+
block: suspend WorkflowTestRuntime<PropsT, OutputT, RenderingT>.() -> T
233260
): T = launchForTestingWith(
234261
props,
235-
WorkflowTestParams(StartFromState(initialState)),
262+
testParams.withStartState(initialState),
236263
context,
237264
block
238265
)
@@ -251,7 +278,43 @@ public fun <StateT, OutputT, RenderingT>
251278
initialState: StateT,
252279
context: CoroutineContext = EmptyCoroutineContext,
253280
block: suspend WorkflowTestRuntime<Unit, OutputT, RenderingT>.() -> Unit
254-
): Unit = launchForTestingFromStateWith(Unit, initialState, context, block)
281+
): Unit = launchForTestingFromStateWith(
282+
initialState = initialState,
283+
testParams = WorkflowTestParams(),
284+
context = context,
285+
block = block,
286+
)
287+
288+
/**
289+
* Creates a [WorkflowTestRuntime] to run this workflow for unit testing.
290+
* If the workflow is [stateful][StatefulWorkflow], [initialState][StatefulWorkflow.initialState]
291+
* is not called. Instead, the workflow is started from the given [initialState].
292+
*
293+
* The [startFrom][WorkflowTestParams.startFrom] value in [testParams] is ignored. This function
294+
* always starts from [initialState].
295+
*
296+
* All workflow-related coroutines are cancelled when the block exits.
297+
*/
298+
@Deprecated("Use renderForTest and WorkflowTurbine instead.")
299+
@TestOnly
300+
public fun <StateT, OutputT, RenderingT>
301+
StatefulWorkflow<Unit, StateT, OutputT, RenderingT>.launchForTestingFromStateWith(
302+
initialState: StateT,
303+
testParams: WorkflowTestParams<StateT>,
304+
context: CoroutineContext = EmptyCoroutineContext,
305+
block: suspend WorkflowTestRuntime<Unit, OutputT, RenderingT>.() -> Unit
306+
): Unit = launchForTestingFromStateWith(Unit, initialState, testParams, context, block)
307+
308+
private fun <StateT> WorkflowTestParams<StateT>.withStartState(initialState: StateT): WorkflowTestParams<StateT> =
309+
WorkflowTestParams(
310+
startFrom = StartFromState(initialState),
311+
checkRenderIdempotence = checkRenderIdempotence,
312+
runtimeConfig = runtimeConfig,
313+
deprecatedLaunchSchedulerMode = deprecatedLaunchSchedulerMode,
314+
autoAdvanceOnStartup = autoAdvanceOnStartup,
315+
autoAdvanceBeforeAwait = autoAdvanceBeforeAwait,
316+
autoAdvanceBeforeHasCheck = autoAdvanceBeforeHasCheck,
317+
)
255318

256319
/**
257320
* Creates a [WorkflowTestRuntime] to run this workflow for unit testing.

workflow-testing/src/test/java/com/squareup/workflow1/testing/WorkflowTestRuntimeTest.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,49 @@ internal class WorkflowTestRuntimeTest {
167167
}
168168
}
169169

170+
@Test fun `fromStateWith props overload forwards testParams`() {
171+
var renderCount = 0
172+
val workflow = Workflow.stateful<String, Int, Nothing, Int>(
173+
initialState = { _, _ -> fail("Should start from explicit initialState") },
174+
render = { props, state ->
175+
renderCount++
176+
state + props.length
177+
},
178+
snapshot = { null }
179+
)
180+
181+
workflow.launchForTestingFromStateWith(
182+
props = "abc",
183+
initialState = 10,
184+
testParams = WorkflowTestParams(checkRenderIdempotence = false)
185+
) {
186+
assertEquals(13, awaitNextRendering())
187+
}
188+
189+
assertEquals(1, renderCount)
190+
}
191+
192+
@Test fun `fromStateWith unit overload forwards testParams`() {
193+
var renderCount = 0
194+
val workflow = Workflow.stateful<Unit, Int, Nothing, Int>(
195+
initialState = { _, _ -> fail("Should start from explicit initialState") },
196+
render = { _, state ->
197+
renderCount++
198+
state
199+
},
200+
snapshot = { null }
201+
)
202+
203+
workflow.launchForTestingFromStateWith(
204+
initialState = 77,
205+
testParams = WorkflowTestParams(checkRenderIdempotence = false)
206+
) {
207+
assertEquals(77, awaitNextRendering())
208+
}
209+
210+
assertEquals(1, renderCount)
211+
}
212+
170213
@Test fun `detects render side effects`() {
171214
var renderCount = 0
172215
val workflow = Workflow.stateless<Unit, Nothing, Unit> {

0 commit comments

Comments
 (0)