Skip to content

Improve support for Swift Syntax 600 and fix SwiftTesting #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
[
"509.0.0..<510.0.0",
"510.0.0..<511.0.0",
"511.0.0..<601.0.0-prerelease",
"511.0.0..<601.0.0",
]

steps:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/Packages
/*.swiftinterface
/*.xcodeproj
xcuserdata/
xcuserdata/
tmp
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test-swift-syntax-versions:
@for version in \
"509.0.0..<510.0.0" \
"510.0.0..<511.0.0" \
"511.0.0..<601.0.0-prerelease"; \
"511.0.0..<601.0.0"; \
do \
echo "\n## Testing SwiftSyntax version $$version"; \
$(MAKE) clean; \
Expand Down
10 changes: 5 additions & 5 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
"state" : {
"revision" : "63d3b45dd249878a41c56274a748ca2c1c9c5230",
"version" : "1.17.1"
"revision" : "7b0bbbae90c41f848f90ac7b4df6c4f50068256d",
"version" : "1.17.5"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax",
"location" : "https://github.com/swiftlang/swift-syntax",
"state" : {
"revision" : "303e5c5c36d6a558407d364878df131c3546fad8",
"version" : "510.0.2"
"revision" : "0687f71944021d616d34d922343dcef086855920",
"version" : "600.0.1"
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let package = Package(
.conditionalPackage(
url: "https://github.com/swiftlang/swift-syntax",
envVar: "SWIFT_SYNTAX_VERSION",
default: "509.0.0..<601.0.0-prerelease"
default: "509.0.0..<601.0.0"
),
],
targets: [
Expand Down
58 changes: 45 additions & 13 deletions Sources/MacroTesting/AssertMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import SwiftSyntaxMacroExpansion
import SwiftSyntaxMacros
import XCTest

#if canImport(Testing)
import Testing
#endif

//let compilableUsages = {
// assertMacro { "" }
// assertMacro { "" } expansion: { "" }
Expand Down Expand Up @@ -190,8 +194,32 @@ public func assertMacro(
line: UInt = #line,
column: UInt = #column
) {
withSnapshotTesting(record: record ?? SnapshotTestingConfiguration.current?.record) {
let macros = macros ?? MacroTestingConfiguration.current.macros
var indentationWidth =
indentationWidth
?? MacroTestingConfiguration.current.indentationWidth
var macros =
macros
?? MacroTestingConfiguration.current.macros
var record =
record
?? SnapshotTestingConfiguration.current?.record
#if canImport(Testing)
indentationWidth =
indentationWidth
?? Test.current?.indentationWidth
macros =
macros
?? Test.current?.macros
record =
record
?? Test.current?.record
#endif
withMacroTesting(indentationWidth: indentationWidth, record: record, macros: macros) {
#if canImport(Testing)
let macros = macros ?? MacroTestingConfiguration.current.macros ?? Test.current?.macros
#else
let macros = macros ?? MacroTestingConfiguration.current.macros
#endif
guard let macros, !macros.isEmpty else {
recordIssue(
"""
Expand Down Expand Up @@ -249,7 +277,10 @@ public func assertMacro(
#if canImport(SwiftSyntax600)
let expandedSourceFile = origSourceFile.expand(
macros: macros,
contextGenerator: { _ in context },
contextGenerator: { syntax in
BasicMacroExpansionContext(
sharingWith: context, lexicalContext: syntax.allMacroLexicalContexts())
},
indentationWidth: indentationWidth
)
#else
Expand Down Expand Up @@ -311,6 +342,7 @@ public func assertMacro(
trailingClosureOffset: offset
),
matches: expandedSource,
fileID: fileID,
file: filePath,
function: function,
line: line,
Expand Down Expand Up @@ -666,13 +698,13 @@ public func withMacroTesting<R>(
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 = indentationWidth { configuration.indentationWidth = indentationWidth }
if let macros = macros { configuration.macros = macros }
if let indentationWidth { configuration.indentationWidth = indentationWidth }
if let macros { configuration.macros = macros }
return try await withSnapshotTesting(record: record) {
try await MacroTestingConfiguration.$current.withValue(configuration) {
_ = try await operation()
try await operation()
}
}
}
Expand All @@ -694,13 +726,13 @@ public func withMacroTesting<R>(
record: SnapshotTestingConfiguration.Record? = nil,
macros: [String: Macro.Type]? = nil,
operation: () throws -> R
) rethrows {
) rethrows -> R {
var configuration = MacroTestingConfiguration.current
if let indentationWidth = indentationWidth { configuration.indentationWidth = indentationWidth }
if let macros = macros { configuration.macros = macros }
if let indentationWidth { configuration.indentationWidth = indentationWidth }
if let macros { configuration.macros = macros }
return try withSnapshotTesting(record: record) {
try MacroTestingConfiguration.$current.withValue(configuration) {
_ = try operation()
try operation()
}
}
}
Expand All @@ -722,7 +754,7 @@ public func withMacroTesting<R>(
record: SnapshotTestingConfiguration.Record? = nil,
macros: [Macro.Type],
operation: () async throws -> R
) async rethrows {
) async rethrows -> R {
try await withMacroTesting(
indentationWidth: indentationWidth,
record: record,
Expand All @@ -748,7 +780,7 @@ public func withMacroTesting<R>(
record: SnapshotTestingConfiguration.Record? = nil,
macros: [Macro.Type],
operation: () throws -> R
) rethrows {
) rethrows -> R {
try withMacroTesting(
indentationWidth: indentationWidth,
record: record,
Expand Down
151 changes: 141 additions & 10 deletions Sources/MacroTesting/Internal/Deprecations.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//import InlineSnapshotTesting
//@_spi(Internals) import SnapshotTesting
//import SwiftDiagnostics
//import SwiftOperators
//import SwiftParser
Expand All @@ -8,6 +9,118 @@
//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<R>(
// 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 }
// let record: SnapshotTestingConfiguration.Record? = isRecording.map { $0 ? .all : .missing }
// if let macros { configuration.macros = macros }
// _ = try await withSnapshotTesting(record: record) {
// 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<R>(
// 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 }
// let record: SnapshotTestingConfiguration.Record? = isRecording.map { $0 ? .all : .missing }
// if let macros { configuration.macros = macros }
// _ = try withSnapshotTesting(record: record) {
// 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<R>(
// 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<R>(
// 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")
Expand All @@ -16,20 +129,27 @@
// 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 ?? MacroTestingConfiguration.current.isRecording else {
// XCTFail("Re-record this assertion", file: file, line: line)
// guard isRecording ?? (SnapshotTestingConfiguration.current?.record == .all) else {
// 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
Expand All @@ -42,7 +162,8 @@
// 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
Expand All @@ -52,7 +173,8 @@
// record: isRecording,
// of: originalSource,
// matches: expandedOrDiagnosedSource,
// file: file,
// fileID: fileID,
// file: filePath,
// function: function,
// line: line,
// column: column
Expand All @@ -68,12 +190,19 @@
// 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
//) {
// XCTFail("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(
Expand All @@ -85,7 +214,8 @@
// 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
Expand All @@ -96,7 +226,8 @@
// record: isRecording,
// of: originalSource,
// matches: expandedOrDiagnosedSource,
// file: file,
// fileID: fileID,
// file: filePath,
// function: function,
// line: line,
// column: column
Expand Down
Loading