diff --git a/WorkflowTesting/Sources/Internal/AppliedAction.swift b/WorkflowTesting/Sources/Internal/AppliedAction.swift index 030a2ad72..950464cbd 100644 --- a/WorkflowTesting/Sources/Internal/AppliedAction.swift +++ b/WorkflowTesting/Sources/Internal/AppliedAction.swift @@ -24,11 +24,11 @@ struct AppliedAction { self.erasedAction = action } - func assert(type: ActionType.Type = ActionType.self, file: StaticString, line: UInt, assertions: (ActionType) -> Void) where ActionType.WorkflowType == WorkflowType { + func assert(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) } } diff --git a/WorkflowTesting/Sources/RenderTesterResult.swift b/WorkflowTesting/Sources/RenderTesterResult.swift index 2ada747e2..221e5b7de 100644 --- a/WorkflowTesting/Sources/RenderTesterResult.swift +++ b/WorkflowTesting/Sources/RenderTesterResult.swift @@ -35,9 +35,9 @@ public struct RenderTesterResult { public func verifyState( file: StaticString = #file, line: UInt = #line, - assertions: (WorkflowType.State) -> Void - ) -> RenderTesterResult { - assertions(state) + assertions: (WorkflowType.State) throws -> Void + ) rethrows -> RenderTesterResult { + try assertions(state) return self } @@ -59,13 +59,13 @@ public struct RenderTesterResult { type: ActionType.Type = ActionType.self, file: StaticString = #file, line: UInt = #line, - assertions: (ActionType) -> Void - ) -> RenderTesterResult where ActionType.WorkflowType == WorkflowType { + assertions: (ActionType) throws -> Void + ) rethrows -> RenderTesterResult 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 } @@ -98,13 +98,13 @@ public struct RenderTesterResult { public func verifyOutput( file: StaticString = #file, line: UInt = #line, - assertions: (WorkflowType.Output) -> Void - ) -> RenderTesterResult { + assertions: (WorkflowType.Output) throws -> Void + ) rethrows -> RenderTesterResult { guard let output = output else { XCTFail("No output was produced", file: file, line: line) return self } - assertions(output) + try assertions(output) return self } } diff --git a/WorkflowTesting/Sources/WorkflowActionTester.swift b/WorkflowTesting/Sources/WorkflowActionTester.swift index 3003b1bc6..f0458ae6b 100644 --- a/WorkflowTesting/Sources/WorkflowActionTester.swift +++ b/WorkflowTesting/Sources/WorkflowActionTester.swift @@ -106,13 +106,13 @@ public struct WorkflowActionTester where Action: WorkflowA public func verifyOutput( file: StaticString = #file, line: UInt = #line, - _ assertions: (WorkflowType.Output) -> Void - ) -> WorkflowActionTester { + _ assertions: (WorkflowType.Output) throws -> Void + ) rethrows -> WorkflowActionTester { guard let output = output else { XCTFail("No output was produced", file: file, line: line) return self } - assertions(output) + try assertions(output) return self } @@ -122,8 +122,8 @@ public struct WorkflowActionTester where Action: WorkflowA /// /// - returns: A tester containing the current state and output. @discardableResult - public func verifyState(_ assertions: (WorkflowType.State) -> Void) -> WorkflowActionTester { - assertions(state) + public func verifyState(_ assertions: (WorkflowType.State) throws -> Void) rethrows -> WorkflowActionTester { + try assertions(state) return self } diff --git a/WorkflowTesting/Sources/WorkflowRenderTester.swift b/WorkflowTesting/Sources/WorkflowRenderTester.swift index 41a2ac6d6..4d8bd15ba 100644 --- a/WorkflowTesting/Sources/WorkflowRenderTester.swift +++ b/WorkflowTesting/Sources/WorkflowRenderTester.swift @@ -229,8 +229,8 @@ @discardableResult public func render( file: StaticString = #file, line: UInt = #line, - assertions: (WorkflowType.Rendering) -> Void - ) -> RenderTesterResult { + assertions: (WorkflowType.Rendering) throws -> Void + ) rethrows -> RenderTesterResult { let contextImplementation = TestContext( state: state, expectedWorkflows: expectedWorkflows, @@ -243,7 +243,7 @@ contextImplementation.assertNoLeftOverExpectations() - assertions(rendering) + try assertions(rendering) return RenderTesterResult( state: contextImplementation.state, diff --git a/WorkflowTesting/Tests/WorkflowActionTesterTests.swift b/WorkflowTesting/Tests/WorkflowActionTesterTests.swift index e4ffc28dc..5109fe70c 100644 --- a/WorkflowTesting/Tests/WorkflowActionTesterTests.swift +++ b/WorkflowTesting/Tests/WorkflowActionTesterTests.swift @@ -26,6 +26,16 @@ final class WorkflowActionTesterTests: XCTestCase { .verifyState { XCTAssertTrue($0) } } + func test_stateTransitions_throw() throws { + try TestAction + .tester(withState: false) + .send(action: .toggleTapped) + .verifyState { + try throwingNoop() + XCTAssertTrue($0) + } + } + func test_stateTransitions_equatable() { TestAction .tester(withState: false) @@ -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) @@ -107,3 +127,5 @@ private struct TestWorkflow: Workflow { () } } + +private func throwingNoop() throws {} diff --git a/WorkflowTesting/Tests/WorkflowRenderTesterTests.swift b/WorkflowTesting/Tests/WorkflowRenderTesterTests.swift index 0e11b0ca0..84b56971e 100644 --- a/WorkflowTesting/Tests/WorkflowRenderTesterTests.swift +++ b/WorkflowTesting/Tests/WorkflowRenderTesterTests.swift @@ -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() @@ -265,7 +276,7 @@ private struct SideEffectWorkflow: Workflow { } private struct TestScreen { - var text: String + var text: String? var tapped: () -> Void }