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

Add AnyWorkflow(rendering:) to make a constant-rendering workflow #56

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions Workflow/Sources/AnyWorkflow+Rendering.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2020 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

extension AnyWorkflow where Output == Never {
/// Creates an AnyWorkflow that does nothing but echo the given `rendering`.
///
/// - Note: To use with `RenderTester`, use `expectRenderingWorkflow`
public init(rendering: Rendering) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this extension adds a lot of value. Simple enough to use RenderingWorkflow as-is, and we won't need any other expect...() method.

self = RenderingWorkflow(rendering: rendering).asAnyWorkflow()
}
}

struct RenderingWorkflow<Rendering>: Workflow {
var rendering: Rendering
typealias Output = Never
typealias State = Void

func render(state: State, context: RenderContext<Self>) -> Rendering {
return rendering
}
}
45 changes: 45 additions & 0 deletions Workflow/Tests/AnyWorkflowRenderingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2020 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import XCTest
@testable import Workflow

public class AnyWorkflowRenderingTests: XCTestCase {
func testRenderingString() {
let workflow = AnyWorkflow(rendering: "Hello")
let node = WorkflowNode(workflow: PassthroughWorkflow(workflow))

XCTAssertEqual(node.render(), "Hello")
}

func testRenderingInt() {
let workflow = AnyWorkflow(rendering: 160)
let node = WorkflowNode(workflow: PassthroughWorkflow(workflow))

XCTAssertEqual(node.render(), 160)
}
}

private struct PassthroughWorkflow<Rendering>: Workflow {
var child: AnyWorkflow<Rendering, Never>
init(_ child: AnyWorkflow<Rendering, Never>) {
self.child = child
}

func render(state: Void, context: RenderContext<Self>) -> Rendering {
child.rendered(in: context)
}
}
19 changes: 19 additions & 0 deletions WorkflowTesting/Sources/WorkflowRenderTester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,25 @@
}
}

extension RenderTester {
/// Expect a constant rendering workflow as created with `AnyWorkflow(rendering:)`
///
/// - Parameters:
/// - key: The key of the expected workflow (if specified).
/// - rendering: The rendering result that should be returned when this workflow is rendered.
public func expectRenderingWorkflow<Rendering>(
key: String = "",
producingRendering rendering: Rendering,
file: StaticString = #file, line: UInt = #line
) -> RenderTester<WorkflowType> {
return expectWorkflow(
type: RenderingWorkflow<Rendering>.self,
producingRendering: rendering,
file: file, line: line
)
}
}

extension Collection {
fileprivate func appending(_ element: Element) -> [Element] {
return self + [element]
Expand Down
20 changes: 20 additions & 0 deletions WorkflowTesting/Tests/WorkflowRenderTesterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ final class WorkflowRenderTesterTests: XCTestCase {
XCTAssertEqual("Failed", state.text)
}
}

func test_renderingWorkflow() {
WrappingWorkflow(child: AnyWorkflow(rendering: "not-called"))
.renderTester()
.expectRenderingWorkflow(producingRendering: "real")
.render { rendering in
XCTAssertEqual("->real<-", rendering)
}
.assertNoOutput()
}
}

private struct TestWorkflow: Workflow {
Expand Down Expand Up @@ -320,3 +330,13 @@ private struct ChildWorkflow: Workflow {
String(text.reversed())
}
}

private struct WrappingWorkflow: Workflow {
var child: AnyWorkflow<String, Never>

typealias State = Void

func render(state: State, context: RenderContext<Self>) -> String {
"->" + child.rendered(in: context) + "<-"
}
}