Skip to content

Commit 4455981

Browse files
Merge pull request #78 from square/zachklipp/cleanup-rendertestresult
Cleanup the RenderTestResult interface after introducing WorkflowOutput.
2 parents 9833c49 + efe8710 commit 4455981

File tree

4 files changed

+14
-235
lines changed

4 files changed

+14
-235
lines changed

workflow-testing/api/workflow-testing.api

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,7 @@ public final class com/squareup/workflow/testing/RenderIdempotencyChecker : com/
99

1010
public abstract interface class com/squareup/workflow/testing/RenderTestResult {
1111
public abstract fun verifyAction (Lkotlin/jvm/functions/Function1;)V
12-
public abstract fun verifyActionOutput (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow/testing/RenderTestResult;
1312
public abstract fun verifyActionResult (Lkotlin/jvm/functions/Function2;)V
14-
public abstract fun verifyActionState (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow/testing/RenderTestResult;
15-
public abstract fun verifyNoActionOutput ()Lcom/squareup/workflow/testing/RenderTestResult;
16-
}
17-
18-
public final class com/squareup/workflow/testing/RenderTestResult$DefaultImpls {
19-
public static fun verifyActionResult (Lcom/squareup/workflow/testing/RenderTestResult;Lkotlin/jvm/functions/Function2;)V
2013
}
2114

2215
public abstract interface class com/squareup/workflow/testing/RenderTester {

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -202,30 +202,10 @@ internal class RealRenderTester<PropsT, StateT, OutputT, RenderingT>(
202202
block(action)
203203
}
204204

205-
override fun verifyActionState(block: (newState: StateT) -> Unit) = apply {
206-
verifyAction { action ->
207-
// Don't care about output.
208-
val (newState, _) = action.applyTo(state)
209-
block(newState)
210-
}
211-
}
212-
213-
override fun verifyActionOutput(block: (output: OutputT) -> Unit) = apply {
214-
verifyAction { action ->
215-
val (_, output) = action.applyTo(state)
216-
if (output == null) {
217-
throw AssertionError("Expected action to set an output")
218-
}
219-
block(output.value)
220-
}
221-
}
222-
223-
override fun verifyNoActionOutput() = apply {
224-
verifyAction { action ->
225-
val (_, output) = action.applyTo(state)
226-
if (output != null) {
227-
throw AssertionError("Expected no output, but action set output to: ${output.value}")
228-
}
205+
override fun verifyActionResult(block: (newState: StateT, output: WorkflowOutput<OutputT>?) -> Unit) {
206+
verifyAction {
207+
val (state, output) = it.applyTo(state)
208+
block(state, output)
229209
}
230210
}
231211

workflow-testing/src/main/java/com/squareup/workflow/testing/RenderTestResult.kt

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.squareup.workflow.testing
1717

1818
import com.squareup.workflow.WorkflowAction
19+
import com.squareup.workflow.WorkflowOutput
1920

2021
/**
2122
* Result of a [RenderTester.render] call that can be used to verify that a [WorkflowAction] was
@@ -37,46 +38,6 @@ interface RenderTestResult<StateT, OutputT> {
3738
*/
3839
fun verifyAction(block: (WorkflowAction<StateT, OutputT>) -> Unit)
3940

40-
/**
41-
* If the render pass handled either a workflow/worker output or a rendering event, "executes" the
42-
* action with the state passed to [testRender], then invokes [block] with the resulting state
43-
* value.
44-
*
45-
* If the workflow didn't process any actions, `newState` will be the initial state.
46-
*
47-
* Note that by using this method, you're also testing the implementation of your action. This can
48-
* be useful if your actions are anonymous. If they are a sealed class or enum, use [verifyAction]
49-
* instead and write separate unit tests for your action implementations.
50-
*/
51-
fun verifyActionState(block: (newState: StateT) -> Unit): RenderTestResult<StateT, OutputT>
52-
53-
/**
54-
* If the render pass handled either a workflow/worker output or a rendering event, "executes" the
55-
* action with the state passed to [testRender], verifies that the action set an output, then
56-
* invokes [block] with the resulting output value.
57-
*
58-
* If the workflow didn't process any actions, or no output was set, an [AssertionError] will be
59-
* thrown.
60-
*
61-
* Note that by using this method, you're also testing the implementation of your action. This can
62-
* be useful if your actions are anonymous. If they are a sealed class or enum, use [verifyAction]
63-
* instead and write separate unit tests for your action implementations.
64-
*/
65-
fun verifyActionOutput(block: (output: OutputT) -> Unit): RenderTestResult<StateT, OutputT>
66-
67-
/**
68-
* If the render pass handled either a workflow/worker output or a rendering event, "executes" the
69-
* action with the state passed to [testRender], and then verifies that the action did not set
70-
* any output.
71-
*
72-
* If the workflow didn't process any actions, this method will do nothing.
73-
*
74-
* Note that by using this method, you're also testing the implementation of your action. This can
75-
* be useful if your actions are anonymous. If they are a sealed class or enum, use [verifyAction]
76-
* instead and write separate unit tests for your action implementations.
77-
*/
78-
fun verifyNoActionOutput(): RenderTestResult<StateT, OutputT>
79-
8041
/**
8142
* Asserts that the render pass handled either a workflow/worker output or a rendering event,
8243
* "executes" the action with the state passed to [testRender], then invokes [block] with the
@@ -88,23 +49,6 @@ interface RenderTestResult<StateT, OutputT> {
8849
* Note that by using this method, you're also testing the implementation of your action. This can
8950
* be useful if your actions are anonymous. If they are a sealed class or enum, use [verifyAction]
9051
* instead and write separate unit tests for your action implementations.
91-
*
92-
* Note that if [OutputT] is nullable, this method does not distinguish between an no output and
93-
* null output. Use [RenderTestResult.verifyActionOutput] and
94-
* [RenderTestResult.verifyNoActionOutput] instead.
9552
*/
96-
@Deprecated("Use verifyActionState and verify(No)ActionOutput")
97-
fun verifyActionResult(block: (newState: StateT, output: OutputT?) -> Unit) {
98-
var state: StateT? = null
99-
var output: OutputT? = null
100-
verifyActionState { state = it }
101-
try {
102-
verifyActionOutput { output = it }
103-
} catch (e: AssertionError) {
104-
// No output was set, leave as null.
105-
}
106-
107-
@Suppress("UNCHECKED_CAST")
108-
block(state as StateT, output)
109-
}
53+
fun verifyActionResult(block: (newState: StateT, output: WorkflowOutput<OutputT>?) -> Unit)
11054
}

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

Lines changed: 8 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,7 @@ class RealRenderTesterTest {
673673
}
674674
}
675675

676-
@Suppress("DEPRECATION")
677-
@Test fun `verifyAction and verifyActionResult pass when no action processed`() {
676+
@Test fun `verifyAction allows no action`() {
678677
val workflow = Workflow.stateless<Unit, Nothing, Sink<TestAction>> {
679678
actionSink.contraMap { it }
680679
}
@@ -690,74 +689,7 @@ class RealRenderTesterTest {
690689
}
691690
}
692691

693-
@Suppress("DEPRECATION")
694-
@Test fun `verifyActionResult works`() {
695-
class TestAction : WorkflowAction<String, String> {
696-
override fun Updater<String, String>.apply() {
697-
state = "new state"
698-
setOutput("output")
699-
}
700-
}
701-
702-
val workflow = Workflow.stateful<Unit, String, String, Sink<TestAction>>(
703-
initialState = { "initial" },
704-
render = { _, _ -> actionSink.contraMap { it } }
705-
)
706-
val testResult = workflow.testRender(Unit)
707-
.render { sink ->
708-
sink.send(TestAction())
709-
}
710-
711-
testResult.verifyActionResult { state, output ->
712-
assertEquals("new state", state)
713-
assertEquals("output", output)
714-
}
715-
}
716-
717-
@Test fun `verifyActionState and verifyActionOutput chain`() {
718-
class TestAction : WorkflowAction<String, String> {
719-
override fun Updater<String, String>.apply() {
720-
state = "new state"
721-
setOutput("output")
722-
}
723-
}
724-
725-
val workflow = Workflow.stateful<Unit, String, String, Sink<TestAction>>(
726-
initialState = { "initial" },
727-
render = { _, _ -> actionSink.contraMap { it } }
728-
)
729-
val testResult = workflow.testRender(Unit)
730-
.render { sink ->
731-
sink.send(TestAction())
732-
}
733-
734-
testResult
735-
.verifyActionState { assertEquals("new state", it) }
736-
.verifyActionOutput { assertEquals("output", it) }
737-
}
738-
739-
@Test fun `verifyActionState and verifyNoActionOutput chain`() {
740-
class TestAction : WorkflowAction<String, String> {
741-
override fun Updater<String, String>.apply() {
742-
state = "new state"
743-
}
744-
}
745-
746-
val workflow = Workflow.stateful<Unit, String, String, Sink<TestAction>>(
747-
initialState = { "initial" },
748-
render = { _, _ -> actionSink.contraMap { it } }
749-
)
750-
val testResult = workflow.testRender(Unit)
751-
.render { sink ->
752-
sink.send(TestAction())
753-
}
754-
755-
testResult
756-
.verifyActionState { assertEquals("new state", it) }
757-
.verifyNoActionOutput()
758-
}
759-
760-
@Test fun `verifyActionState allows no action`() {
692+
@Test fun `verifyActionResult allows no action`() {
761693
val workflow = Workflow.stateless<Unit, Nothing, Sink<TestAction>> {
762694
actionSink.contraMap { it }
763695
}
@@ -766,52 +698,16 @@ class RealRenderTesterTest {
766698
// Don't send to sink!
767699
}
768700

769-
testResult.verifyAction { assertEquals(noAction(), it) }
770-
testResult.verifyActionState { newState ->
701+
testResult.verifyActionResult { newState, output ->
771702
assertSame(Unit, newState)
703+
assertNull(output)
772704
}
773705
}
774706

775-
@Test fun `verifyActionState handles new state`() {
707+
@Test fun `verifyActionResult handles new state and output`() {
776708
class TestAction : WorkflowAction<String, String> {
777709
override fun Updater<String, String>.apply() {
778710
state = "new state"
779-
}
780-
}
781-
782-
val workflow = Workflow.stateful<Unit, String, String, Sink<TestAction>>(
783-
initialState = { "initial" },
784-
render = { _, _ -> actionSink.contraMap { it } }
785-
)
786-
val testResult = workflow.testRender(Unit)
787-
.render { sink ->
788-
sink.send(TestAction())
789-
}
790-
791-
testResult.verifyActionState { state ->
792-
assertEquals("new state", state)
793-
}
794-
}
795-
796-
@Test fun `verifyActionOutput allows no action`() {
797-
val workflow = Workflow.stateless<Unit, Nothing, Sink<TestAction>> {
798-
actionSink.contraMap { it }
799-
}
800-
val testResult = workflow.testRender(Unit)
801-
.render {
802-
// Don't send to sink!
803-
}
804-
805-
testResult.verifyAction { assertEquals(noAction(), it) }
806-
val error = assertFailsWith<AssertionError> {
807-
testResult.verifyActionOutput {}
808-
}
809-
assertEquals("Expected action to set an output", error.message)
810-
}
811-
812-
@Test fun `verifyActionOutput handles output`() {
813-
class TestAction : WorkflowAction<String, String> {
814-
override fun Updater<String, String>.apply() {
815711
setOutput("output")
816712
}
817713
}
@@ -825,44 +721,10 @@ class RealRenderTesterTest {
825721
sink.send(TestAction())
826722
}
827723

828-
testResult.verifyActionOutput { output ->
829-
assertEquals("output", output)
830-
}
831-
}
832-
833-
@Test fun `verifyNoActionOutput allows no action`() {
834-
val workflow = Workflow.stateless<Unit, Nothing, Sink<TestAction>> {
835-
actionSink.contraMap { it }
836-
}
837-
val testResult = workflow.testRender(Unit)
838-
.render {
839-
// Don't send to sink!
840-
}
841-
842-
testResult.verifyAction { assertEquals(noAction(), it) }
843-
testResult.verifyNoActionOutput()
844-
}
845-
846-
@Test fun `verifyNoActionOutput fails when output is set`() {
847-
class TestAction : WorkflowAction<String, String> {
848-
override fun Updater<String, String>.apply() {
849-
setOutput("output")
850-
}
851-
}
852-
853-
val workflow = Workflow.stateful<Unit, String, String, Sink<TestAction>>(
854-
initialState = { "initial" },
855-
render = { _, _ -> actionSink.contraMap { it } }
856-
)
857-
val testResult = workflow.testRender(Unit)
858-
.render { sink ->
859-
sink.send(TestAction())
860-
}
861-
862-
val error = assertFailsWith<AssertionError> {
863-
testResult.verifyNoActionOutput()
724+
testResult.verifyActionResult { state, output ->
725+
assertEquals("new state", state)
726+
assertEquals("output", output?.value)
864727
}
865-
assertEquals("Expected no output, but action set output to: output", error.message)
866728
}
867729

868730
@Test fun `render is executed multiple times`() {

0 commit comments

Comments
 (0)