Skip to content

Commit 14d657e

Browse files
authored
Merge pull request #1427 from Zikstar/isaaco/update_render_for_test
Add missing features to renderForTest Api
2 parents b9e854e + cb733ce commit 14d657e

File tree

8 files changed

+655
-81
lines changed

8 files changed

+655
-81
lines changed

workflow-runtime/api/workflow-runtime.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public final class com/squareup/workflow1/TreeSnapshot {
4444
public fun equals (Ljava/lang/Object;)Z
4545
public fun hashCode ()I
4646
public final fun toByteString ()Lokio/ByteString;
47+
public final fun workflowSnapshotByteString ()Lokio/ByteString;
4748
}
4849

4950
public final class com/squareup/workflow1/TreeSnapshot$Companion {

workflow-runtime/src/commonMain/kotlin/com/squareup/workflow1/TreeSnapshot.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public class TreeSnapshot internal constructor(
6363
sink.readByteString()
6464
}
6565

66+
fun workflowSnapshotByteString(): ByteString? {
67+
return workflowSnapshot?.bytes
68+
}
69+
6670
override fun equals(other: Any?): Boolean = when {
6771
other === this -> true
6872
other !is TreeSnapshot -> false

workflow-testing/api/workflow-testing.api

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ public final class com/squareup/workflow1/testing/WorkflowTestParams$StartMode$S
159159
public final fun getSnapshot ()Lcom/squareup/workflow1/Snapshot;
160160
}
161161

162+
public final class com/squareup/workflow1/testing/WorkflowTestParamsKt {
163+
public static final fun createInterceptors (Lcom/squareup/workflow1/testing/WorkflowTestParams;)Ljava/util/List;
164+
}
165+
162166
public final class com/squareup/workflow1/testing/WorkflowTestRuntime {
163167
public static final field Companion Lcom/squareup/workflow1/testing/WorkflowTestRuntime$Companion;
164168
public static final field DEFAULT_TIMEOUT_MS J
@@ -193,22 +197,33 @@ public final class com/squareup/workflow1/testing/WorkflowTestRuntimeKt {
193197
public final class com/squareup/workflow1/testing/WorkflowTurbine {
194198
public static final field Companion Lcom/squareup/workflow1/testing/WorkflowTurbine$Companion;
195199
public static final field WORKFLOW_TEST_DEFAULT_TIMEOUT_MS J
196-
public fun <init> (Ljava/lang/Object;Lapp/cash/turbine/ReceiveTurbine;)V
200+
public fun <init> (Ljava/lang/Object;Lcom/squareup/workflow1/TreeSnapshot;Lapp/cash/turbine/ReceiveTurbine;Lapp/cash/turbine/ReceiveTurbine;Lapp/cash/turbine/ReceiveTurbine;)V
197201
public final fun awaitNext (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
198202
public static synthetic fun awaitNext$default (Lcom/squareup/workflow1/testing/WorkflowTurbine;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
203+
public final fun awaitNextOutput (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
199204
public final fun awaitNextRendering (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
200205
public final fun awaitNextRenderingSatisfying (Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
206+
public final fun awaitNextSnapshot (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
201207
public final fun getFirstRendering ()Ljava/lang/Object;
208+
public final fun getFirstSnapshot ()Lcom/squareup/workflow1/TreeSnapshot;
202209
public final fun skipRenderings (ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
203210
}
204211

205212
public final class com/squareup/workflow1/testing/WorkflowTurbine$Companion {
206213
}
207214

208215
public final class com/squareup/workflow1/testing/WorkflowTurbineKt {
209-
public static final fun renderForTest (Lcom/squareup/workflow1/Workflow;Lkotlin/coroutines/CoroutineContext;Ljava/util/List;Ljava/util/Set;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;)V
210-
public static final fun renderForTest (Lcom/squareup/workflow1/Workflow;Lkotlinx/coroutines/flow/StateFlow;Lkotlin/coroutines/CoroutineContext;Ljava/util/List;Ljava/util/Set;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;)V
211-
public static synthetic fun renderForTest$default (Lcom/squareup/workflow1/Workflow;Lkotlin/coroutines/CoroutineContext;Ljava/util/List;Ljava/util/Set;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
212-
public static synthetic fun renderForTest$default (Lcom/squareup/workflow1/Workflow;Lkotlinx/coroutines/flow/StateFlow;Lkotlin/coroutines/CoroutineContext;Ljava/util/List;Ljava/util/Set;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
216+
public static final fun renderForTest (Lcom/squareup/workflow1/StatefulWorkflow;Lcom/squareup/workflow1/testing/WorkflowTestParams;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;)V
217+
public static final fun renderForTest (Lcom/squareup/workflow1/StatefulWorkflow;Lkotlinx/coroutines/flow/StateFlow;Lcom/squareup/workflow1/testing/WorkflowTestParams;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;)V
218+
public static synthetic fun renderForTest$default (Lcom/squareup/workflow1/StatefulWorkflow;Lcom/squareup/workflow1/testing/WorkflowTestParams;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
219+
public static synthetic fun renderForTest$default (Lcom/squareup/workflow1/StatefulWorkflow;Lkotlinx/coroutines/flow/StateFlow;Lcom/squareup/workflow1/testing/WorkflowTestParams;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
220+
public static final fun renderForTestForStartWith (Lcom/squareup/workflow1/Workflow;Lkotlin/coroutines/CoroutineContext;Lcom/squareup/workflow1/testing/WorkflowTestParams;Ljava/util/List;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;)V
221+
public static final fun renderForTestForStartWith (Lcom/squareup/workflow1/Workflow;Lkotlinx/coroutines/flow/StateFlow;Lcom/squareup/workflow1/testing/WorkflowTestParams;Lkotlin/coroutines/CoroutineContext;Ljava/util/List;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;)V
222+
public static synthetic fun renderForTestForStartWith$default (Lcom/squareup/workflow1/Workflow;Lkotlin/coroutines/CoroutineContext;Lcom/squareup/workflow1/testing/WorkflowTestParams;Ljava/util/List;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
223+
public static synthetic fun renderForTestForStartWith$default (Lcom/squareup/workflow1/Workflow;Lkotlinx/coroutines/flow/StateFlow;Lcom/squareup/workflow1/testing/WorkflowTestParams;Lkotlin/coroutines/CoroutineContext;Ljava/util/List;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
224+
public static final fun renderForTestFromStateWith (Lcom/squareup/workflow1/StatefulWorkflow;Ljava/lang/Object;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;)V
225+
public static final fun renderForTestFromStateWith (Lcom/squareup/workflow1/StatefulWorkflow;Lkotlinx/coroutines/flow/StateFlow;Ljava/lang/Object;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;)V
226+
public static synthetic fun renderForTestFromStateWith$default (Lcom/squareup/workflow1/StatefulWorkflow;Ljava/lang/Object;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
227+
public static synthetic fun renderForTestFromStateWith$default (Lcom/squareup/workflow1/StatefulWorkflow;Lkotlinx/coroutines/flow/StateFlow;Ljava/lang/Object;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
213228
}
214229

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package com.squareup.workflow1.testing
33
import com.squareup.workflow1.RuntimeConfig
44
import com.squareup.workflow1.Snapshot
55
import com.squareup.workflow1.TreeSnapshot
6+
import com.squareup.workflow1.WorkflowInterceptor
7+
import com.squareup.workflow1.WorkflowInterceptor.WorkflowSession
68
import com.squareup.workflow1.testing.WorkflowTestParams.StartMode
79
import com.squareup.workflow1.testing.WorkflowTestParams.StartMode.StartFresh
810
import com.squareup.workflow1.testing.WorkflowTestParams.StartMode.StartFromCompleteSnapshot
911
import com.squareup.workflow1.testing.WorkflowTestParams.StartMode.StartFromState
1012
import com.squareup.workflow1.testing.WorkflowTestParams.StartMode.StartFromWorkflowSnapshot
13+
import kotlinx.coroutines.CoroutineScope
1114
import org.jetbrains.annotations.TestOnly
1215

1316
/**
@@ -85,3 +88,33 @@ public class WorkflowTestParams<out StateT>(
8588
public class StartFromState<StateT>(public val state: StateT) : StartMode<StateT>()
8689
}
8790
}
91+
92+
// Helper function to create interceptors from WorkflowTestParams
93+
public fun <StateT> WorkflowTestParams<StateT>.createInterceptors(): List<WorkflowInterceptor> {
94+
val interceptors = mutableListOf<WorkflowInterceptor>()
95+
96+
if (checkRenderIdempotence) {
97+
interceptors += RenderIdempotencyChecker
98+
}
99+
100+
(startFrom as? StartFromState)?.let { startFrom ->
101+
interceptors += object : WorkflowInterceptor {
102+
@Suppress("UNCHECKED_CAST")
103+
override fun <P, S> onInitialState(
104+
props: P,
105+
snapshot: Snapshot?,
106+
workflowScope: CoroutineScope,
107+
proceed: (P, Snapshot?, CoroutineScope) -> S,
108+
session: WorkflowSession
109+
): S {
110+
return if (session.parent == null) {
111+
startFrom.state as S
112+
} else {
113+
proceed(props, snapshot, workflowScope)
114+
}
115+
}
116+
}
117+
}
118+
119+
return interceptors
120+
}

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import com.squareup.workflow1.Snapshot
88
import com.squareup.workflow1.StatefulWorkflow
99
import com.squareup.workflow1.TreeSnapshot
1010
import com.squareup.workflow1.Workflow
11-
import com.squareup.workflow1.WorkflowInterceptor
12-
import com.squareup.workflow1.WorkflowInterceptor.WorkflowSession
1311
import com.squareup.workflow1.config.JvmTestRuntimeConfigTools
1412
import com.squareup.workflow1.renderWorkflowIn
1513
import com.squareup.workflow1.testing.WorkflowTestParams.StartMode.StartFresh
@@ -301,35 +299,6 @@ public fun <T, PropsT, StateT, OutputT, RenderingT>
301299
}
302300
}
303301

304-
private fun WorkflowTestParams<*>.createInterceptors(): List<WorkflowInterceptor> {
305-
val interceptors = mutableListOf<WorkflowInterceptor>()
306-
307-
if (checkRenderIdempotence) {
308-
interceptors += RenderIdempotencyChecker
309-
}
310-
311-
(startFrom as? StartFromState)?.let { startFrom ->
312-
interceptors += object : WorkflowInterceptor {
313-
@Suppress("UNCHECKED_CAST")
314-
override fun <P, S> onInitialState(
315-
props: P,
316-
snapshot: Snapshot?,
317-
workflowScope: CoroutineScope,
318-
proceed: (P, Snapshot?, CoroutineScope) -> S,
319-
session: WorkflowSession
320-
): S {
321-
return if (session.parent == null) {
322-
startFrom.state as S
323-
} else {
324-
proceed(props, snapshot, workflowScope)
325-
}
326-
}
327-
}
328-
}
329-
330-
return interceptors
331-
}
332-
333302
private fun <T> unwrapCancellationCause(block: () -> T): T {
334303
try {
335304
return block()

0 commit comments

Comments
 (0)