Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions workflow-runtime/api/workflow-runtime.api
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public final class com/squareup/workflow1/TreeSnapshot {
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
public final fun toByteString ()Lokio/ByteString;
public final fun workflowSnapshotByteString ()Lokio/ByteString;
}

public final class com/squareup/workflow1/TreeSnapshot$Companion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public class TreeSnapshot internal constructor(
sink.readByteString()
}

fun workflowSnapshotByteString(): ByteString? {
return workflowSnapshot?.bytes
}

override fun equals(other: Any?): Boolean = when {
other === this -> true
other !is TreeSnapshot -> false
Expand Down
25 changes: 20 additions & 5 deletions workflow-testing/api/workflow-testing.api
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ public final class com/squareup/workflow1/testing/WorkflowTestParams$StartMode$S
public final fun getSnapshot ()Lcom/squareup/workflow1/Snapshot;
}

public final class com/squareup/workflow1/testing/WorkflowTestParamsKt {
public static final fun createInterceptors (Lcom/squareup/workflow1/testing/WorkflowTestParams;)Ljava/util/List;
}

public final class com/squareup/workflow1/testing/WorkflowTestRuntime {
public static final field Companion Lcom/squareup/workflow1/testing/WorkflowTestRuntime$Companion;
public static final field DEFAULT_TIMEOUT_MS J
Expand Down Expand Up @@ -193,22 +197,33 @@ public final class com/squareup/workflow1/testing/WorkflowTestRuntimeKt {
public final class com/squareup/workflow1/testing/WorkflowTurbine {
public static final field Companion Lcom/squareup/workflow1/testing/WorkflowTurbine$Companion;
public static final field WORKFLOW_TEST_DEFAULT_TIMEOUT_MS J
public fun <init> (Ljava/lang/Object;Lapp/cash/turbine/ReceiveTurbine;)V
public fun <init> (Ljava/lang/Object;Lcom/squareup/workflow1/TreeSnapshot;Lapp/cash/turbine/ReceiveTurbine;Lapp/cash/turbine/ReceiveTurbine;Lapp/cash/turbine/ReceiveTurbine;)V
public final fun awaitNext (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
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;
public final fun awaitNextOutput (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun awaitNextRendering (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun awaitNextRenderingSatisfying (Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun awaitNextSnapshot (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun getFirstRendering ()Ljava/lang/Object;
public final fun getFirstSnapshot ()Lcom/squareup/workflow1/TreeSnapshot;
public final fun skipRenderings (ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

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

public final class com/squareup/workflow1/testing/WorkflowTurbineKt {
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
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
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
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
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
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
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
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
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
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
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
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
public static final fun renderForTestFromStateWith (Lcom/squareup/workflow1/StatefulWorkflow;Ljava/lang/Object;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;)V
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
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
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
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package com.squareup.workflow1.testing
import com.squareup.workflow1.RuntimeConfig
import com.squareup.workflow1.Snapshot
import com.squareup.workflow1.TreeSnapshot
import com.squareup.workflow1.WorkflowInterceptor
import com.squareup.workflow1.WorkflowInterceptor.WorkflowSession
import com.squareup.workflow1.testing.WorkflowTestParams.StartMode
import com.squareup.workflow1.testing.WorkflowTestParams.StartMode.StartFresh
import com.squareup.workflow1.testing.WorkflowTestParams.StartMode.StartFromCompleteSnapshot
import com.squareup.workflow1.testing.WorkflowTestParams.StartMode.StartFromState
import com.squareup.workflow1.testing.WorkflowTestParams.StartMode.StartFromWorkflowSnapshot
import kotlinx.coroutines.CoroutineScope
import org.jetbrains.annotations.TestOnly

/**
Expand Down Expand Up @@ -85,3 +88,33 @@ public class WorkflowTestParams<out StateT>(
public class StartFromState<StateT>(public val state: StateT) : StartMode<StateT>()
}
}

// Helper function to create interceptors from WorkflowTestParams
public fun <StateT> WorkflowTestParams<StateT>.createInterceptors(): List<WorkflowInterceptor> {
val interceptors = mutableListOf<WorkflowInterceptor>()

if (checkRenderIdempotence) {
interceptors += RenderIdempotencyChecker
}

(startFrom as? StartFromState)?.let { startFrom ->
interceptors += object : WorkflowInterceptor {
@Suppress("UNCHECKED_CAST")
override fun <P, S> onInitialState(
props: P,
snapshot: Snapshot?,
workflowScope: CoroutineScope,
proceed: (P, Snapshot?, CoroutineScope) -> S,
session: WorkflowSession
): S {
return if (session.parent == null) {
startFrom.state as S
} else {
proceed(props, snapshot, workflowScope)
}
}
}
}

return interceptors
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import com.squareup.workflow1.Snapshot
import com.squareup.workflow1.StatefulWorkflow
import com.squareup.workflow1.TreeSnapshot
import com.squareup.workflow1.Workflow
import com.squareup.workflow1.WorkflowInterceptor
import com.squareup.workflow1.WorkflowInterceptor.WorkflowSession
import com.squareup.workflow1.config.JvmTestRuntimeConfigTools
import com.squareup.workflow1.renderWorkflowIn
import com.squareup.workflow1.testing.WorkflowTestParams.StartMode.StartFresh
Expand Down Expand Up @@ -301,35 +299,6 @@ public fun <T, PropsT, StateT, OutputT, RenderingT>
}
}

private fun WorkflowTestParams<*>.createInterceptors(): List<WorkflowInterceptor> {
val interceptors = mutableListOf<WorkflowInterceptor>()

if (checkRenderIdempotence) {
interceptors += RenderIdempotencyChecker
}

(startFrom as? StartFromState)?.let { startFrom ->
interceptors += object : WorkflowInterceptor {
@Suppress("UNCHECKED_CAST")
override fun <P, S> onInitialState(
props: P,
snapshot: Snapshot?,
workflowScope: CoroutineScope,
proceed: (P, Snapshot?, CoroutineScope) -> S,
session: WorkflowSession
): S {
return if (session.parent == null) {
startFrom.state as S
} else {
proceed(props, snapshot, workflowScope)
}
}
}
}

return interceptors
}

private fun <T> unwrapCancellationCause(block: () -> T): T {
try {
return block()
Expand Down
Loading
Loading