1010//
1111//===----------------------------------------------------------------------===//
1212
13- import XCTest
13+ import Testing
14+ import Foundation
1415import FoundationMacros
1516import SwiftSyntax
1617import 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) \n Fix-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) \n Fix-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+ }
0 commit comments