Skip to content

Commit c772dc6

Browse files
committed
Implement my suggested changes atop Tim's branch
1 parent 6287124 commit c772dc6

File tree

2 files changed

+51
-133
lines changed

2 files changed

+51
-133
lines changed

Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift

Lines changed: 31 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public import SwiftSyntaxMacroExpansion
1717
public import SwiftSyntaxMacros
1818
@_spi(XCTestFailureLocation) public import SwiftSyntaxMacrosGenericTestSupport
1919
private import XCTest
20-
#if canImport(Testing)
20+
#if compiler(>=6)
2121
private import Testing
2222
#endif
2323
#else
@@ -37,10 +37,6 @@ public typealias DiagnosticSpec = SwiftSyntaxMacrosGenericTestSupport.Diagnostic
3737
/// Assert that expanding the given macros in the original source produces
3838
/// the given expanded source code.
3939
///
40-
/// - Warning: If you call this function inside a Swift Testing test case,
41-
/// the test will report no assertion failures even if the test fails.
42-
/// Use ``expectMacroExpansion(_:expandedSource:diagnostics:macroSpecs:applyFixIts:fixedSource:testModuleName:testFileName:indentationWidth:buildConfiguration:fileID:file:line:column:)`` instead.
43-
///
4440
/// - Parameters:
4541
/// - originalSource: The original source code, which is expected to contain
4642
/// macros in various places (e.g., `#stringify(x + y)`).
@@ -55,7 +51,7 @@ public typealias DiagnosticSpec = SwiftSyntaxMacrosGenericTestSupport.Diagnostic
5551
/// - indentationWidth: The indentation width used in the expansion.
5652
/// - buildConfiguration: a build configuration that will be made available
5753
/// to the macro implementation
58-
/// - SeeAlso: ``assertMacroExpansion(_:expandedSource:diagnostics:macroSpecs:applyFixIts:fixedSource:testModuleName:testFileName:indentationWidth:buildConfiguration:file:line:)``
54+
/// - SeeAlso: ``assertMacroExpansion(_:expandedSource:diagnostics:macroSpecs:applyFixIts:fixedSource:testModuleName:testFileName:indentationWidth:buildConfiguration:fileID:file:line:column:)``
5955
/// to also specify the list of conformances passed to the macro expansion.
6056
public func assertMacroExpansion(
6157
_ originalSource: String,
@@ -68,8 +64,10 @@ public func assertMacroExpansion(
6864
testFileName: String = "test.swift",
6965
indentationWidth: Trivia = .spaces(4),
7066
buildConfiguration: (any BuildConfiguration)? = nil,
67+
fileID: StaticString = #fileID,
7168
file: StaticString = #filePath,
72-
line: UInt = #line
69+
line: UInt = #line,
70+
column: UInt = #column
7371
) {
7472
let specs = macros.mapValues { MacroSpec(type: $0) }
7573
assertMacroExpansion(
@@ -83,18 +81,16 @@ public func assertMacroExpansion(
8381
testFileName: testFileName,
8482
indentationWidth: indentationWidth,
8583
buildConfiguration: buildConfiguration,
84+
fileID: fileID,
8685
file: file,
87-
line: line
86+
line: line,
87+
column: column
8888
)
8989
}
9090

9191
/// Assert that expanding the given macros in the original source produces
9292
/// the given expanded source code.
9393
///
94-
/// - Warning: If you call this function inside a Swift Testing test case,
95-
/// the test will report no assertion failures even if the test fails.
96-
/// Use ``expectMacroExpansion(_:expandedSource:diagnostics:macroSpecs:applyFixIts:fixedSource:testModuleName:testFileName:indentationWidth:buildConfiguration:fileID:file:line:column:)`` instead.
97-
///
9894
/// - Parameters:
9995
/// - originalSource: The original source code, which is expected to contain
10096
/// macros in various places (e.g., `#stringify(x + y)`).
@@ -110,62 +106,9 @@ public func assertMacroExpansion(
110106
/// - testModuleName: The name of the test module to use.
111107
/// - testFileName: The name of the test file name to use.
112108
/// - indentationWidth: The indentation width used in the expansion.
109+
/// - buildConfiguration: a build configuration that will be made available
110+
/// to the macro implementation
113111
public func assertMacroExpansion(
114-
_ originalSource: String,
115-
expandedSource expectedExpandedSource: String,
116-
diagnostics: [DiagnosticSpec] = [],
117-
macroSpecs: [String: MacroSpec],
118-
applyFixIts: [String]? = nil,
119-
fixedSource expectedFixedSource: String? = nil,
120-
testModuleName: String = "TestModule",
121-
testFileName: String = "test.swift",
122-
indentationWidth: Trivia = .spaces(4),
123-
buildConfiguration: (any BuildConfiguration)? = nil,
124-
file: StaticString = #filePath,
125-
line: UInt = #line
126-
) {
127-
SwiftSyntaxMacrosGenericTestSupport.assertMacroExpansion(
128-
originalSource,
129-
expandedSource: expectedExpandedSource,
130-
diagnostics: diagnostics,
131-
macroSpecs: macroSpecs,
132-
applyFixIts: applyFixIts,
133-
fixedSource: expectedFixedSource,
134-
testModuleName: testModuleName,
135-
testFileName: testFileName,
136-
indentationWidth: indentationWidth,
137-
buildConfiguration: buildConfiguration,
138-
failureHandler: {
139-
XCTFail($0.message, file: $0.location.staticFilePath, line: $0.location.unsignedLine)
140-
},
141-
fileID: "", // Not used in the failure handler
142-
filePath: file,
143-
line: line,
144-
column: 0 // Not used in the failure handler
145-
)
146-
}
147-
148-
#if canImport(Testing)
149-
/// Expect that expanding the given macros in the original source produces
150-
/// the given expanded source code.
151-
/// This function is the Swift Testing equivalent of ``assertMacroExpansion(_:expandedSource:diagnostics:macroSpecs:applyFixIts:fixedSource:testModuleName:testFileName:indentationWidth:buildConfiguration:file:line:)``
152-
///
153-
/// - Parameters:
154-
/// - originalSource: The original source code, which is expected to contain
155-
/// macros in various places (e.g., `#stringify(x + y)`).
156-
/// - expectedExpandedSource: The source code that we expect to see after
157-
/// performing macro expansion on the original source.
158-
/// - diagnostics: The diagnostics when expanding any macro
159-
/// - macroSpecs: The macros that should be expanded, provided as a dictionary
160-
/// mapping macro names (e.g., `"CodableMacro"`) to specification with macro type
161-
/// (e.g., `CodableMacro.self`) and a list of conformances macro provides
162-
/// (e.g., `["Decodable", "Encodable"]`).
163-
/// - applyFixIts: If specified, filters the Fix-Its that are applied to generate `fixedSource` to only those whose message occurs in this array. If `nil`, all Fix-Its from the diagnostics are applied.
164-
/// - fixedSource: If specified, asserts that the source code after applying Fix-Its matches this string.
165-
/// - testModuleName: The name of the test module to use.
166-
/// - testFileName: The name of the test file name to use.
167-
/// - indentationWidth: The indentation width used in the expansion.
168-
public func expectMacroExpansion(
169112
_ originalSource: String,
170113
expandedSource expectedExpandedSource: String,
171114
diagnostics: [DiagnosticSpec] = [],
@@ -192,74 +135,34 @@ public func expectMacroExpansion(
192135
testFileName: testFileName,
193136
indentationWidth: indentationWidth,
194137
buildConfiguration: buildConfiguration,
195-
failureHandler: {
138+
failureHandler: { testFailure in
139+
// Record a Swift Testing issue.
140+
//
141+
// (Note: If/when Swift Testing gains interoperability with XCTest, this
142+
// will be translated into an XCTest failure if called while an XCTest is
143+
// running.)
196144
Issue.record(
197-
Comment(rawValue: $0.message),
145+
Comment(rawValue: testFailure.message),
198146
sourceLocation: .init(
199-
fileID: fileID.description,
200-
filePath: file.description,
201-
line: Int(line),
202-
column: Int(column)
147+
fileID: testFailure.location.fileID,
148+
filePath: testFailure.location.filePath,
149+
line: testFailure.location.line,
150+
column: testFailure.location.column
203151
)
204152
)
205-
},
206-
fileID: "", // Not used in the failure handler
207-
filePath: file,
208-
line: line,
209-
column: 0 // Not used in the failure handler
210-
)
211-
}
212153

213-
/// Expect that expanding the given macros in the original source produces
214-
/// the given expanded source code.
215-
/// This function is the Swift Testing equivalent of ``assertMacroExpansion(_:expandedSource:diagnostics:macroSpecs:applyFixIts:fixedSource:testModuleName:testFileName:indentationWidth:buildConfiguration:file:line:)``
216-
///
217-
/// - Parameters:
218-
/// - originalSource: The original source code, which is expected to contain
219-
/// macros in various places (e.g., `#stringify(x + y)`).
220-
/// - expectedExpandedSource: The source code that we expect to see after
221-
/// performing macro expansion on the original source.
222-
/// - diagnostics: The diagnostics when expanding any macro
223-
/// - macros: The macros that should be expanded, provided as a dictionary
224-
/// mapping macro names (e.g., `"stringify"`) to implementation types
225-
/// (e.g., `StringifyMacro.self`).
226-
/// - applyFixIts: If specified, filters the Fix-Its that are applied to generate `fixedSource` to only those whose message occurs in this array. If `nil`, all Fix-Its from the diagnostics are applied.
227-
/// - fixedSource: If specified, asserts that the source code after applying Fix-Its matches this string.
228-
/// - testModuleName: The name of the test module to use.
229-
/// - testFileName: The name of the test file name to use.
230-
/// - indentationWidth: The indentation width used in the expansion.
231-
public func expectMacroExpansion(
232-
_ originalSource: String,
233-
expandedSource expectedExpandedSource: String,
234-
diagnostics: [DiagnosticSpec] = [],
235-
macros: [String: Macro.Type],
236-
applyFixIts: [String]? = nil,
237-
fixedSource expectedFixedSource: String? = nil,
238-
testModuleName: String = "TestModule",
239-
testFileName: String = "test.swift",
240-
indentationWidth: Trivia = .spaces(4),
241-
buildConfiguration: (any BuildConfiguration)? = nil,
242-
fileID: StaticString = #fileID,
243-
file: StaticString = #filePath,
244-
line: UInt = #line,
245-
column: UInt = #column
246-
) {
247-
let specs = macros.mapValues { MacroSpec(type: $0) }
248-
expectMacroExpansion(
249-
originalSource,
250-
expandedSource: expectedExpandedSource,
251-
diagnostics: diagnostics,
252-
macroSpecs: specs,
253-
applyFixIts: applyFixIts,
254-
fixedSource: expectedFixedSource,
255-
testModuleName: testModuleName,
256-
testFileName: testFileName,
257-
indentationWidth: indentationWidth,
258-
buildConfiguration: buildConfiguration,
154+
// Record an XCTest failure.
155+
//
156+
// FIXME: If/when Swift Testing gains interoperability with XCTest, this
157+
// will become redundant with the above -- at least, when this library is
158+
// compiled and run using a Swift version which supports interoperability.
159+
// At that point, this should be adjusted so that it's only called when
160+
// using older Swift versions.
161+
XCTFail(testFailure.message, file: testFailure.location.staticFilePath, line: testFailure.location.unsignedLine)
162+
},
259163
fileID: fileID,
260-
file: file,
164+
filePath: file,
261165
line: line,
262166
column: column
263167
)
264168
}
265-
#endif

Tests/SwiftSyntaxMacroExpansionTest/SwiftTestingTests.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#if canImport(Testing)
13+
#if compiler(>=6)
1414
import Testing
1515
import SwiftSyntaxMacrosTestSupport
1616

1717
@Suite("Swift Testing Macro Expansion Tests")
1818
struct SwiftTestingMacroExpansionTests {
1919
@Test("Test Happy Path")
2020
func testHappyPathWorks() {
21-
expectMacroExpansion(
21+
assertMacroExpansion(
2222
"""
2323
@constantOne
2424
var x: Int /*1*/ // hello
@@ -38,7 +38,7 @@ struct SwiftTestingMacroExpansionTests {
3838
@Test("Test Failure")
3939
func failureReportedCorrectly() {
4040
withKnownIssue {
41-
expectMacroExpansion(
41+
assertMacroExpansion(
4242
"""
4343
@constantOne
4444
var x: Int /*1*/ // hello
@@ -51,10 +51,25 @@ struct SwiftTestingMacroExpansionTests {
5151
}
5252
""",
5353
macros: ["constantOne": ConstantOneGetter.self],
54-
indentationWidth: .spaces(4)
54+
indentationWidth: .spaces(4),
55+
fileID: "ExampleModule/ExampleTest.swift",
56+
file: "path/to/ExampleTest.swift",
57+
line: 9999,
58+
column: 8888
5559
)
5660
} matching: { issue in
57-
issue.description.contains("Macro expansion did not produce the expected expanded source")
61+
guard issue.description.contains("Macro expansion did not produce the expected expanded source") else {
62+
return false
63+
}
64+
65+
#expect(issue.sourceLocation == .init(
66+
fileID: "ExampleModule/ExampleTest.swift",
67+
filePath: "path/to/ExampleTest.swift",
68+
line: 9999,
69+
column: 8888
70+
))
71+
72+
return true
5873
}
5974
}
6075
}

0 commit comments

Comments
 (0)