Skip to content

Commit a8888a3

Browse files
Make side effect expectations explicit requirement.
1 parent e85d260 commit a8888a3

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ internal class RealRenderTester<PropsT, StateT, OutputT, RenderingT>(
9898
}
9999

100100
private var explicitWorkerExpectationsRequired: Boolean = false
101+
private var explicitSideEffectExpectationsRequired: Boolean = false
101102
override val actionSink: Sink<WorkflowAction<PropsT, StateT, OutputT>> get() = this
102103

103104
override fun expectWorkflow(
@@ -125,6 +126,10 @@ internal class RealRenderTester<PropsT, StateT, OutputT, RenderingT>(
125126
expectWorker(description = "unexpected worker", exactMatch = false) { _, _, _ -> true }
126127
}
127128

129+
if (!explicitSideEffectExpectationsRequired) {
130+
expectSideEffect(description = "unexpected side effect", exactMatch = false) { true }
131+
}
132+
128133
// Clone the expectations to run a "dry" render pass.
129134
val noopContext = deepCloneForRender()
130135
workflow.render(props, state, RenderContext(noopContext, workflow))
@@ -251,6 +256,11 @@ internal class RealRenderTester<PropsT, StateT, OutputT, RenderingT>(
251256
explicitWorkerExpectationsRequired = true
252257
}
253258

259+
override fun requireExplicitSideEffectExpectations():
260+
RenderTester<PropsT, StateT, OutputT, RenderingT> = this.apply {
261+
explicitSideEffectExpectationsRequired = true
262+
}
263+
254264
override fun send(value: WorkflowAction<PropsT, StateT, OutputT>) {
255265
checkNoOutputs()
256266
check(processedAction == null) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ public abstract class RenderTester<PropsT, StateT, OutputT, RenderingT> {
244244
public abstract fun requireExplicitWorkerExpectations():
245245
RenderTester<PropsT, StateT, OutputT, RenderingT>
246246

247+
public abstract fun requireExplicitSideEffectExpectations():
248+
RenderTester<PropsT, StateT, OutputT, RenderingT>
249+
247250
/**
248251
* Describes a call to
249252
* [RenderContext.renderChild][com.squareup.workflow1.BaseRenderContext.renderChild].

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ internal class RealRenderTesterTest {
106106
runningSideEffect("the key") {}
107107
}
108108
val tester = workflow.testRender(Unit)
109+
.requireExplicitSideEffectExpectations()
109110
.expectSideEffect(key = "the key")
110111
.expectSideEffect(description = "duplicate match") { it == "the key" }
111112

@@ -326,7 +327,7 @@ internal class RealRenderTesterTest {
326327
val workflow = Workflow.stateless<Unit, Nothing, Unit> {
327328
runningSideEffect("effect") {}
328329
}
329-
val tester = workflow.testRender(Unit)
330+
val tester = workflow.testRender(Unit).requireExplicitSideEffectExpectations()
330331

331332
val error = assertFailsWith<AssertionError> {
332333
tester.render()
@@ -340,6 +341,7 @@ internal class RealRenderTesterTest {
340341
runningSideEffect("unexpected") {}
341342
}
342343
val tester = workflow.testRender(Unit)
344+
.requireExplicitSideEffectExpectations()
343345
.expectSideEffect("expected")
344346

345347
val error = assertFailsWith<AssertionError> {
@@ -353,6 +355,7 @@ internal class RealRenderTesterTest {
353355
runningSideEffect("effect") {}
354356
}
355357
val tester = workflow.testRender(Unit)
358+
.requireExplicitSideEffectExpectations()
356359
.expectSideEffect("effect")
357360
.expectSideEffect(description = "custom", exactMatch = true) { key -> "effect" in key }
358361

@@ -546,6 +549,30 @@ internal class RealRenderTesterTest {
546549
tester.render()
547550
}
548551

552+
@Test fun `runningSideEffect doesn't throw when none expected`() {
553+
val workflow = Workflow.stateless<Unit, Nothing, Unit> {
554+
runningSideEffect(key = "foo") { }
555+
}
556+
val tester = workflow.testRender(Unit)
557+
tester.render()
558+
}
559+
560+
@Test fun `runningSideEffect does throw when none expected and require explicit side effect is set`() {
561+
val key = "foo"
562+
val workflow = Workflow.stateless<Unit, Nothing, Unit> {
563+
runningSideEffect(key = key) { }
564+
}
565+
val tester = workflow.testRender(Unit).requireExplicitSideEffectExpectations()
566+
val error = assertFailsWith<AssertionError> {
567+
tester.render()
568+
}
569+
570+
assertEquals(
571+
"Tried to run unexpected side effect with key \"$key\"",
572+
error.message
573+
)
574+
}
575+
549576
@Test fun `runningWorker does throw when none expected and require explicit workers is set`() {
550577
class MySpecialWorker : Worker<Nothing> {
551578
override fun doesSameWorkAs(otherWorker: Worker<*>): Boolean = true

0 commit comments

Comments
 (0)