From b1750dfd5c570fa02d701b5ae597dfea29b7c3ef Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Wed, 3 Jul 2024 20:41:07 -0400 Subject: [PATCH 01/10] Beta support for Swift Testing. --- Package.resolved | 4 +- Package.swift | 2 +- Sources/MacroTesting/AssertMacro.swift | 478 +++++++++--------- .../MacroTesting/Internal/Deprecations.swift | 4 +- .../MacroTesting/Internal/RecordIssue.swift | 26 + 5 files changed, 269 insertions(+), 245 deletions(-) create mode 100644 Sources/MacroTesting/Internal/RecordIssue.swift diff --git a/Package.resolved b/Package.resolved index 85b7712..85db4e7 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/pointfreeco/swift-snapshot-testing", "state" : { - "revision" : "8ddd519780452729c6634ad6bd0d2595938e9ea3", - "version" : "1.16.1" + "branch" : "swift-testing", + "revision" : "3e37ab028ccb7c6fad089b09b85af50c8e77e00a" } }, { diff --git a/Package.swift b/Package.swift index 7b3e53b..659e77b 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/apple/swift-syntax", "509.0.0"..<"601.0.0-prerelease"), - .package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.16.0"), + .package(url: "https://github.com/pointfreeco/swift-snapshot-testing", branch: "swift-testing"), ], targets: [ .target( diff --git a/Sources/MacroTesting/AssertMacro.swift b/Sources/MacroTesting/AssertMacro.swift index c6fae47..a9ca8f1 100644 --- a/Sources/MacroTesting/AssertMacro.swift +++ b/Sources/MacroTesting/AssertMacro.swift @@ -1,4 +1,5 @@ import InlineSnapshotTesting +@_spi(Internals) import SnapshotTesting import SwiftDiagnostics import SwiftOperators import SwiftParser @@ -112,7 +113,7 @@ import XCTest public func assertMacro( _ macros: [String: Macro.Type]? = nil, indentationWidth: Trivia? = nil, - record isRecording: Bool? = nil, + record: SnapshotTestingConfiguration.Record? = nil, of originalSource: () throws -> String, diagnostics diagnosedSource: (() -> String)? = nil, fixes fixedSource: (() -> String)? = nil, @@ -122,269 +123,266 @@ public func assertMacro( line: UInt = #line, column: UInt = #column ) { - let macros = macros ?? MacroTestingConfiguration.current.macros - guard !macros.isEmpty else { - XCTFail( - """ - No macros configured for this assertion. Pass a mapping to this function, e.g.: - - assertMacro(["stringify": StringifyMacro.self]) { … } - - Or wrap your assertion using 'withMacroTesting', e.g. in 'invokeTest': - - class StringifyMacroTests: XCTestCase { - override func invokeTest() { - withMacroTesting(macros: ["stringify": StringifyMacro.self]) { - super.invokeTest() + withSnapshotTesting(record: record) { + let macros = macros ?? MacroTestingConfiguration.current.macros + guard !macros.isEmpty else { + recordIssue( + """ + No macros configured for this assertion. Pass a mapping to this function, e.g.: + + assertMacro(["stringify": StringifyMacro.self]) { … } + + Or wrap your assertion using 'withMacroTesting', e.g. in 'invokeTest': + + class StringifyMacroTests: XCTestCase { + override func invokeTest() { + withMacroTesting(macros: ["stringify": StringifyMacro.self]) { + super.invokeTest() + } } + … } - … - } - """, - file: file, - line: line - ) - return - } - - let wasRecording = SnapshotTesting.isRecording - SnapshotTesting.isRecording = isRecording ?? MacroTestingConfiguration.current.isRecording - defer { SnapshotTesting.isRecording = wasRecording } - - do { - var origSourceFile = Parser.parse(source: try originalSource()) - if let foldedSourceFile = try OperatorTable.standardOperators.foldAll(origSourceFile).as( - SourceFileSyntax.self - ) { - origSourceFile = foldedSourceFile - } - - let origDiagnostics = ParseDiagnosticsGenerator.diagnostics(for: origSourceFile) - let indentationWidth = - indentationWidth - ?? MacroTestingConfiguration.current.indentationWidth - ?? Trivia( - stringLiteral: String( - SourceLocationConverter(fileName: "-", tree: origSourceFile).sourceLines - .first(where: { $0.first?.isWhitespace == true && $0 != "\n" })? - .prefix(while: { $0.isWhitespace }) - ?? " " - ) - ) - - var context = BasicMacroExpansionContext( - sourceFiles: [ - origSourceFile: .init(moduleName: "TestModule", fullFilePath: "Test.swift") - ] - ) - #if canImport(SwiftSyntax600) - var expandedSourceFile = origSourceFile.expand( - macros: macros, - contextGenerator: { _ in context }, - indentationWidth: indentationWidth - ) - #else - var expandedSourceFile = origSourceFile.expand( - macros: macros, - in: context, - indentationWidth: indentationWidth - ) - #endif - - var offset = 0 - - func anchor(_ diag: Diagnostic) -> Diagnostic { - let location = context.location(for: diag.position, anchoredAt: diag.node, fileName: "") - return Diagnostic( - node: diag.node, - position: AbsolutePosition(utf8Offset: location.offset), - message: diag.diagMessage, - highlights: diag.highlights, - notes: diag.notes, - fixIts: diag.fixIts - ) - } - - var allDiagnostics: [Diagnostic] { origDiagnostics + context.diagnostics } - if !allDiagnostics.isEmpty || diagnosedSource != nil { - offset += 1 - - let converter = SourceLocationConverter(fileName: "-", tree: origSourceFile) - let lineCount = converter.location(for: origSourceFile.endPosition).line - let diagnostics = - DiagnosticsFormatter - .annotatedSource( - tree: origSourceFile, - diags: allDiagnostics.map(anchor), - context: context, - contextSize: lineCount - ) - .description - .replacingOccurrences(of: #"(^|\n) *\d* +│ "#, with: "$1", options: .regularExpression) - .trimmingCharacters(in: .newlines) - - assertInlineSnapshot( - of: diagnostics, - as: ._lines, - message: """ - Diagnostic output (\(newPrefix)) differed from expected output (\(oldPrefix)). \ - Difference: … - """, - syntaxDescriptor: InlineSnapshotSyntaxDescriptor( - deprecatedTrailingClosureLabels: ["matches"], - trailingClosureLabel: "diagnostics", - trailingClosureOffset: offset - ), - matches: diagnosedSource, - file: file, - function: function, - line: line, - column: column - ) - } else if diagnosedSource != nil { - offset += 1 - assertInlineSnapshot( - of: nil, - as: ._lines, - message: """ - Diagnostic output (\(newPrefix)) differed from expected output (\(oldPrefix)). \ - Difference: … - """, - syntaxDescriptor: InlineSnapshotSyntaxDescriptor( - deprecatedTrailingClosureLabels: ["matches"], - trailingClosureLabel: "diagnostics", - trailingClosureOffset: offset - ), - matches: diagnosedSource, + """, file: file, - function: function, - line: line, - column: column + line: line ) + return } - - if !allDiagnostics.isEmpty && allDiagnostics.allSatisfy({ !$0.fixIts.isEmpty }) { - offset += 1 - - let edits = - context.diagnostics - .flatMap(\.fixIts) - .flatMap { $0.changes } - .map { $0.edit(in: context) } - - var fixedSourceFile = origSourceFile - fixedSourceFile = Parser.parse( - source: FixItApplier.apply( - edits: edits, to: origSourceFile - ) - .description - ) - if let foldedSourceFile = try OperatorTable.standardOperators.foldAll(fixedSourceFile).as( + do { + var origSourceFile = Parser.parse(source: try originalSource()) + if let foldedSourceFile = try OperatorTable.standardOperators.foldAll(origSourceFile).as( SourceFileSyntax.self ) { - fixedSourceFile = foldedSourceFile + origSourceFile = foldedSourceFile } - assertInlineSnapshot( - of: fixedSourceFile.description.trimmingCharacters(in: .newlines), - as: ._lines, - message: """ - Fixed output (\(newPrefix)) differed from expected output (\(oldPrefix)). \ - Difference: … - """, - syntaxDescriptor: InlineSnapshotSyntaxDescriptor( - trailingClosureLabel: "fixes", - trailingClosureOffset: offset - ), - matches: fixedSource, - file: file, - function: function, - line: line, - column: column - ) + let origDiagnostics = ParseDiagnosticsGenerator.diagnostics(for: origSourceFile) + let indentationWidth = + indentationWidth + ?? MacroTestingConfiguration.current.indentationWidth + ?? Trivia( + stringLiteral: String( + SourceLocationConverter(fileName: "-", tree: origSourceFile).sourceLines + .first(where: { $0.first?.isWhitespace == true && $0 != "\n" })? + .prefix(while: { $0.isWhitespace }) + ?? " " + ) + ) - context = BasicMacroExpansionContext( + var context = BasicMacroExpansionContext( sourceFiles: [ - fixedSourceFile: .init(moduleName: "TestModule", fullFilePath: "Test.swift") + origSourceFile: .init(moduleName: "TestModule", fullFilePath: "Test.swift") ] ) #if canImport(SwiftSyntax600) - expandedSourceFile = fixedSourceFile.expand( + var expandedSourceFile = origSourceFile.expand( macros: macros, contextGenerator: { _ in context }, indentationWidth: indentationWidth ) #else - expandedSourceFile = fixedSourceFile.expand( + var expandedSourceFile = origSourceFile.expand( macros: macros, in: context, indentationWidth: indentationWidth ) #endif - } else if fixedSource != nil { - offset += 1 - assertInlineSnapshot( - of: nil, - as: ._lines, - message: """ - Fixed output (\(newPrefix)) differed from expected output (\(oldPrefix)). \ - Difference: … - """, - syntaxDescriptor: InlineSnapshotSyntaxDescriptor( - trailingClosureLabel: "fixes", - trailingClosureOffset: offset - ), - matches: fixedSource, - file: file, - function: function, - line: line, - column: column - ) - } - if allDiagnostics.filter({ $0.diagMessage.severity == .error }).isEmpty { - offset += 1 - assertInlineSnapshot( - of: expandedSourceFile.description.trimmingCharacters(in: .newlines), - as: ._lines, - message: """ - Expanded output (\(newPrefix)) differed from expected output (\(oldPrefix)). \ - Difference: … - """, - syntaxDescriptor: InlineSnapshotSyntaxDescriptor( - deprecatedTrailingClosureLabels: ["matches"], - trailingClosureLabel: "expansion", - trailingClosureOffset: offset - ), - matches: expandedSource, - file: file, - function: function, - line: line, - column: column - ) - } else if expandedSource != nil { - offset += 1 - assertInlineSnapshot( - of: nil, - as: ._lines, - message: """ - Expanded output (\(newPrefix)) differed from expected output (\(oldPrefix)). \ - Difference: … - """, - syntaxDescriptor: InlineSnapshotSyntaxDescriptor( - deprecatedTrailingClosureLabels: ["matches"], - trailingClosureLabel: "expansion", - trailingClosureOffset: offset - ), - matches: expandedSource, - file: file, - function: function, - line: line, - column: column - ) + var offset = 0 + + func anchor(_ diag: Diagnostic) -> Diagnostic { + let location = context.location(for: diag.position, anchoredAt: diag.node, fileName: "") + return Diagnostic( + node: diag.node, + position: AbsolutePosition(utf8Offset: location.offset), + message: diag.diagMessage, + highlights: diag.highlights, + notes: diag.notes, + fixIts: diag.fixIts + ) + } + + var allDiagnostics: [Diagnostic] { origDiagnostics + context.diagnostics } + if !allDiagnostics.isEmpty || diagnosedSource != nil { + offset += 1 + + let converter = SourceLocationConverter(fileName: "-", tree: origSourceFile) + let lineCount = converter.location(for: origSourceFile.endPosition).line + let diagnostics = + DiagnosticsFormatter + .annotatedSource( + tree: origSourceFile, + diags: allDiagnostics.map(anchor), + context: context, + contextSize: lineCount + ) + .description + .replacingOccurrences(of: #"(^|\n) *\d* +│ "#, with: "$1", options: .regularExpression) + .trimmingCharacters(in: .newlines) + + assertInlineSnapshot( + of: diagnostics, + as: ._lines, + message: """ + Diagnostic output (\(newPrefix)) differed from expected output (\(oldPrefix)). \ + Difference: … + """, + syntaxDescriptor: InlineSnapshotSyntaxDescriptor( + deprecatedTrailingClosureLabels: ["matches"], + trailingClosureLabel: "diagnostics", + trailingClosureOffset: offset + ), + matches: diagnosedSource, + file: file, + function: function, + line: line, + column: column + ) + } else if diagnosedSource != nil { + offset += 1 + assertInlineSnapshot( + of: nil, + as: ._lines, + message: """ + Diagnostic output (\(newPrefix)) differed from expected output (\(oldPrefix)). \ + Difference: … + """, + syntaxDescriptor: InlineSnapshotSyntaxDescriptor( + deprecatedTrailingClosureLabels: ["matches"], + trailingClosureLabel: "diagnostics", + trailingClosureOffset: offset + ), + matches: diagnosedSource, + file: file, + function: function, + line: line, + column: column + ) + } + + if !allDiagnostics.isEmpty && allDiagnostics.allSatisfy({ !$0.fixIts.isEmpty }) { + offset += 1 + + let edits = + context.diagnostics + .flatMap(\.fixIts) + .flatMap { $0.changes } + .map { $0.edit(in: context) } + + var fixedSourceFile = origSourceFile + fixedSourceFile = Parser.parse( + source: FixItApplier.apply( + edits: edits, to: origSourceFile + ) + .description + ) + if let foldedSourceFile = try OperatorTable.standardOperators.foldAll(fixedSourceFile).as( + SourceFileSyntax.self + ) { + fixedSourceFile = foldedSourceFile + } + + assertInlineSnapshot( + of: fixedSourceFile.description.trimmingCharacters(in: .newlines), + as: ._lines, + message: """ + Fixed output (\(newPrefix)) differed from expected output (\(oldPrefix)). \ + Difference: … + """, + syntaxDescriptor: InlineSnapshotSyntaxDescriptor( + trailingClosureLabel: "fixes", + trailingClosureOffset: offset + ), + matches: fixedSource, + file: file, + function: function, + line: line, + column: column + ) + + context = BasicMacroExpansionContext( + sourceFiles: [ + fixedSourceFile: .init(moduleName: "TestModule", fullFilePath: "Test.swift") + ] + ) + #if canImport(SwiftSyntax600) + expandedSourceFile = fixedSourceFile.expand( + macros: macros, + contextGenerator: { _ in context }, + indentationWidth: indentationWidth + ) + #else + expandedSourceFile = fixedSourceFile.expand( + macros: macros, + in: context, + indentationWidth: indentationWidth + ) + #endif + } else if fixedSource != nil { + offset += 1 + assertInlineSnapshot( + of: nil, + as: ._lines, + message: """ + Fixed output (\(newPrefix)) differed from expected output (\(oldPrefix)). \ + Difference: … + """, + syntaxDescriptor: InlineSnapshotSyntaxDescriptor( + trailingClosureLabel: "fixes", + trailingClosureOffset: offset + ), + matches: fixedSource, + file: file, + function: function, + line: line, + column: column + ) + } + + if allDiagnostics.filter({ $0.diagMessage.severity == .error }).isEmpty { + offset += 1 + assertInlineSnapshot( + of: expandedSourceFile.description.trimmingCharacters(in: .newlines), + as: ._lines, + message: """ + Expanded output (\(newPrefix)) differed from expected output (\(oldPrefix)). \ + Difference: … + """, + syntaxDescriptor: InlineSnapshotSyntaxDescriptor( + deprecatedTrailingClosureLabels: ["matches"], + trailingClosureLabel: "expansion", + trailingClosureOffset: offset + ), + matches: expandedSource, + file: file, + function: function, + line: line, + column: column + ) + } else if expandedSource != nil { + offset += 1 + assertInlineSnapshot( + of: nil, + as: ._lines, + message: """ + Expanded output (\(newPrefix)) differed from expected output (\(oldPrefix)). \ + Difference: … + """, + syntaxDescriptor: InlineSnapshotSyntaxDescriptor( + deprecatedTrailingClosureLabels: ["matches"], + trailingClosureLabel: "expansion", + trailingClosureOffset: offset + ), + matches: expandedSource, + file: file, + function: function, + line: line, + column: column + ) + } + } catch { + recordIssue("Threw error: \(error)", file: file, line: line) } - } catch { - XCTFail("Threw error: \(error)", file: file, line: line) } } @@ -467,7 +465,7 @@ extension BasicMacroExpansionContext { public func assertMacro( _ macros: [Macro.Type], indentationWidth: Trivia? = nil, - record isRecording: Bool? = nil, + record: SnapshotTestingConfiguration.Record? = nil, of originalSource: () throws -> String, diagnostics diagnosedSource: (() -> String)? = nil, fixes fixedSource: (() -> String)? = nil, @@ -480,7 +478,7 @@ public func assertMacro( assertMacro( Dictionary(macros: macros), indentationWidth: indentationWidth, - record: isRecording, + record: record, of: originalSource, diagnostics: diagnosedSource, fixes: fixedSource, diff --git a/Sources/MacroTesting/Internal/Deprecations.swift b/Sources/MacroTesting/Internal/Deprecations.swift index 8563aa1..b9e52f9 100644 --- a/Sources/MacroTesting/Internal/Deprecations.swift +++ b/Sources/MacroTesting/Internal/Deprecations.swift @@ -22,7 +22,7 @@ public func assertMacro( column: UInt = #column ) { guard isRecording ?? MacroTestingConfiguration.current.isRecording else { - XCTFail("Re-record this assertion", file: file, line: line) + recordIssue("Re-record this assertion", file: file, line: line) return } assertMacro( @@ -73,7 +73,7 @@ public func assertMacro( line: UInt = #line, column: UInt = #column ) { - XCTFail("Delete 'matches' and re-record this assertion", file: file, line: line) + recordIssue("Delete 'matches' and re-record this assertion", file: file, line: line) } @available( diff --git a/Sources/MacroTesting/Internal/RecordIssue.swift b/Sources/MacroTesting/Internal/RecordIssue.swift new file mode 100644 index 0000000..f63669c --- /dev/null +++ b/Sources/MacroTesting/Internal/RecordIssue.swift @@ -0,0 +1,26 @@ +import XCTest + +#if canImport(Testing) + import Testing +#endif + +@_spi(Internals) +public func recordIssue( + _ message: @autoclosure () -> String, + file: StaticString = #filePath, + line: UInt = #line +) { + #if canImport(Testing) + if Test.current != nil { + Issue.record( + Comment(rawValue: message()), + filePath: file.description, + line: Int(line) + ) + } else { + XCTFail(message(), file: file, line: line) + } + #else + XCTFail(message(), file: filePath, line: line) + #endif +} From dde6aeff5d49c9aa62637269a1c8fd88ba575ba1 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Wed, 3 Jul 2024 20:59:13 -0400 Subject: [PATCH 02/10] wip --- Package.resolved | 6 +- Sources/MacroTesting/AssertMacro.swift | 26 +++--- .../MacroTesting/Internal/Deprecations.swift | 87 ++++++++++++++++++- Tests/MacroTestingTests/BaseTestCase.swift | 12 +-- 4 files changed, 109 insertions(+), 22 deletions(-) diff --git a/Package.resolved b/Package.resolved index 85db4e7..5e85967 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,7 +6,7 @@ "location" : "https://github.com/pointfreeco/swift-snapshot-testing", "state" : { "branch" : "swift-testing", - "revision" : "3e37ab028ccb7c6fad089b09b85af50c8e77e00a" + "revision" : "b479fa466e0d53febf049437b747a3fcb4dec216" } }, { @@ -14,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-syntax", "state" : { - "revision" : "303e5c5c36d6a558407d364878df131c3546fad8", - "version" : "510.0.2" + "revision" : "4c6cc0a3b9e8f14b3ae2307c5ccae4de6167ac2c", + "version" : "600.0.0-prerelease-2024-06-12" } } ], diff --git a/Sources/MacroTesting/AssertMacro.swift b/Sources/MacroTesting/AssertMacro.swift index a9ca8f1..77526ea 100644 --- a/Sources/MacroTesting/AssertMacro.swift +++ b/Sources/MacroTesting/AssertMacro.swift @@ -530,14 +530,14 @@ public func assertMacro( /// - operation: The operation to run with the configuration updated. public func withMacroTesting( indentationWidth: Trivia? = nil, - isRecording: Bool? = nil, + record: SnapshotTestingConfiguration.Record? = nil, macros: [String: Macro.Type]? = nil, operation: () async throws -> R ) async rethrows { var configuration = MacroTestingConfiguration.current - if let indentationWidth = indentationWidth { configuration.indentationWidth = indentationWidth } - if let isRecording = isRecording { configuration.isRecording = isRecording } - if let macros = macros { configuration.macros = macros } + if let indentationWidth { configuration.indentationWidth = indentationWidth } + if let record { configuration.record = record } + if let macros { configuration.macros = macros } try await MacroTestingConfiguration.$current.withValue(configuration) { try await operation() } @@ -557,14 +557,14 @@ public func withMacroTesting( /// - operation: The operation to run with the configuration updated. public func withMacroTesting( indentationWidth: Trivia? = nil, - isRecording: Bool? = nil, + record: SnapshotTestingConfiguration.Record? = nil, macros: [String: Macro.Type]? = nil, operation: () throws -> R ) rethrows { var configuration = MacroTestingConfiguration.current - if let indentationWidth = indentationWidth { configuration.indentationWidth = indentationWidth } - if let isRecording = isRecording { configuration.isRecording = isRecording } - if let macros = macros { configuration.macros = macros } + if let indentationWidth { configuration.indentationWidth = indentationWidth } + if let record { configuration.record = record } + if let macros { configuration.macros = macros } try MacroTestingConfiguration.$current.withValue(configuration) { try operation() } @@ -584,13 +584,13 @@ public func withMacroTesting( /// - operation: The operation to run with the configuration updated. public func withMacroTesting( indentationWidth: Trivia? = nil, - isRecording: Bool? = nil, + record: SnapshotTestingConfiguration.Record? = nil, macros: [Macro.Type], operation: () async throws -> R ) async rethrows { try await withMacroTesting( indentationWidth: indentationWidth, - isRecording: isRecording, + record: record, macros: Dictionary(macros: macros), operation: operation ) @@ -610,13 +610,13 @@ public func withMacroTesting( /// - operation: The operation to run with the configuration updated. public func withMacroTesting( indentationWidth: Trivia? = nil, - isRecording: Bool? = nil, + record: SnapshotTestingConfiguration.Record? = nil, macros: [Macro.Type], operation: () throws -> R ) rethrows { try withMacroTesting( indentationWidth: indentationWidth, - isRecording: isRecording, + record: record, macros: Dictionary(macros: macros), operation: operation ) @@ -694,7 +694,7 @@ struct MacroTestingConfiguration { @TaskLocal static var current = Self() var indentationWidth: Trivia? = nil - var isRecording = false + var record = SnapshotTestingConfiguration.Record.missing var macros: [String: Macro.Type] = [:] } diff --git a/Sources/MacroTesting/Internal/Deprecations.swift b/Sources/MacroTesting/Internal/Deprecations.swift index b9e52f9..db7619d 100644 --- a/Sources/MacroTesting/Internal/Deprecations.swift +++ b/Sources/MacroTesting/Internal/Deprecations.swift @@ -8,6 +8,91 @@ import SwiftSyntaxMacroExpansion import SwiftSyntaxMacros import XCTest +// MARK: Deprecated after 0.4.2 + +@available(iOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(macOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(tvOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(visionOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(watchOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@_disfavoredOverload +public func withMacroTesting( + indentationWidth: Trivia? = nil, + isRecording: Bool? = nil, + macros: [String: Macro.Type]? = nil, + operation: () async throws -> R +) async rethrows { + var configuration = MacroTestingConfiguration.current + if let indentationWidth { configuration.indentationWidth = indentationWidth } + if let isRecording { configuration.record = isRecording ? .all : .missing } + if let macros { configuration.macros = macros } + try await MacroTestingConfiguration.$current.withValue(configuration) { + try await operation() + } +} + +@available(iOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(macOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(tvOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(visionOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(watchOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@_disfavoredOverload +public func withMacroTesting( + indentationWidth: Trivia? = nil, + isRecording: Bool? = nil, + macros: [String: Macro.Type]? = nil, + operation: () throws -> R +) rethrows { + var configuration = MacroTestingConfiguration.current + if let indentationWidth { configuration.indentationWidth = indentationWidth } + if let isRecording { configuration.record = isRecording ? .all : .missing } + if let macros { configuration.macros = macros } + try MacroTestingConfiguration.$current.withValue(configuration) { + try operation() + } +} + +@available(iOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(macOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(tvOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(visionOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(watchOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@_disfavoredOverload +public func withMacroTesting( + indentationWidth: Trivia? = nil, + isRecording: Bool? = nil, + macros: [Macro.Type], + operation: () async throws -> R +) async rethrows { + try await withMacroTesting( + indentationWidth: indentationWidth, + isRecording: isRecording, + macros: Dictionary(macros: macros), + operation: operation + ) +} + + +@available(iOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(macOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(tvOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(visionOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@available(watchOS, deprecated, renamed: "withMacroTesting(indentationWidth:record:macros:operation:)") +@_disfavoredOverload +public func withMacroTesting( + indentationWidth: Trivia? = nil, + isRecording: Bool? = nil, + macros: [Macro.Type], + operation: () throws -> R +) rethrows { + try withMacroTesting( + indentationWidth: indentationWidth, + isRecording: isRecording, + macros: Dictionary(macros: macros), + operation: operation + ) +} + // MARK: Deprecated after 0.1.0 @available(*, deprecated, message: "Re-record this assertion") @@ -21,7 +106,7 @@ public func assertMacro( line: UInt = #line, column: UInt = #column ) { - guard isRecording ?? MacroTestingConfiguration.current.isRecording else { + guard isRecording ?? (MacroTestingConfiguration.current.record == .all) else { recordIssue("Re-record this assertion", file: file, line: line) return } diff --git a/Tests/MacroTestingTests/BaseTestCase.swift b/Tests/MacroTestingTests/BaseTestCase.swift index cf4a54f..e079126 100644 --- a/Tests/MacroTestingTests/BaseTestCase.swift +++ b/Tests/MacroTestingTests/BaseTestCase.swift @@ -2,9 +2,11 @@ import MacroTesting import XCTest class BaseTestCase: XCTestCase { - // override func invokeTest() { - // withMacroTesting(isRecording: true) { - // super.invokeTest() - // } - // } + override func invokeTest() { + withMacroTesting( + record: .missing + ) { + super.invokeTest() + } + } } From ae330c03a478fb5bd5b00d32688517411d1f56d3 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Mon, 8 Jul 2024 14:05:38 -0400 Subject: [PATCH 03/10] wip --- Sources/MacroTesting/AssertMacro.swift | 9 +-- .../MacroTesting/Internal/Deprecations.swift | 19 ++++--- Sources/MacroTesting/MacrosTestTrait.swift | 51 +++++++++++++++++ .../MacroTestingTests/SwiftTestingTests.swift | 55 +++++++++++++++++++ 4 files changed, 121 insertions(+), 13 deletions(-) create mode 100644 Sources/MacroTesting/MacrosTestTrait.swift create mode 100644 Tests/MacroTestingTests/SwiftTestingTests.swift diff --git a/Sources/MacroTesting/AssertMacro.swift b/Sources/MacroTesting/AssertMacro.swift index 77526ea..8bf8c0c 100644 --- a/Sources/MacroTesting/AssertMacro.swift +++ b/Sources/MacroTesting/AssertMacro.swift @@ -123,9 +123,9 @@ public func assertMacro( line: UInt = #line, column: UInt = #column ) { - withSnapshotTesting(record: record) { + withSnapshotTesting(record: record ?? SnapshotTestingConfiguration.current?.record) { let macros = macros ?? MacroTestingConfiguration.current.macros - guard !macros.isEmpty else { + guard let macros, !macros.isEmpty else { recordIssue( """ No macros configured for this assertion. Pass a mapping to this function, e.g.: @@ -536,7 +536,6 @@ public func withMacroTesting( ) async rethrows { var configuration = MacroTestingConfiguration.current if let indentationWidth { configuration.indentationWidth = indentationWidth } - if let record { configuration.record = record } if let macros { configuration.macros = macros } try await MacroTestingConfiguration.$current.withValue(configuration) { try await operation() @@ -563,7 +562,6 @@ public func withMacroTesting( ) rethrows { var configuration = MacroTestingConfiguration.current if let indentationWidth { configuration.indentationWidth = indentationWidth } - if let record { configuration.record = record } if let macros { configuration.macros = macros } try MacroTestingConfiguration.$current.withValue(configuration) { try operation() @@ -694,8 +692,7 @@ struct MacroTestingConfiguration { @TaskLocal static var current = Self() var indentationWidth: Trivia? = nil - var record = SnapshotTestingConfiguration.Record.missing - var macros: [String: Macro.Type] = [:] + var macros: [String: Macro.Type]? } extension Dictionary where Key == String, Value == Macro.Type { diff --git a/Sources/MacroTesting/Internal/Deprecations.swift b/Sources/MacroTesting/Internal/Deprecations.swift index db7619d..4d5df81 100644 --- a/Sources/MacroTesting/Internal/Deprecations.swift +++ b/Sources/MacroTesting/Internal/Deprecations.swift @@ -1,4 +1,5 @@ import InlineSnapshotTesting +@_spi(Internals) import SnapshotTesting import SwiftDiagnostics import SwiftOperators import SwiftParser @@ -24,10 +25,12 @@ public func withMacroTesting( ) async rethrows { var configuration = MacroTestingConfiguration.current if let indentationWidth { configuration.indentationWidth = indentationWidth } - if let isRecording { configuration.record = isRecording ? .all : .missing } + let record: SnapshotTestingConfiguration.Record? = isRecording.map { $0 ? .all : .missing } if let macros { configuration.macros = macros } - try await MacroTestingConfiguration.$current.withValue(configuration) { - try await operation() + _ = try await withSnapshotTesting(record: record) { + try await MacroTestingConfiguration.$current.withValue(configuration) { + try await operation() + } } } @@ -45,10 +48,12 @@ public func withMacroTesting( ) rethrows { var configuration = MacroTestingConfiguration.current if let indentationWidth { configuration.indentationWidth = indentationWidth } - if let isRecording { configuration.record = isRecording ? .all : .missing } + let record: SnapshotTestingConfiguration.Record? = isRecording.map { $0 ? .all : .missing } if let macros { configuration.macros = macros } - try MacroTestingConfiguration.$current.withValue(configuration) { - try operation() + _ = try withSnapshotTesting(record: record) { + try MacroTestingConfiguration.$current.withValue(configuration) { + try operation() + } } } @@ -106,7 +111,7 @@ public func assertMacro( line: UInt = #line, column: UInt = #column ) { - guard isRecording ?? (MacroTestingConfiguration.current.record == .all) else { + guard isRecording ?? (SnapshotTestingConfiguration.current?.record == .all) else { recordIssue("Re-record this assertion", file: file, line: line) return } diff --git a/Sources/MacroTesting/MacrosTestTrait.swift b/Sources/MacroTesting/MacrosTestTrait.swift new file mode 100644 index 0000000..99e860a --- /dev/null +++ b/Sources/MacroTesting/MacrosTestTrait.swift @@ -0,0 +1,51 @@ +#if canImport(Testing) + import SnapshotTesting + import SwiftSyntax +import SwiftSyntaxMacros + @_spi(Experimental) import Testing + + @_spi(Experimental) + extension Trait where Self == _MacrosTestTrait { + /// Configure snapshot testing in a suite or test. + /// + /// - Parameters: + /// - record: The record mode of the test. + /// - diffTool: The diff tool to use in failure messages. + public static func macros( + indentationWidth: Trivia? = nil, + record: SnapshotTestingConfiguration.Record? = nil, + macros: [String: Macro.Type]? = nil + ) -> Self { + _MacrosTestTrait( + configuration: MacroTestingConfiguration( + indentationWidth: indentationWidth, + macros: macros + ), + record: record + ) + } + } + + /// A type representing the configuration of snapshot testing. + @_spi(Experimental) + public struct _MacrosTestTrait: CustomExecutionTrait, SuiteTrait, TestTrait { + public let isRecursive = true + let configuration: MacroTestingConfiguration + let record: SnapshotTestingConfiguration.Record? + + public func execute( + _ function: @escaping () async throws -> Void, + for test: Test, + testCase: Test.Case? + ) async throws { + try await withMacroTesting( + indentationWidth: configuration.indentationWidth, + macros: configuration.macros + ) { + try await withSnapshotTesting(record: record) { + try await function() + } + } + } + } +#endif diff --git a/Tests/MacroTestingTests/SwiftTestingTests.swift b/Tests/MacroTestingTests/SwiftTestingTests.swift new file mode 100644 index 0000000..df857db --- /dev/null +++ b/Tests/MacroTestingTests/SwiftTestingTests.swift @@ -0,0 +1,55 @@ +#if canImport(Testing) + @_spi(Experimental) import MacroTesting + import Testing + + @Suite( + .macros( + //record: .failed, + macros: ["URL": URLMacro.self] + ) + ) + struct URLMacroSwiftTestingTests { + @Test + func expansionWithMalformedURLEmitsError() { + assertMacro { + """ + let invalid = #URL("https://not a url.com") + """ + } diagnostics: { + """ + let invalid = #URL("https://not a url.com") + ┬──────────────────────────── + ╰─ 🛑 malformed url: "https://not a url.com" + """ + } + } + + @Test + func expansionWithStringInterpolationEmitsError() { + assertMacro { + #""" + #URL("https://\(domain)/api/path") + """# + } diagnostics: { + #""" + #URL("https://\(domain)/api/path") + ┬───────────────────────────────── + ╰─ 🛑 #URL requires a static string literal + """# + } + } + + @Test + func expansionWithValidURL() { + assertMacro { + """ + let valid = #URL("https://swift.org/") + """ + } expansion: { + """ + let valid = URL(string: "https://swift.org/")! + """ + } + } + } +#endif From 829cacdf3995dc4f4225bd42d76b22782d984264 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Mon, 8 Jul 2024 14:10:20 -0400 Subject: [PATCH 04/10] fix --- Sources/MacroTesting/Internal/RecordIssue.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/MacroTesting/Internal/RecordIssue.swift b/Sources/MacroTesting/Internal/RecordIssue.swift index f63669c..76f0a4c 100644 --- a/Sources/MacroTesting/Internal/RecordIssue.swift +++ b/Sources/MacroTesting/Internal/RecordIssue.swift @@ -21,6 +21,6 @@ public func recordIssue( XCTFail(message(), file: file, line: line) } #else - XCTFail(message(), file: filePath, line: line) + XCTFail(message(), file: file, line: line) #endif } From 0c2870e701b912b55e2564b2ce8f356eb46c7ad0 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Mon, 8 Jul 2024 14:18:56 -0400 Subject: [PATCH 05/10] wip --- Package.resolved | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.resolved b/Package.resolved index 5e85967..a8d5a77 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,7 +6,7 @@ "location" : "https://github.com/pointfreeco/swift-snapshot-testing", "state" : { "branch" : "swift-testing", - "revision" : "b479fa466e0d53febf049437b747a3fcb4dec216" + "revision" : "bd68b8f751713d6f8dc2eb79689f2dbb31988307" } }, { From fe42e56fa07e4effe916f1a9e108343a8f835725 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Mon, 8 Jul 2024 14:33:09 -0400 Subject: [PATCH 06/10] update snaps --- Sources/MacroTesting/AssertMacro.swift | 6 ++++-- Tests/MacroTestingTests/AddAsyncTests.swift | 2 ++ .../AddCompletionHandlerTests.swift | 2 ++ Tests/MacroTestingTests/OptionSetMacroTests.swift | 12 ++++++------ 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Sources/MacroTesting/AssertMacro.swift b/Sources/MacroTesting/AssertMacro.swift index 8bf8c0c..1f68100 100644 --- a/Sources/MacroTesting/AssertMacro.swift +++ b/Sources/MacroTesting/AssertMacro.swift @@ -563,8 +563,10 @@ public func withMacroTesting( var configuration = MacroTestingConfiguration.current if let indentationWidth { configuration.indentationWidth = indentationWidth } if let macros { configuration.macros = macros } - try MacroTestingConfiguration.$current.withValue(configuration) { - try operation() + _ = try withSnapshotTesting(record: record) { + try MacroTestingConfiguration.$current.withValue(configuration) { + try operation() + } } } diff --git a/Tests/MacroTestingTests/AddAsyncTests.swift b/Tests/MacroTestingTests/AddAsyncTests.swift index e2d54f1..673709b 100644 --- a/Tests/MacroTestingTests/AddAsyncTests.swift +++ b/Tests/MacroTestingTests/AddAsyncTests.swift @@ -34,6 +34,7 @@ final class AddAsyncMacroTests: BaseTestCase { } } } + } """# } @@ -60,6 +61,7 @@ final class AddAsyncMacroTests: BaseTestCase { continuation.resume(returning: returnValue) } } + } """ } diff --git a/Tests/MacroTestingTests/AddCompletionHandlerTests.swift b/Tests/MacroTestingTests/AddCompletionHandlerTests.swift index f577d9c..9277064 100644 --- a/Tests/MacroTestingTests/AddCompletionHandlerTests.swift +++ b/Tests/MacroTestingTests/AddCompletionHandlerTests.swift @@ -26,6 +26,7 @@ final class AddCompletionHandlerTests: BaseTestCase { Task { completionHandler(await f(a: a, for: b, value)) } + } """ } @@ -93,6 +94,7 @@ final class AddCompletionHandlerTests: BaseTestCase { Task { completionHandler(await fetchData()) } + } } """ diff --git a/Tests/MacroTestingTests/OptionSetMacroTests.swift b/Tests/MacroTestingTests/OptionSetMacroTests.swift index 05cac84..ff36d98 100644 --- a/Tests/MacroTestingTests/OptionSetMacroTests.swift +++ b/Tests/MacroTestingTests/OptionSetMacroTests.swift @@ -50,16 +50,16 @@ final class OptionSetMacroTests: BaseTestCase { } static let nextDay: Self = - Self (rawValue: 1 << Options.nextDay.rawValue) + Self(rawValue: 1 << Options.nextDay.rawValue) static let secondDay: Self = - Self (rawValue: 1 << Options.secondDay.rawValue) + Self(rawValue: 1 << Options.secondDay.rawValue) static let priority: Self = - Self (rawValue: 1 << Options.priority.rawValue) + Self(rawValue: 1 << Options.priority.rawValue) static let standard: Self = - Self (rawValue: 1 << Options.standard.rawValue) + Self(rawValue: 1 << Options.standard.rawValue) } extension ShippingOptions: OptionSet { @@ -100,10 +100,10 @@ final class OptionSetMacroTests: BaseTestCase { } public static let nextDay: Self = - Self (rawValue: 1 << Options.nextDay.rawValue) + Self(rawValue: 1 << Options.nextDay.rawValue) public static let standard: Self = - Self (rawValue: 1 << Options.standard.rawValue) + Self(rawValue: 1 << Options.standard.rawValue) } """ } From 6973c509b0bdfd0e252a4db959ae8e43a4d242f8 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Mon, 8 Jul 2024 16:40:22 -0400 Subject: [PATCH 07/10] update snapshot testing to 1.17 --- Package.resolved | 4 ++-- Package.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Package.resolved b/Package.resolved index a8d5a77..f3edd8d 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/pointfreeco/swift-snapshot-testing", "state" : { - "branch" : "swift-testing", - "revision" : "bd68b8f751713d6f8dc2eb79689f2dbb31988307" + "revision" : "f6c51fa7609b1057ca5420127440413c54971ff6", + "version" : "1.17.0" } }, { diff --git a/Package.swift b/Package.swift index 659e77b..eda8ab8 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/apple/swift-syntax", "509.0.0"..<"601.0.0-prerelease"), - .package(url: "https://github.com/pointfreeco/swift-snapshot-testing", branch: "swift-testing"), + .package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.17.0"), ], targets: [ .target( From a8abe0fe9a36b194c46f60f2854518bffd9d5aa6 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Mon, 8 Jul 2024 16:42:28 -0400 Subject: [PATCH 08/10] wip --- Sources/MacroTesting/AssertMacro.swift | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Sources/MacroTesting/AssertMacro.swift b/Sources/MacroTesting/AssertMacro.swift index 1f68100..9be2f97 100644 --- a/Sources/MacroTesting/AssertMacro.swift +++ b/Sources/MacroTesting/AssertMacro.swift @@ -533,12 +533,14 @@ public func withMacroTesting( record: SnapshotTestingConfiguration.Record? = nil, macros: [String: Macro.Type]? = nil, operation: () async throws -> R -) async rethrows { +) async rethrows -> R { var configuration = MacroTestingConfiguration.current if let indentationWidth { configuration.indentationWidth = indentationWidth } if let macros { configuration.macros = macros } - try await MacroTestingConfiguration.$current.withValue(configuration) { - try await operation() + return try await withSnapshotTesting(record: record) { + try await MacroTestingConfiguration.$current.withValue(configuration) { + try await operation() + } } } @@ -559,11 +561,11 @@ public func withMacroTesting( record: SnapshotTestingConfiguration.Record? = nil, macros: [String: Macro.Type]? = nil, operation: () throws -> R -) rethrows { +) rethrows -> R { var configuration = MacroTestingConfiguration.current if let indentationWidth { configuration.indentationWidth = indentationWidth } if let macros { configuration.macros = macros } - _ = try withSnapshotTesting(record: record) { + return try withSnapshotTesting(record: record) { try MacroTestingConfiguration.$current.withValue(configuration) { try operation() } @@ -587,7 +589,7 @@ public func withMacroTesting( record: SnapshotTestingConfiguration.Record? = nil, macros: [Macro.Type], operation: () async throws -> R -) async rethrows { +) async rethrows -> R { try await withMacroTesting( indentationWidth: indentationWidth, record: record, @@ -613,7 +615,7 @@ public func withMacroTesting( record: SnapshotTestingConfiguration.Record? = nil, macros: [Macro.Type], operation: () throws -> R -) rethrows { +) rethrows -> R { try withMacroTesting( indentationWidth: indentationWidth, record: record, From 8c56c27adcdb3e252ab399c242a9c9f048264a63 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Tue, 9 Jul 2024 09:55:21 -0700 Subject: [PATCH 09/10] Updates --- .swiftpm/configuration/Package.resolved | 23 ++++++++++++ Package.swift | 2 +- Sources/MacroTesting/AssertMacro.swift | 35 +++++++++++------- .../MacroTesting/Internal/Deprecations.swift | 36 ++++++++++++++----- .../MacroTesting/Internal/RecordIssue.swift | 18 ++++++---- Tests/MacroTestingTests/AddAsyncTests.swift | 2 -- .../AddCompletionHandlerTests.swift | 2 -- .../OptionSetMacroTests.swift | 12 +++---- 8 files changed, 92 insertions(+), 38 deletions(-) create mode 100644 .swiftpm/configuration/Package.resolved diff --git a/.swiftpm/configuration/Package.resolved b/.swiftpm/configuration/Package.resolved new file mode 100644 index 0000000..2c3597d --- /dev/null +++ b/.swiftpm/configuration/Package.resolved @@ -0,0 +1,23 @@ +{ + "pins" : [ + { + "identity" : "swift-snapshot-testing", + "kind" : "remoteSourceControl", + "location" : "https://github.com/pointfreeco/swift-snapshot-testing", + "state" : { + "revision" : "63d3b45dd249878a41c56274a748ca2c1c9c5230", + "version" : "1.17.1" + } + }, + { + "identity" : "swift-syntax", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-syntax", + "state" : { + "revision" : "303e5c5c36d6a558407d364878df131c3546fad8", + "version" : "510.0.2" + } + } + ], + "version" : 2 +} diff --git a/Package.swift b/Package.swift index eda8ab8..b6942c9 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/apple/swift-syntax", "509.0.0"..<"601.0.0-prerelease"), - .package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.17.0"), + .package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.17.1"), ], targets: [ .target( diff --git a/Sources/MacroTesting/AssertMacro.swift b/Sources/MacroTesting/AssertMacro.swift index 9be2f97..f8cf2b3 100644 --- a/Sources/MacroTesting/AssertMacro.swift +++ b/Sources/MacroTesting/AssertMacro.swift @@ -118,7 +118,8 @@ public func assertMacro( diagnostics diagnosedSource: (() -> String)? = nil, fixes fixedSource: (() -> String)? = nil, expansion expandedSource: (() -> String)? = nil, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column @@ -143,8 +144,10 @@ public func assertMacro( … } """, - file: file, - line: line + fileID: fileID, + filePath: filePath, + line: line, + column: column ) return } @@ -233,7 +236,7 @@ public func assertMacro( trailingClosureOffset: offset ), matches: diagnosedSource, - file: file, + file: filePath, function: function, line: line, column: column @@ -253,7 +256,7 @@ public func assertMacro( trailingClosureOffset: offset ), matches: diagnosedSource, - file: file, + file: filePath, function: function, line: line, column: column @@ -294,7 +297,7 @@ public func assertMacro( trailingClosureOffset: offset ), matches: fixedSource, - file: file, + file: filePath, function: function, line: line, column: column @@ -332,7 +335,7 @@ public func assertMacro( trailingClosureOffset: offset ), matches: fixedSource, - file: file, + file: filePath, function: function, line: line, column: column @@ -354,7 +357,7 @@ public func assertMacro( trailingClosureOffset: offset ), matches: expandedSource, - file: file, + file: filePath, function: function, line: line, column: column @@ -374,14 +377,20 @@ public func assertMacro( trailingClosureOffset: offset ), matches: expandedSource, - file: file, + file: filePath, function: function, line: line, column: column ) } } catch { - recordIssue("Threw error: \(error)", file: file, line: line) + recordIssue( + "Threw error: \(error)", + fileID: fileID, + filePath: filePath, + line: line, + column: column + ) } } } @@ -470,7 +479,8 @@ public func assertMacro( diagnostics diagnosedSource: (() -> String)? = nil, fixes fixedSource: (() -> String)? = nil, expansion expandedSource: (() -> String)? = nil, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column @@ -483,7 +493,8 @@ public func assertMacro( diagnostics: diagnosedSource, fixes: fixedSource, expansion: expandedSource, - file: file, + fileID: fileID, + file: filePath, function: function, line: line, column: column diff --git a/Sources/MacroTesting/Internal/Deprecations.swift b/Sources/MacroTesting/Internal/Deprecations.swift index 4d5df81..7d86f9a 100644 --- a/Sources/MacroTesting/Internal/Deprecations.swift +++ b/Sources/MacroTesting/Internal/Deprecations.swift @@ -106,20 +106,27 @@ public func assertMacro( record isRecording: Bool? = nil, of originalSource: () throws -> String, matches expandedOrDiagnosedSource: () -> String, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column ) { guard isRecording ?? (SnapshotTestingConfiguration.current?.record == .all) else { - recordIssue("Re-record this assertion", file: file, line: line) + recordIssue( + "Re-record this assertion", + fileID: fileID, + filePath: filePath, + line: line, + column: column + ) return } assertMacro( macros, record: true, of: originalSource, - file: file, + file: filePath, function: function, line: line, column: column @@ -132,7 +139,8 @@ public func assertMacro( record isRecording: Bool? = nil, of originalSource: () throws -> String, matches expandedOrDiagnosedSource: () -> String, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column @@ -142,7 +150,8 @@ public func assertMacro( record: isRecording, of: originalSource, matches: expandedOrDiagnosedSource, - file: file, + fileID: fileID, + file: filePath, function: function, line: line, column: column @@ -158,12 +167,19 @@ public func assertMacro( record isRecording: Bool? = nil, of originalSource: () throws -> String, matches expandedOrDiagnosedSource: () -> String, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column ) { - recordIssue("Delete 'matches' and re-record this assertion", file: file, line: line) + recordIssue( + "Delete 'matches' and re-record this assertion", + fileID: fileID, + filePath: filePath, + line: line, + column: column + ) } @available( @@ -175,7 +191,8 @@ public func assertMacro( record isRecording: Bool? = nil, of originalSource: () throws -> String, matches expandedOrDiagnosedSource: () -> String, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column @@ -186,7 +203,8 @@ public func assertMacro( record: isRecording, of: originalSource, matches: expandedOrDiagnosedSource, - file: file, + fileID: fileID, + file: filePath, function: function, line: line, column: column diff --git a/Sources/MacroTesting/Internal/RecordIssue.swift b/Sources/MacroTesting/Internal/RecordIssue.swift index 76f0a4c..01c700d 100644 --- a/Sources/MacroTesting/Internal/RecordIssue.swift +++ b/Sources/MacroTesting/Internal/RecordIssue.swift @@ -7,20 +7,26 @@ import XCTest @_spi(Internals) public func recordIssue( _ message: @autoclosure () -> String, - file: StaticString = #filePath, - line: UInt = #line + fileID: StaticString, + filePath: StaticString, + line: UInt, + column: UInt ) { #if canImport(Testing) if Test.current != nil { Issue.record( Comment(rawValue: message()), - filePath: file.description, - line: Int(line) + sourceLocation: SourceLocation( + fileID: fileID.description, + filePath: filePath.description, + line: Int(line), + column: Int(column) + ) ) } else { - XCTFail(message(), file: file, line: line) + XCTFail(message(), file: filePath, line: line) } #else - XCTFail(message(), file: file, line: line) + XCTFail(message(), file: filePath, line: line) #endif } diff --git a/Tests/MacroTestingTests/AddAsyncTests.swift b/Tests/MacroTestingTests/AddAsyncTests.swift index 673709b..e2d54f1 100644 --- a/Tests/MacroTestingTests/AddAsyncTests.swift +++ b/Tests/MacroTestingTests/AddAsyncTests.swift @@ -34,7 +34,6 @@ final class AddAsyncMacroTests: BaseTestCase { } } } - } """# } @@ -61,7 +60,6 @@ final class AddAsyncMacroTests: BaseTestCase { continuation.resume(returning: returnValue) } } - } """ } diff --git a/Tests/MacroTestingTests/AddCompletionHandlerTests.swift b/Tests/MacroTestingTests/AddCompletionHandlerTests.swift index 9277064..f577d9c 100644 --- a/Tests/MacroTestingTests/AddCompletionHandlerTests.swift +++ b/Tests/MacroTestingTests/AddCompletionHandlerTests.swift @@ -26,7 +26,6 @@ final class AddCompletionHandlerTests: BaseTestCase { Task { completionHandler(await f(a: a, for: b, value)) } - } """ } @@ -94,7 +93,6 @@ final class AddCompletionHandlerTests: BaseTestCase { Task { completionHandler(await fetchData()) } - } } """ diff --git a/Tests/MacroTestingTests/OptionSetMacroTests.swift b/Tests/MacroTestingTests/OptionSetMacroTests.swift index ff36d98..05cac84 100644 --- a/Tests/MacroTestingTests/OptionSetMacroTests.swift +++ b/Tests/MacroTestingTests/OptionSetMacroTests.swift @@ -50,16 +50,16 @@ final class OptionSetMacroTests: BaseTestCase { } static let nextDay: Self = - Self(rawValue: 1 << Options.nextDay.rawValue) + Self (rawValue: 1 << Options.nextDay.rawValue) static let secondDay: Self = - Self(rawValue: 1 << Options.secondDay.rawValue) + Self (rawValue: 1 << Options.secondDay.rawValue) static let priority: Self = - Self(rawValue: 1 << Options.priority.rawValue) + Self (rawValue: 1 << Options.priority.rawValue) static let standard: Self = - Self(rawValue: 1 << Options.standard.rawValue) + Self (rawValue: 1 << Options.standard.rawValue) } extension ShippingOptions: OptionSet { @@ -100,10 +100,10 @@ final class OptionSetMacroTests: BaseTestCase { } public static let nextDay: Self = - Self(rawValue: 1 << Options.nextDay.rawValue) + Self (rawValue: 1 << Options.nextDay.rawValue) public static let standard: Self = - Self(rawValue: 1 << Options.standard.rawValue) + Self (rawValue: 1 << Options.standard.rawValue) } """ } From 51626e8bfb55986ea7c7f1d71e55dad3ee7c4b41 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Tue, 9 Jul 2024 10:08:00 -0700 Subject: [PATCH 10/10] wip --- Tests/MacroTestingTests/AddAsyncTests.swift | 2 ++ .../AddCompletionHandlerTests.swift | 2 ++ Tests/MacroTestingTests/OptionSetMacroTests.swift | 12 ++++++------ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Tests/MacroTestingTests/AddAsyncTests.swift b/Tests/MacroTestingTests/AddAsyncTests.swift index e2d54f1..673709b 100644 --- a/Tests/MacroTestingTests/AddAsyncTests.swift +++ b/Tests/MacroTestingTests/AddAsyncTests.swift @@ -34,6 +34,7 @@ final class AddAsyncMacroTests: BaseTestCase { } } } + } """# } @@ -60,6 +61,7 @@ final class AddAsyncMacroTests: BaseTestCase { continuation.resume(returning: returnValue) } } + } """ } diff --git a/Tests/MacroTestingTests/AddCompletionHandlerTests.swift b/Tests/MacroTestingTests/AddCompletionHandlerTests.swift index f577d9c..9277064 100644 --- a/Tests/MacroTestingTests/AddCompletionHandlerTests.swift +++ b/Tests/MacroTestingTests/AddCompletionHandlerTests.swift @@ -26,6 +26,7 @@ final class AddCompletionHandlerTests: BaseTestCase { Task { completionHandler(await f(a: a, for: b, value)) } + } """ } @@ -93,6 +94,7 @@ final class AddCompletionHandlerTests: BaseTestCase { Task { completionHandler(await fetchData()) } + } } """ diff --git a/Tests/MacroTestingTests/OptionSetMacroTests.swift b/Tests/MacroTestingTests/OptionSetMacroTests.swift index 05cac84..ff36d98 100644 --- a/Tests/MacroTestingTests/OptionSetMacroTests.swift +++ b/Tests/MacroTestingTests/OptionSetMacroTests.swift @@ -50,16 +50,16 @@ final class OptionSetMacroTests: BaseTestCase { } static let nextDay: Self = - Self (rawValue: 1 << Options.nextDay.rawValue) + Self(rawValue: 1 << Options.nextDay.rawValue) static let secondDay: Self = - Self (rawValue: 1 << Options.secondDay.rawValue) + Self(rawValue: 1 << Options.secondDay.rawValue) static let priority: Self = - Self (rawValue: 1 << Options.priority.rawValue) + Self(rawValue: 1 << Options.priority.rawValue) static let standard: Self = - Self (rawValue: 1 << Options.standard.rawValue) + Self(rawValue: 1 << Options.standard.rawValue) } extension ShippingOptions: OptionSet { @@ -100,10 +100,10 @@ final class OptionSetMacroTests: BaseTestCase { } public static let nextDay: Self = - Self (rawValue: 1 << Options.nextDay.rawValue) + Self(rawValue: 1 << Options.nextDay.rawValue) public static let standard: Self = - Self (rawValue: 1 << Options.standard.rawValue) + Self(rawValue: 1 << Options.standard.rawValue) } """ }