Skip to content

Commit b9adfa1

Browse files
mbrandonwawaltzforvenus
authored andcommitted
Fixed .failed record mode in inline snapshots. (pointfreeco#874)
* Fixed .failed record mode in inline snapshots. * wip * wip * add some test coverage * wip
1 parent 7984636 commit b9adfa1

File tree

2 files changed

+97
-23
lines changed

2 files changed

+97
-23
lines changed

Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift

+32-22
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,25 @@ import Foundation
100100
}
101101
}
102102
let expected = expected?()
103-
guard
104-
record != .all,
105-
record != .missing || expected != nil
106-
else {
103+
func recordSnapshot() {
107104
// NB: Write snapshot state before calling `XCTFail` in case `continueAfterFailure = false`
108105
inlineSnapshotState[File(path: filePath), default: []].append(
109106
InlineSnapshot(
110107
expected: expected,
111108
actual: actual,
112-
wasRecording: record == .all,
109+
wasRecording: record == .all || record == .failed,
113110
syntaxDescriptor: syntaxDescriptor,
114111
function: "\(function)",
115112
line: line,
116113
column: column
117114
)
118115
)
116+
}
117+
guard
118+
record != .all,
119+
(record != .missing && record != .failed) || expected != nil
120+
else {
121+
recordSnapshot()
119122

120123
var failure: String
121124
if syntaxDescriptor.trailingClosureLabel
@@ -162,12 +165,19 @@ import Foundation
162165
else { return }
163166

164167
let message = message()
165-
syntaxDescriptor.fail(
166-
"""
168+
var failureMessage = """
167169
\(message.isEmpty ? "Snapshot did not match. Difference: …" : message)
168-
170+
169171
\(difference.indenting(by: 2))
170-
""",
172+
"""
173+
174+
if record == .failed {
175+
recordSnapshot()
176+
failureMessage += "\n\nA new snapshot was automatically recorded."
177+
}
178+
179+
syntaxDescriptor.fail(
180+
failureMessage,
171181
fileID: fileID,
172182
file: filePath,
173183
line: line,
@@ -352,27 +362,27 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
352362
}
353363
}
354364

355-
private struct File: Hashable {
356-
let path: StaticString
357-
static func == (lhs: Self, rhs: Self) -> Bool {
365+
@_spi(Internals) public struct File: Hashable {
366+
public let path: StaticString
367+
public static func == (lhs: Self, rhs: Self) -> Bool {
358368
"\(lhs.path)" == "\(rhs.path)"
359369
}
360-
func hash(into hasher: inout Hasher) {
370+
public func hash(into hasher: inout Hasher) {
361371
hasher.combine("\(self.path)")
362372
}
363373
}
364374

365-
private struct InlineSnapshot: Hashable {
366-
var expected: String?
367-
var actual: String?
368-
var wasRecording: Bool
369-
var syntaxDescriptor: InlineSnapshotSyntaxDescriptor
370-
var function: String
371-
var line: UInt
372-
var column: UInt
375+
@_spi(Internals) public struct InlineSnapshot: Hashable {
376+
public var expected: String?
377+
public var actual: String?
378+
public var wasRecording: Bool
379+
public var syntaxDescriptor: InlineSnapshotSyntaxDescriptor
380+
public var function: String
381+
public var line: UInt
382+
public var column: UInt
373383
}
374384

375-
private var inlineSnapshotState: [File: [InlineSnapshot]] = [:]
385+
@_spi(Internals) public var inlineSnapshotState: [File: [InlineSnapshot]] = [:]
376386

377387
private struct TestSource {
378388
let source: String

Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift

+65-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Foundation
2-
import InlineSnapshotTesting
2+
@_spi(Internals) import InlineSnapshotTesting
33
import SnapshotTesting
44
import XCTest
55

@@ -286,6 +286,70 @@ final class InlineSnapshotTestingTests: XCTestCase {
286286
"""##
287287
}
288288
}
289+
290+
#if canImport(Darwin)
291+
func testRecordFailed_IncorrectExpectation() throws {
292+
let initialInlineSnapshotState = inlineSnapshotState
293+
defer { inlineSnapshotState = initialInlineSnapshotState }
294+
295+
XCTExpectFailure {
296+
withSnapshotTesting(record: .failed) {
297+
assertInlineSnapshot(of: 42, as: .json) {
298+
"""
299+
4
300+
"""
301+
}
302+
}
303+
} issueMatcher: {
304+
$0.compactDescription == """
305+
failed - Snapshot did not match. Difference: …
306+
307+
@@ −1,1 +1,1 @@
308+
−4
309+
+42
310+
311+
A new snapshot was automatically recorded.
312+
"""
313+
}
314+
315+
XCTAssertEqual(inlineSnapshotState.count, 1)
316+
XCTAssertEqual(
317+
String(describing: inlineSnapshotState.keys.first!.path)
318+
.hasSuffix("InlineSnapshotTestingTests.swift"),
319+
true
320+
)
321+
}
322+
#endif
323+
324+
#if canImport(Darwin)
325+
func testRecordFailed_MissingExpectation() throws {
326+
let initialInlineSnapshotState = inlineSnapshotState
327+
defer { inlineSnapshotState = initialInlineSnapshotState }
328+
329+
XCTExpectFailure {
330+
withSnapshotTesting(record: .failed) {
331+
assertInlineSnapshot(of: 42, as: .json)
332+
}
333+
} issueMatcher: {
334+
$0.compactDescription == """
335+
failed - Automatically recorded a new snapshot. Difference: …
336+
337+
@@ −1,1 +1,1 @@
338+
339+
+42
340+
341+
Re-run "testRecordFailed_MissingExpectation()" to assert against the newly-recorded snapshot.
342+
"""
343+
}
344+
345+
XCTAssertEqual(inlineSnapshotState.count, 1)
346+
XCTAssertEqual(
347+
String(describing: inlineSnapshotState.keys.first!.path)
348+
.hasSuffix("InlineSnapshotTestingTests.swift"),
349+
true
350+
)
351+
}
352+
#endif
289353
}
290354

291355
private func assertCustomInlineSnapshot(

0 commit comments

Comments
 (0)