From d351e6c786256ac2bf4720495af2903417266778 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Thu, 18 Jul 2024 14:18:17 -0500 Subject: [PATCH 1/5] Fixed .failed record mode in inline snapshots. --- .../AssertInlineSnapshot.swift | 28 +++++++++++++------ .../InlineSnapshotTestingTests.swift | 12 ++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift b/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift index 9249b27fe..affebe594 100644 --- a/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift +++ b/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift @@ -100,22 +100,25 @@ import Foundation } } let expected = expected?() - guard - record != .all, - record != .missing || expected != nil - else { + func recordSnapshot() { // NB: Write snapshot state before calling `XCTFail` in case `continueAfterFailure = false` inlineSnapshotState[File(path: filePath), default: []].append( InlineSnapshot( expected: expected, actual: actual, - wasRecording: record == .all, + wasRecording: record == .all || record == .failed, // todo syntaxDescriptor: syntaxDescriptor, function: "\(function)", line: line, column: column ) ) + } + guard + record != .all, + (record != .missing && record != .failed) || expected != nil + else { + recordSnapshot() var failure: String if syntaxDescriptor.trailingClosureLabel @@ -162,12 +165,19 @@ import Foundation else { return } let message = message() - syntaxDescriptor.fail( - """ + var failureMessage = """ \(message.isEmpty ? "Snapshot did not match. Difference: …" : message) - + \(difference.indenting(by: 2)) - """, + """ + + if record == .failed { + recordSnapshot() + failureMessage += "\n\nA new snapshot was automatically recorded." + } + + syntaxDescriptor.fail( + failureMessage, fileID: fileID, file: filePath, line: line, diff --git a/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift b/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift index 42198acbf..990e0b022 100644 --- a/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift +++ b/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift @@ -286,6 +286,18 @@ final class InlineSnapshotTestingTests: XCTestCase { """## } } + + + func testRecordFailed_Real() throws { + withSnapshotTesting(record: .failed) { + assertInlineSnapshot(of: 42, as: .json) { + """ + 42 + """ + } + } + } + } private func assertCustomInlineSnapshot( From 27d8b7cfa94b3943510a8a14ec0ffdbe6fd85995 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Thu, 18 Jul 2024 14:20:32 -0500 Subject: [PATCH 2/5] wip --- .../InlineSnapshotTestingTests.swift | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift b/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift index 990e0b022..42198acbf 100644 --- a/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift +++ b/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift @@ -286,18 +286,6 @@ final class InlineSnapshotTestingTests: XCTestCase { """## } } - - - func testRecordFailed_Real() throws { - withSnapshotTesting(record: .failed) { - assertInlineSnapshot(of: 42, as: .json) { - """ - 42 - """ - } - } - } - } private func assertCustomInlineSnapshot( From fd35e3b98dc464c323a35d2c0e7052af804423c8 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Thu, 18 Jul 2024 14:21:06 -0500 Subject: [PATCH 3/5] wip --- Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift b/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift index affebe594..71dda0e3a 100644 --- a/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift +++ b/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift @@ -106,7 +106,7 @@ import Foundation InlineSnapshot( expected: expected, actual: actual, - wasRecording: record == .all || record == .failed, // todo + wasRecording: record == .all || record == .failed, syntaxDescriptor: syntaxDescriptor, function: "\(function)", line: line, From 7334a8ea3df12c5dc63d5064e18366a2bea492a4 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Thu, 18 Jul 2024 14:39:32 -0500 Subject: [PATCH 4/5] add some test coverage --- .../AssertInlineSnapshot.swift | 26 ++++---- .../InlineSnapshotTestingTests.swift | 63 ++++++++++++++++++- 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift b/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift index 71dda0e3a..6a3f7996b 100644 --- a/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift +++ b/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift @@ -362,27 +362,27 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable { } } - private struct File: Hashable { - let path: StaticString - static func == (lhs: Self, rhs: Self) -> Bool { +@_spi(Internals) public struct File: Hashable { + public let path: StaticString + public static func == (lhs: Self, rhs: Self) -> Bool { "\(lhs.path)" == "\(rhs.path)" } - func hash(into hasher: inout Hasher) { + public func hash(into hasher: inout Hasher) { hasher.combine("\(self.path)") } } - private struct InlineSnapshot: Hashable { - var expected: String? - var actual: String? - var wasRecording: Bool - var syntaxDescriptor: InlineSnapshotSyntaxDescriptor - var function: String - var line: UInt - var column: UInt +@_spi(Internals) public struct InlineSnapshot: Hashable { + public var expected: String? + public var actual: String? + public var wasRecording: Bool + public var syntaxDescriptor: InlineSnapshotSyntaxDescriptor + public var function: String + public var line: UInt + public var column: UInt } - private var inlineSnapshotState: [File: [InlineSnapshot]] = [:] + @_spi(Internals) public var inlineSnapshotState: [File: [InlineSnapshot]] = [:] private struct TestSource { let source: String diff --git a/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift b/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift index 42198acbf..2610833e6 100644 --- a/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift +++ b/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift @@ -1,5 +1,5 @@ import Foundation -import InlineSnapshotTesting +@_spi(Internals) import InlineSnapshotTesting import SnapshotTesting import XCTest @@ -286,6 +286,67 @@ final class InlineSnapshotTestingTests: XCTestCase { """## } } + + + func testRecordFailed_IncorrectExpectation() throws { + let initialInlineSnapshotState = inlineSnapshotState + defer { inlineSnapshotState = initialInlineSnapshotState } + + XCTExpectFailure { + withSnapshotTesting(record: .failed) { + assertInlineSnapshot(of: 42, as: .json) { + """ + 4 + """ + } + } + } issueMatcher: { + $0.compactDescription == """ + failed - Snapshot did not match. Difference: … + + @@ −1,1 +1,1 @@ + −4 + +42 + + A new snapshot was automatically recorded. + """ + } + + XCTAssertEqual(inlineSnapshotState.count, 1) + XCTAssertEqual( + String(describing: inlineSnapshotState.keys.first!.path) + .hasSuffix("InlineSnapshotTestingTests.swift"), + true + ) + } + + func testRecordFailed_MissingExpectation() throws { + let initialInlineSnapshotState = inlineSnapshotState + defer { inlineSnapshotState = initialInlineSnapshotState } + + XCTExpectFailure { + withSnapshotTesting(record: .failed) { + assertInlineSnapshot(of: 42, as: .json) + } + } issueMatcher: { + $0.compactDescription == """ + failed - Automatically recorded a new snapshot. Difference: … + + @@ −1,1 +1,1 @@ + − + +42 + + Re-run "testRecordFailed_MissingExpectation()" to assert against the newly-recorded snapshot. + """ + } + + XCTAssertEqual(inlineSnapshotState.count, 1) + XCTAssertEqual( + String(describing: inlineSnapshotState.keys.first!.path) + .hasSuffix("InlineSnapshotTestingTests.swift"), + true + ) + } } private func assertCustomInlineSnapshot( From 369fa82bb4347c04df32ac96a17fa9a59cec6c81 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Thu, 18 Jul 2024 14:42:36 -0500 Subject: [PATCH 5/5] wip --- .../InlineSnapshotTestingTests.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift b/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift index 2610833e6..40b2154cf 100644 --- a/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift +++ b/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift @@ -287,7 +287,7 @@ final class InlineSnapshotTestingTests: XCTestCase { } } - +#if canImport(Darwin) func testRecordFailed_IncorrectExpectation() throws { let initialInlineSnapshotState = inlineSnapshotState defer { inlineSnapshotState = initialInlineSnapshotState } @@ -319,7 +319,9 @@ final class InlineSnapshotTestingTests: XCTestCase { true ) } + #endif +#if canImport(Darwin) func testRecordFailed_MissingExpectation() throws { let initialInlineSnapshotState = inlineSnapshotState defer { inlineSnapshotState = initialInlineSnapshotState } @@ -347,6 +349,7 @@ final class InlineSnapshotTestingTests: XCTestCase { true ) } + #endif } private func assertCustomInlineSnapshot(