Skip to content

Commit

Permalink
Support inline snapshot API migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Sep 20, 2023
1 parent d1082c4 commit e7fe33e
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
/// The default label describing an inline snapshot.
public static let defaultTrailingClosureLabel = "matches"

/// A list of trailing closure labels from deprecated interfaces.
///
/// Useful for providing migration paths for custom snapshot functions.
public var deprecatedTrailingClosureLabels: [String]

/// The label of the trailing closure that returns the inline snapshot.
public var trailingClosureLabel: String

Expand Down Expand Up @@ -172,9 +177,11 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
/// - trailingClosureOffset: The offset of the trailing closure that returns the inline
/// snapshot, relative to the first trailing closure.
public init(
deprecatedTrailingClosureLabels: [String] = [],
trailingClosureLabel: String = Self.defaultTrailingClosureLabel,
trailingClosureOffset: Int = 0
) {
self.deprecatedTrailingClosureLabels = deprecatedTrailingClosureLabels
self.trailingClosureLabel = trailingClosureLabel
self.trailingClosureOffset = trailingClosureOffset
}
Expand Down Expand Up @@ -216,6 +223,10 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
line: trailingClosureLine.map(UInt.init) ?? line
)
}

fileprivate func contains(_ label: String) -> Bool {
self.trailingClosureLabel == label || self.deprecatedTrailingClosureLabels.contains(label)
}
}

// MARK: - Private
Expand Down Expand Up @@ -377,6 +388,9 @@ private final class SnapshotRewriter: SyntaxRewriter {
repeating: "#", count: snapshot.actual.hashCount(isMultiline: true)
)
let leadingIndent = leadingTrivia + self.indent
let snapshotLabel = TokenSyntax(
stringLiteral: snapshot.syntaxDescriptor.trailingClosureLabel
)
let snapshotClosure = ClosureExprSyntax(
leftBrace: .leftBraceToken(trailingTrivia: .newline),
statements: CodeBlockItemListSyntax {
Expand Down Expand Up @@ -425,6 +439,7 @@ private final class SnapshotRewriter: SyntaxRewriter {
switch centeredTrailingClosureOffset {
case ..<0:
let index = arguments.index(arguments.startIndex, offsetBy: trailingClosureOffset)
functionCallExpr.arguments[index].label = snapshotLabel
functionCallExpr.arguments[index].expression = ExprSyntax(snapshotClosure)

case 0:
Expand All @@ -438,7 +453,7 @@ private final class SnapshotRewriter: SyntaxRewriter {
case 1...:
var newElement: MultipleTrailingClosureElementSyntax {
MultipleTrailingClosureElementSyntax(
label: TokenSyntax(stringLiteral: snapshot.syntaxDescriptor.trailingClosureLabel),
label: snapshotLabel,
closure: snapshotClosure.with(\.leadingTrivia, snapshotClosure.leadingTrivia + .space)
)
}
Expand All @@ -455,10 +470,11 @@ private final class SnapshotRewriter: SyntaxRewriter {
limitedBy: endIndex
)
{
if functionCallExpr.additionalTrailingClosures[index].label.text
== snapshot.syntaxDescriptor.trailingClosureLabel
{
if snapshot.syntaxDescriptor.contains(
functionCallExpr.additionalTrailingClosures[index].label.text
) {
if snapshot.wasRecording {
functionCallExpr.additionalTrailingClosures[index].label = snapshotLabel
functionCallExpr.additionalTrailingClosures[index].closure = snapshotClosure
}
} else {
Expand Down

0 comments on commit e7fe33e

Please sign in to comment.