From 202355fcf8dbb7ac1669bf98d00bcbed44ee002f Mon Sep 17 00:00:00 2001 From: Ben Lu Date: Wed, 17 May 2023 11:25:38 -0700 Subject: [PATCH] [WorkflowTesting] Use CustomDump to better visualize failure messages --- Package.swift | 6 +++- WorkflowTesting.podspec | 1 + .../Sources/RenderTesterResult.swift | 7 ++-- .../Sources/WorkflowActionTester.swift | 11 ++++--- .../Sources/WorkflowRenderTester.swift | 7 ++-- .../WorkflowRenderTesterFailureTests.swift | 33 +++++++++++++++++-- 6 files changed, 50 insertions(+), 15 deletions(-) diff --git a/Package.swift b/Package.swift index 8ec771305..388e19b84 100644 --- a/Package.swift +++ b/Package.swift @@ -87,6 +87,7 @@ let package = Package( .package(url: "https://github.com/ReactiveCocoa/ReactiveSwift.git", from: "7.1.1"), .package(url: "https://github.com/ReactiveX/RxSwift.git", from: "6.2.0"), .package(url: "https://github.com/nicklockwood/SwiftFormat", exact: "0.44.14"), + .package(url: "https://github.com/pointfreeco/swift-custom-dump.git", from: "0.6.1"), ], targets: [ // MARK: Workflow @@ -103,7 +104,10 @@ let package = Package( ), .target( name: "WorkflowTesting", - dependencies: ["Workflow"], + dependencies: [ + .product(name: "CustomDump", package: "swift-custom-dump"), + "Workflow", + ], path: "WorkflowTesting/Sources" ), .testTarget( diff --git a/WorkflowTesting.podspec b/WorkflowTesting.podspec index 7f9d33f66..73e70d02e 100644 --- a/WorkflowTesting.podspec +++ b/WorkflowTesting.podspec @@ -19,6 +19,7 @@ Pod::Spec.new do |s| s.source_files = 'WorkflowTesting/Sources/**/*.swift' s.dependency 'Workflow', "#{s.version}" + s.dependency 'CustomDump', '~> 0.6.1' s.framework = 'XCTest' s.pod_target_xcconfig = { diff --git a/WorkflowTesting/Sources/RenderTesterResult.swift b/WorkflowTesting/Sources/RenderTesterResult.swift index 221e5b7de..9cd13914e 100644 --- a/WorkflowTesting/Sources/RenderTesterResult.swift +++ b/WorkflowTesting/Sources/RenderTesterResult.swift @@ -14,6 +14,7 @@ * limitations under the License. */ +import CustomDump import Workflow import XCTest @@ -77,7 +78,7 @@ public struct RenderTesterResult { line: UInt = #line ) -> RenderTesterResult where ActionType.WorkflowType == WorkflowType, ActionType: Equatable { return verifyAction(file: file, line: line) { appliedAction in - XCTAssertEqual(appliedAction, action, file: file, line: line) + XCTAssertNoDifference(appliedAction, action, file: file, line: line) } } @@ -117,7 +118,7 @@ extension RenderTesterResult where WorkflowType.State: Equatable { file: StaticString = #file, line: UInt = #line ) -> RenderTesterResult { - XCTAssertEqual(state, expectedState, file: file, line: line) + XCTAssertNoDifference(state, expectedState, file: file, line: line) return self } } @@ -131,7 +132,7 @@ extension RenderTesterResult where WorkflowType.Output: Equatable { line: UInt = #line ) -> RenderTesterResult { return verifyOutput(file: file, line: line) { output in - XCTAssertEqual(output, expectedOutput, file: file, line: line) + XCTAssertNoDifference(output, expectedOutput, file: file, line: line) } } } diff --git a/WorkflowTesting/Sources/WorkflowActionTester.swift b/WorkflowTesting/Sources/WorkflowActionTester.swift index f0458ae6b..cefa30462 100644 --- a/WorkflowTesting/Sources/WorkflowActionTester.swift +++ b/WorkflowTesting/Sources/WorkflowActionTester.swift @@ -14,6 +14,7 @@ * limitations under the License. */ +import CustomDump import Workflow import XCTest @@ -32,10 +33,10 @@ extension WorkflowAction { /// .tester(withState: .firstState) /// .send(action: .exampleAction) /// .verifyOutput { output in -/// XCTAssertEqual(.finished, output) +/// XCTAssertNoDifference(.finished, output) /// } /// .verifyState { state in -/// XCTAssertEqual(.differentState, state) +/// XCTAssertNoDifference(.differentState, state) /// } /// ``` /// @@ -47,7 +48,7 @@ extension WorkflowAction { /// .send(action: .actionProducingNoOutput) /// .assertNoOutput() /// .verifyState { state in -/// XCTAssertEqual(.differentState, state) +/// XCTAssertNoDifference(.differentState, state) /// } /// ``` /// @@ -143,7 +144,7 @@ extension WorkflowActionTester where WorkflowType.State: Equatable { @discardableResult public func assert(state expectedState: WorkflowType.State, file: StaticString = #file, line: UInt = #line) -> WorkflowActionTester { return verifyState { actualState in - XCTAssertEqual(actualState, expectedState, file: file, line: line) + XCTAssertNoDifference(actualState, expectedState, file: file, line: line) } } } @@ -157,7 +158,7 @@ extension WorkflowActionTester where WorkflowType.Output: Equatable { @discardableResult public func assert(output expectedOutput: WorkflowType.Output, file: StaticString = #file, line: UInt = #line) -> WorkflowActionTester { return verifyOutput { actualOutput in - XCTAssertEqual(actualOutput, expectedOutput, file: file, line: line) + XCTAssertNoDifference(actualOutput, expectedOutput, file: file, line: line) } } } diff --git a/WorkflowTesting/Sources/WorkflowRenderTester.swift b/WorkflowTesting/Sources/WorkflowRenderTester.swift index 5acf2ca50..5f53996d4 100644 --- a/WorkflowTesting/Sources/WorkflowRenderTester.swift +++ b/WorkflowTesting/Sources/WorkflowRenderTester.swift @@ -19,6 +19,7 @@ // `@testable import Workflow` will fail compilation in Release mode. #if DEBUG +import CustomDump import XCTest @testable import Workflow @@ -60,7 +61,7 @@ extension Workflow { /// producingOutput: nil /// ) /// .render { rendering in -/// XCTAssertEqual("expected text on rendering", rendering.text) +/// XCTAssertNoDifference("expected text on rendering", rendering.text) /// } /// .assert(state: TestWorkflow.State()) /// .assert(output: TestWorkflow.Output.finished) @@ -71,7 +72,7 @@ extension Workflow { /// workflow /// .renderTester() /// .render { rendering in -/// XCTAssertEqual("expected text on rendering", rendering.text) +/// XCTAssertNoDifference("expected text on rendering", rendering.text) /// } /// ``` /// @@ -80,7 +81,7 @@ extension Workflow { /// workflow /// .renderTester() /// .render { rendering in -/// XCTAssertEqual("expected text on rendering", rendering.text) +/// XCTAssertNoDifference("expected text on rendering", rendering.text) /// rendering.updateText("updated") /// } /// .assert( diff --git a/WorkflowTesting/Tests/WorkflowRenderTesterFailureTests.swift b/WorkflowTesting/Tests/WorkflowRenderTesterFailureTests.swift index 156d3e5ee..633c2527f 100644 --- a/WorkflowTesting/Tests/WorkflowRenderTesterFailureTests.swift +++ b/WorkflowTesting/Tests/WorkflowRenderTesterFailureTests.swift @@ -227,7 +227,16 @@ final class WorkflowRenderTesterFailureTests: XCTestCase { rendering.doNoopAction(10) } - expectingFailure(#"("noop(10)") is not equal to ("noop(70)")"#) { + expectingFailure( + """ + XCTAssertNoDifference failed: … + + − TestAction.noop(10) + + TestAction.noop(70) + + (First: −, Second: +) + """ + ) { result.assert(action: TestAction.noop(70)) } @@ -272,7 +281,16 @@ final class WorkflowRenderTesterFailureTests: XCTestCase { } } - expectingFailure(#"("sendOutput("second")") is not equal to ("noop(0)")"#) { + expectingFailure( + """ + XCTAssertNoDifference failed: … + + − TestAction.sendOutput("second") + + TestAction.noop(0) + + (First: −, Second: +) + """ + ) { result.assert(action: TestAction.noop(0)) } @@ -296,7 +314,16 @@ final class WorkflowRenderTesterFailureTests: XCTestCase { rendering.doOutput("hello") } - expectingFailure(#"("string("hello")") is not equal to ("string("nope")")"#) { + expectingFailure( + """ + XCTAssertNoDifference failed: … + + − TestWorkflow.Output.string("hello") + + TestWorkflow.Output.string("nope") + + (First: −, Second: +) + """ + ) { result.assert(output: .string("nope")) }