Skip to content

Commit dcbdbf6

Browse files
authored
Convert Macro tests to swift-testing (#1352)
1 parent 1255d73 commit dcbdbf6

8 files changed

+97
-81
lines changed

Package.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ package.targets.append(contentsOf: [
247247
.testTarget(
248248
name: "FoundationMacrosTests",
249249
dependencies: [
250-
"FoundationMacros",
251-
"TestSupport"
250+
"FoundationMacros"
252251
],
253252
swiftSettings: availabilityMacros + featureSettings + testOnlySwiftSettings
254253
)

Tests/FoundationMacrosTests/BundleMacroTests.swift

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

13-
import XCTest
13+
import Testing
1414
import FoundationMacros
1515

16-
final class BundleMacroTests: XCTestCase {
16+
@Suite("#bundle Macro")
17+
private struct BundleMacroTests {
1718

18-
func testSimple() {
19+
@Test func testSimple() {
1920
AssertMacroExpansion(
2021
macros: ["bundle": BundleMacro.self],
2122
"""
@@ -35,7 +36,7 @@ final class BundleMacroTests: XCTestCase {
3536
)
3637
}
3738

38-
func testUsingParenthesis() {
39+
@Test func testUsingParenthesis() {
3940
AssertMacroExpansion(
4041
macros: ["bundle": BundleMacro.self],
4142
"""

Tests/FoundationMacrosTests/MacroTestUtilities.swift

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import XCTest
13+
import Testing
14+
import Foundation
1415
import FoundationMacros
1516
import SwiftSyntax
1617
import SwiftSyntaxMacros
@@ -46,9 +47,9 @@ struct DiagnosticTest : ExpressibleByStringLiteral, Hashable, CustomStringConver
4647

4748
var mappedToExpression: Self {
4849
DiagnosticTest(
49-
message.replacing("Predicate", with: "Expression").replacing("predicate", with: "expression"),
50+
message._replacing("Predicate", with: "Expression")._replacing("predicate", with: "expression"),
5051
fixIts: fixIts.map {
51-
FixItTest($0.message, result: $0.result.replacing("#Predicate", with: "#Expression"))
52+
FixItTest($0.message, result: $0.result._replacing("#Predicate", with: "#Expression"))
5253
}
5354
)
5455
}
@@ -99,7 +100,7 @@ extension Diagnostic {
99100
} else {
100101
var result = "Message: \(debugDescription)\nFix-Its:\n"
101102
for fixIt in fixIts {
102-
result += "\t\(fixIt.message.message)\n\t\(fixIt.changes.first!._result.replacingOccurrences(of: "\n", with: "\n\t"))"
103+
result += "\t\(fixIt.message.message)\n\t\(fixIt.changes.first!._result._replacing("\n", with: "\n\t"))"
103104
}
104105
return result
105106
}
@@ -113,14 +114,14 @@ extension DiagnosticTest {
113114
} else {
114115
var result = "Message: \(message)\nFix-Its:\n"
115116
for fixIt in fixIts {
116-
result += "\t\(fixIt.message)\n\t\(fixIt.result.replacingOccurrences(of: "\n", with: "\n\t"))"
117+
result += "\t\(fixIt.message)\n\t\(fixIt.result._replacing("\n", with: "\n\t"))"
117118
}
118119
return result
119120
}
120121
}
121122
}
122123

123-
func AssertMacroExpansion(macros: [String : Macro.Type], testModuleName: String = "TestModule", testFileName: String = "test.swift", _ source: String, _ result: String = "", diagnostics: Set<DiagnosticTest> = [], file: StaticString = #filePath, line: UInt = #line) {
124+
func AssertMacroExpansion(macros: [String : Macro.Type], testModuleName: String = "TestModule", testFileName: String = "test.swift", _ source: String, _ result: String = "", diagnostics: Set<DiagnosticTest> = [], sourceLocation: Testing.SourceLocation = #_sourceLocation) {
124125
let origSourceFile = Parser.parse(source: source)
125126
let expandedSourceFile: Syntax
126127
let context: BasicMacroExpansionContext
@@ -131,43 +132,53 @@ func AssertMacroExpansion(macros: [String : Macro.Type], testModuleName: String
131132
BasicMacroExpansionContext(sharingWith: context, lexicalContext: [$0])
132133
}
133134
} catch {
134-
XCTFail("Operator folding on input source failed with error \(error)")
135+
Issue.record("Operator folding on input source failed with error \(error)")
135136
return
136137
}
137138
let expansionResult = expandedSourceFile.description
138139
if !context.diagnostics.contains(where: { $0.diagMessage.severity == .error }) {
139-
XCTAssertEqual(expansionResult, result, file: file, line: line)
140+
#expect(expansionResult == result, sourceLocation: sourceLocation)
140141
}
141142
for diagnostic in context.diagnostics {
142143
if !diagnostics.contains(where: { $0.matches(diagnostic) }) {
143-
XCTFail("Produced extra diagnostic:\n\(diagnostic._assertionDescription)", file: file, line: line)
144+
Issue.record("Produced extra diagnostic:\n\(diagnostic._assertionDescription)", sourceLocation: sourceLocation)
144145
} else {
145146
let location = context.location(of: diagnostic.node, at: .afterLeadingTrivia, filePathMode: .fileID)
146-
XCTAssertNotNil(location, "Produced diagnostic without attached source information:\n\(diagnostic._assertionDescription)", file: file, line: line)
147+
#expect(location != nil, "Produced diagnostic without attached source information:\n\(diagnostic._assertionDescription)", sourceLocation: sourceLocation)
147148
}
148149
}
149150
for diagnostic in diagnostics {
150151
if !context.diagnostics.contains(where: { diagnostic.matches($0) }) {
151-
XCTFail("Failed to produce diagnostic:\n\(diagnostic._assertionDescription)", file: file, line: line)
152+
Issue.record("Failed to produce diagnostic:\n\(diagnostic._assertionDescription)", sourceLocation: sourceLocation)
152153
}
153154
}
154155
}
155156

156-
func AssertPredicateExpansion(_ source: String, _ result: String = "", diagnostics: Set<DiagnosticTest> = [], file: StaticString = #filePath, line: UInt = #line) {
157+
func AssertPredicateExpansion(_ source: String, _ result: String = "", diagnostics: Set<DiagnosticTest> = [], sourceLocation: Testing.SourceLocation = #_sourceLocation) {
157158
AssertMacroExpansion(
158159
macros: ["Predicate": PredicateMacro.self],
159160
source,
160161
result,
161162
diagnostics: diagnostics,
162-
file: file,
163-
line: line
163+
sourceLocation: sourceLocation
164164
)
165165
AssertMacroExpansion(
166166
macros: ["Expression" : FoundationMacros.ExpressionMacro.self],
167-
source.replacing("#Predicate", with: "#Expression"),
168-
result.replacing(".Predicate", with: ".Expression"),
167+
source._replacing("#Predicate", with: "#Expression"),
168+
result._replacing(".Predicate", with: ".Expression"),
169169
diagnostics: Set(diagnostics.map(\.mappedToExpression)),
170-
file: file,
171-
line: line
170+
sourceLocation: sourceLocation
172171
)
173172
}
173+
174+
extension String {
175+
func _replacing(_ text: String, with other: String) -> Self {
176+
if #available(macOS 13.0, *) {
177+
// Use the stdlib API if available
178+
self.replacing(text, with: other)
179+
} else {
180+
// Use the Foundation API on older OSes
181+
self.replacingOccurrences(of: text, with: other, options: [.literal])
182+
}
183+
}
184+
}

Tests/FoundationMacrosTests/PredicateMacroBasicTests.swift

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

13-
import XCTest
13+
import Testing
1414

15-
final class PredicateMacroBasicTests: XCTestCase {
16-
func testSimple() {
15+
@Suite("#Predicate Macro Basics")
16+
private struct PredicateMacroBasicTests {
17+
@Test func simple() {
1718
AssertPredicateExpansion(
1819
"""
1920
#Predicate<Object> { input in
@@ -30,7 +31,7 @@ final class PredicateMacroBasicTests: XCTestCase {
3031
)
3132
}
3233

33-
func testImplicitReturn() {
34+
@Test func implicitReturn() {
3435
AssertPredicateExpansion(
3536
"""
3637
#Predicate<Object> { input in
@@ -47,7 +48,7 @@ final class PredicateMacroBasicTests: XCTestCase {
4748
)
4849
}
4950

50-
func testInferredGenerics() {
51+
@Test func inferredGenerics() {
5152
AssertPredicateExpansion(
5253
"""
5354
#Predicate { input in
@@ -64,7 +65,7 @@ final class PredicateMacroBasicTests: XCTestCase {
6465
)
6566
}
6667

67-
func testShorthandArgumentNames() {
68+
@Test func shorthandArgumentNames() {
6869
AssertPredicateExpansion(
6970
"""
7071
#Predicate<Object> {
@@ -81,7 +82,7 @@ final class PredicateMacroBasicTests: XCTestCase {
8182
)
8283
}
8384

84-
func testExplicitClosureArgumentTypes() {
85+
@Test func explicitClosureArgumentTypes() {
8586
AssertPredicateExpansion(
8687
"""
8788
#Predicate<Int, String> { (a: Int, b: String) -> Bool in
@@ -98,7 +99,7 @@ final class PredicateMacroBasicTests: XCTestCase {
9899
)
99100
}
100101

101-
func testDiagnoseMissingTrailingClosure() {
102+
@Test func diagnoseMissingTrailingClosure() {
102103
AssertPredicateExpansion(
103104
"""
104105
#Predicate
@@ -141,7 +142,7 @@ final class PredicateMacroBasicTests: XCTestCase {
141142
)
142143
}
143144

144-
func testKeyPath() {
145+
@Test func keyPath() {
145146
AssertPredicateExpansion(
146147
"""
147148
#Predicate<Object> {
@@ -192,7 +193,7 @@ final class PredicateMacroBasicTests: XCTestCase {
192193
)
193194
}
194195

195-
func testComments() {
196+
@Test func comments() {
196197
AssertPredicateExpansion(
197198
"""
198199
// comment

Tests/FoundationMacrosTests/PredicateMacroFunctionCallTests.swift

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

13-
import XCTest
13+
import Testing
1414

15-
final class PredicateMacroFunctionCallTests: XCTestCase {
16-
func testSubscript() {
15+
@Suite("#Predicate Macro Function Calls")
16+
private struct PredicateMacroFunctionCallTests {
17+
@Test func `subscript`() {
1718
AssertPredicateExpansion(
1819
"""
1920
#Predicate<Object> { input in
@@ -96,7 +97,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
9697
)
9798
}
9899

99-
func testContains() {
100+
@Test func contains() {
100101
AssertPredicateExpansion(
101102
"""
102103
#Predicate<Object, Object> { inputA, inputB in
@@ -130,7 +131,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
130131
)
131132
}
132133

133-
func testContainsWhere() {
134+
@Test func containsWhere() {
134135
AssertPredicateExpansion(
135136
"""
136137
#Predicate<Object> { inputA in
@@ -175,7 +176,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
175176
)
176177
}
177178

178-
func testAllSatisfy() {
179+
@Test func allSatisfy() {
179180
AssertPredicateExpansion(
180181
"""
181182
#Predicate<Object> { inputA in
@@ -220,7 +221,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
220221
)
221222
}
222223

223-
func testFilter() {
224+
@Test func filter() {
224225
AssertPredicateExpansion(
225226
"""
226227
#Predicate<Object> { inputA in
@@ -347,7 +348,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
347348
)
348349
}
349350

350-
func testStartsWith() {
351+
@Test func startsWith() {
351352
AssertPredicateExpansion(
352353
"""
353354
#Predicate<Object> { inputA in
@@ -386,7 +387,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
386387
)
387388
}
388389

389-
func testMin() {
390+
@Test func min() {
390391
AssertPredicateExpansion(
391392
"""
392393
#Predicate<[Int]> { inputA in
@@ -406,7 +407,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
406407
)
407408
}
408409

409-
func testMax() {
410+
@Test func max() {
410411
AssertPredicateExpansion(
411412
"""
412413
#Predicate<[Int]> { inputA in
@@ -426,7 +427,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
426427
)
427428
}
428429

429-
func testLocalizedStandardContains() {
430+
@Test func localizedStandardContains() {
430431
AssertPredicateExpansion(
431432
"""
432433
#Predicate<String> { inputA in
@@ -462,7 +463,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
462463
)
463464
}
464465

465-
func testLocalizedStandardCompare() {
466+
@Test func localizedStandardCompare() {
466467
AssertPredicateExpansion(
467468
"""
468469
#Predicate<String> { inputA in
@@ -516,7 +517,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
516517
)
517518
}
518519

519-
func testCaseInsensitiveCompare() {
520+
@Test func caseInsensitiveCompare() {
520521
AssertPredicateExpansion(
521522
"""
522523
#Predicate<String> { inputA in
@@ -535,7 +536,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
535536
}
536537

537538
#if FOUNDATION_FRAMEWORK
538-
func testEvaluate() {
539+
@Test func evaluate() {
539540
AssertPredicateExpansion(
540541
"""
541542
#Predicate<String> { input in
@@ -584,7 +585,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
584585
}
585586
#endif
586587

587-
func testDiagnoseUnsupportedFunction() {
588+
@Test func diagnoseUnsupportedFunction() {
588589
AssertPredicateExpansion(
589590
"""
590591
#Predicate<Object> { inputA in

0 commit comments

Comments
 (0)