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
4 changes: 2 additions & 2 deletions WorkflowTesting/Sources/Internal/AppliedAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ struct AppliedAction<WorkflowType: Workflow> {
self.erasedAction = action
}

func assert<ActionType: WorkflowAction>(type: ActionType.Type = ActionType.self, file: StaticString, line: UInt, assertions: (ActionType) -> Void) where ActionType.WorkflowType == WorkflowType {
func assert<ActionType: WorkflowAction>(type: ActionType.Type = ActionType.self, file: StaticString, line: UInt, assertions: (ActionType) throws -> Void) rethrows where ActionType.WorkflowType == WorkflowType {
guard let action = erasedAction as? ActionType else {
XCTFail("Expected action of type \(ActionType.self), got \(erasedAction)", file: file, line: line)
return
}
assertions(action)
try assertions(action)
}
}
18 changes: 9 additions & 9 deletions WorkflowTesting/Sources/RenderTesterResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public struct RenderTesterResult<WorkflowType: Workflow> {
public func verifyState(
file: StaticString = #file,
line: UInt = #line,
assertions: (WorkflowType.State) -> Void
) -> RenderTesterResult<WorkflowType> {
assertions(state)
assertions: (WorkflowType.State) throws -> Void
) rethrows -> RenderTesterResult<WorkflowType> {
try assertions(state)
return self
}

Expand All @@ -59,13 +59,13 @@ public struct RenderTesterResult<WorkflowType: Workflow> {
type: ActionType.Type = ActionType.self,
file: StaticString = #file,
line: UInt = #line,
assertions: (ActionType) -> Void
) -> RenderTesterResult<WorkflowType> where ActionType.WorkflowType == WorkflowType {
assertions: (ActionType) throws -> Void
) rethrows -> RenderTesterResult<WorkflowType> where ActionType.WorkflowType == WorkflowType {
guard let appliedAction = appliedAction else {
XCTFail("No action was produced", file: file, line: line)
return self
}
appliedAction.assert(file: file, line: line, assertions: assertions)
try appliedAction.assert(file: file, line: line, assertions: assertions)
return self
}

Expand Down Expand Up @@ -98,13 +98,13 @@ public struct RenderTesterResult<WorkflowType: Workflow> {
public func verifyOutput(
file: StaticString = #file,
line: UInt = #line,
assertions: (WorkflowType.Output) -> Void
) -> RenderTesterResult<WorkflowType> {
assertions: (WorkflowType.Output) throws -> Void
) rethrows -> RenderTesterResult<WorkflowType> {
guard let output = output else {
XCTFail("No output was produced", file: file, line: line)
return self
}
assertions(output)
try assertions(output)
return self
}
}
Expand Down
10 changes: 5 additions & 5 deletions WorkflowTesting/Sources/WorkflowActionTester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ public struct WorkflowActionTester<WorkflowType, Action> where Action: WorkflowA
public func verifyOutput(
file: StaticString = #file,
line: UInt = #line,
_ assertions: (WorkflowType.Output) -> Void
) -> WorkflowActionTester<WorkflowType, Action> {
_ assertions: (WorkflowType.Output) throws -> Void
) rethrows -> WorkflowActionTester<WorkflowType, Action> {
guard let output = output else {
XCTFail("No output was produced", file: file, line: line)
return self
}
assertions(output)
try assertions(output)
return self
}

Expand All @@ -122,8 +122,8 @@ public struct WorkflowActionTester<WorkflowType, Action> where Action: WorkflowA
///
/// - returns: A tester containing the current state and output.
@discardableResult
public func verifyState(_ assertions: (WorkflowType.State) -> Void) -> WorkflowActionTester<WorkflowType, Action> {
assertions(state)
public func verifyState(_ assertions: (WorkflowType.State) throws -> Void) rethrows -> WorkflowActionTester<WorkflowType, Action> {
try assertions(state)
return self
}

Expand Down
6 changes: 3 additions & 3 deletions WorkflowTesting/Sources/WorkflowRenderTester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@
@discardableResult
public func render(
file: StaticString = #file, line: UInt = #line,
assertions: (WorkflowType.Rendering) -> Void
) -> RenderTesterResult<WorkflowType> {
assertions: (WorkflowType.Rendering) throws -> Void
) rethrows -> RenderTesterResult<WorkflowType> {
let contextImplementation = TestContext(
state: state,
expectedWorkflows: expectedWorkflows,
Expand All @@ -243,7 +243,7 @@

contextImplementation.assertNoLeftOverExpectations()

assertions(rendering)
try assertions(rendering)

return RenderTesterResult<WorkflowType>(
state: contextImplementation.state,
Expand Down
22 changes: 22 additions & 0 deletions WorkflowTesting/Tests/WorkflowActionTesterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ final class WorkflowActionTesterTests: XCTestCase {
.verifyState { XCTAssertTrue($0) }
}

func test_stateTransitions_throw() throws {
Copy link
Collaborator Author

@nononoah nononoah Feb 17, 2021

Choose a reason for hiding this comment

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

This tests and those that follow are demonstrative of the testing style these changes support, and serve to ensure we continue to support this testing style.

If others find them unnecessary, I'm happy to remove them.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is fine. I don't think it's strictly necessary — it's more of a "ensure the code doesn't change" test than a unit test, but this will help prevent unintended breakages.

try TestAction
.tester(withState: false)
.send(action: .toggleTapped)
.verifyState {
try throwingNoop()
XCTAssertTrue($0)
}
}

func test_stateTransitions_equatable() {
TestAction
.tester(withState: false)
Expand All @@ -49,6 +59,16 @@ final class WorkflowActionTesterTests: XCTestCase {
}
}

func test_outputs_throw() throws {
try TestAction
.tester(withState: false)
.send(action: .exitTapped)
.verifyOutput { output in
try throwingNoop()
XCTAssertEqual(output, .finished)
}
}

func test_outputs_equatable() {
TestAction
.tester(withState: false)
Expand Down Expand Up @@ -107,3 +127,5 @@ private struct TestWorkflow: Workflow {
()
}
}

private func throwingNoop() throws {}
13 changes: 12 additions & 1 deletion WorkflowTesting/Tests/WorkflowRenderTesterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ final class WorkflowRenderTesterTests: XCTestCase {
.assertNoAction()
}

func test_simple_render_throw() throws {
let renderTester = TestWorkflow(initialText: "initial").renderTester()

try renderTester
.render { screen in
let text = try XCTUnwrap(screen.text)
XCTAssertEqual("initial", text)
}
.assertNoAction()
}

func test_action() {
let renderTester = TestWorkflow(initialText: "initial").renderTester()

Expand Down Expand Up @@ -265,7 +276,7 @@ private struct SideEffectWorkflow: Workflow {
}

private struct TestScreen {
var text: String
var text: String?
var tapped: () -> Void
}

Expand Down