10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
- import XCTest
13
+ import Testing
14
+ import Foundation
14
15
import FoundationMacros
15
16
import SwiftSyntax
16
17
import SwiftSyntaxMacros
@@ -46,9 +47,9 @@ struct DiagnosticTest : ExpressibleByStringLiteral, Hashable, CustomStringConver
46
47
47
48
var mappedToExpression : Self {
48
49
DiagnosticTest (
49
- message. replacing ( " Predicate " , with: " Expression " ) . replacing ( " predicate " , with: " expression " ) ,
50
+ message. _replacing ( " Predicate " , with: " Expression " ) . _replacing ( " predicate " , with: " expression " ) ,
50
51
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 " ) )
52
53
}
53
54
)
54
55
}
@@ -99,7 +100,7 @@ extension Diagnostic {
99
100
} else {
100
101
var result = " Message: \( debugDescription) \n Fix-Its: \n "
101
102
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 " ) ) "
103
104
}
104
105
return result
105
106
}
@@ -113,14 +114,14 @@ extension DiagnosticTest {
113
114
} else {
114
115
var result = " Message: \( message) \n Fix-Its: \n "
115
116
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 " ) ) "
117
118
}
118
119
return result
119
120
}
120
121
}
121
122
}
122
123
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 ) {
124
125
let origSourceFile = Parser . parse ( source: source)
125
126
let expandedSourceFile : Syntax
126
127
let context : BasicMacroExpansionContext
@@ -131,43 +132,53 @@ func AssertMacroExpansion(macros: [String : Macro.Type], testModuleName: String
131
132
BasicMacroExpansionContext ( sharingWith: context, lexicalContext: [ $0] )
132
133
}
133
134
} catch {
134
- XCTFail ( " Operator folding on input source failed with error \( error) " )
135
+ Issue . record ( " Operator folding on input source failed with error \( error) " )
135
136
return
136
137
}
137
138
let expansionResult = expandedSourceFile. description
138
139
if !context. diagnostics. contains ( where: { $0. diagMessage. severity == . error } ) {
139
- XCTAssertEqual ( expansionResult, result, file : file , line : line )
140
+ #expect ( expansionResult == result, sourceLocation : sourceLocation )
140
141
}
141
142
for diagnostic in context. diagnostics {
142
143
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 )
144
145
} else {
145
146
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 )
147
148
}
148
149
}
149
150
for diagnostic in diagnostics {
150
151
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 )
152
153
}
153
154
}
154
155
}
155
156
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 ) {
157
158
AssertMacroExpansion (
158
159
macros: [ " Predicate " : PredicateMacro . self] ,
159
160
source,
160
161
result,
161
162
diagnostics: diagnostics,
162
- file: file,
163
- line: line
163
+ sourceLocation: sourceLocation
164
164
)
165
165
AssertMacroExpansion (
166
166
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 " ) ,
169
169
diagnostics: Set ( diagnostics. map ( \. mappedToExpression) ) ,
170
- file: file,
171
- line: line
170
+ sourceLocation: sourceLocation
172
171
)
173
172
}
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
+ }
0 commit comments