Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce alternative testing infrastructure to test single render passes more directly. #349

Merged
merged 3 commits into from
May 17, 2019

Conversation

zach-klippenstein
Copy link
Collaborator

@zach-klippenstein zach-klippenstein commented May 16, 2019

Given a StatefulWorkflow or StatelessWorkflow, call .testRender() on it.
Pass the InputT, and for StatefulWorkflows also pass the StateT.
This returns a TestRenderResult which provides access to the actual rendering
as well as methods to assert that children or workers were rendered and invoke
their output handlers.

@zach-klippenstein zach-klippenstein added this to In progress in Workflow (Kotlin) via automation May 16, 2019
@zach-klippenstein zach-klippenstein changed the title WIP: RenderTester Introduce alternative testing infrastructure to test single render passes more directly. May 16, 2019
@zach-klippenstein zach-klippenstein changed the base branch from master to zachklipp/fix-bounds May 16, 2019 20:07
@zach-klippenstein zach-klippenstein force-pushed the zachklipp/rendertester branch 2 times, most recently from ac47ed4 to 92584ef Compare May 16, 2019 20:26
@zach-klippenstein zach-klippenstein marked this pull request as ready for review May 16, 2019 20:26
@@ -28,7 +28,7 @@ import kotlinx.coroutines.Deferred
*
* @see RealRenderContext
*/
internal data class Behavior<StateT, out OutputT : Any>(
data class Behavior<StateT, out OutputT : Any> internal constructor(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sad to make this public, but don't want to duplicate the code in the test module.

*/
internal class RealRenderContext<StateT, OutputT : Any>(
class RealRenderContext<StateT, OutputT : Any>(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very sad to make this public. See above comment.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I brought this up in the Kotlin Slack. I should file a ticket: https://kotlinlang.slack.com/archives/C0B8PUJGZ/p1555962527006200

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you do, lemme know I will vote.

@@ -30,17 +30,21 @@ internal typealias AnyId = WorkflowId<*, *, *>
* Value type that can be used to distinguish between different workflows of different types or
* the same type (in that case using a [name]).
*/
internal data class WorkflowId<in InputT, out OutputT : Any, out RenderingT>
data class WorkflowId<in InputT, out OutputT : Any, out RenderingT>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢

@zach-klippenstein zach-klippenstein added this to the v0.16.0 milestone May 16, 2019
@zach-klippenstein zach-klippenstein changed the base branch from zachklipp/fix-bounds to master May 16, 2019 21:41
@@ -0,0 +1,20 @@
package com.squareup.workflow.testing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copyright

val result = workflow.testRender()

result.assertNoWorkflowsRendered()
result.assertNoWorkersRendered()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: do we really render a worker? Don't we run them?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spent a whole 5 minutes googling the correct tenses of "to run", I think I got it right now.

@@ -0,0 +1,144 @@
package com.squareup.workflow.testing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copyright

*/
internal class RealRenderContext<StateT, OutputT : Any>(
class RealRenderContext<StateT, OutputT : Any>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I brought this up in the Kotlin Slack. I should file a ticket: https://kotlinlang.slack.com/archives/C0B8PUJGZ/p1555962527006200

* @see StatefulWorkflow.testRender
* @see com.squareup.workflow.StatelessWorkflow.testRender
*/
class MockChildWorkflow<I, R>(private val renderer: (I) -> R) : Workflow<I, Nothing, R> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FakeChildWorkflow?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually records the input though so you can check it later, doesn't that make it technically a "mock"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes. This blog is handy 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although we lose symmetry with the worker then, which actually is a fake…

I want to add more instrumentation on them anyway, so they'll both be mocks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vRallev @rjrjr I just pushed another commit that changes the API a little. Now you call assert* and execute*Output* directly on the workers/workflows themselves, from within a block. Please take another look. If people like it i'll squash before merging.

* @see com.squareup.workflow.StatefulWorkflow.testRender
* @see com.squareup.workflow.StatelessWorkflow.testRender
*/
class MockWorker<T>(val name: String) : Worker<T> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FakeWorker?

/**
* A mock implementation of [Worker] for use in tests with `testRender` and [TestRenderResult].
*
* Not this [Worker] can not actually emit any output itself. Use
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo on Not -> Note (I believe this should read "Note this [Worker] can not actually emit any output...")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

* @see StatefulWorkflow.testRender
* @see com.squareup.workflow.StatelessWorkflow.testRender
*/
class MockChildWorkflow<I, R>(private val renderer: (I) -> R) : Workflow<I, Nothing, R> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes. This blog is handy 👍

…sses more directly.

Given a `StatefulWorkflow` or `StatelessWorkflow`, call `.testRender()` on it.
Pass the `InputT`, and for `StatefulWorkflow`s also pass the `StateT`.
This returns a `TestRenderResult` which provides access to the actual rendering
as well as methods to assert that children or workers were rendered and invoke
their output handlers.
@rjrjr
Copy link
Contributor

rjrjr commented May 17, 2019

Still LG with the tidier syntax (extension on the fakes).

@zach-klippenstein zach-klippenstein merged commit 459a31d into master May 17, 2019
Workflow (Kotlin) automation moved this from In progress to Done May 17, 2019
@zach-klippenstein zach-klippenstein deleted the zachklipp/rendertester branch May 17, 2019 19:23
@zach-klippenstein zach-klippenstein added the kotlin Affects the Kotlin library. label Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kotlin Affects the Kotlin library.
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants