@@ -70,6 +70,43 @@ enum AssertionMapper {
7070extension AssertionMapper {
7171
7272 fileprivate static func replace( source: String , key: String , value: String ) -> String {
73- return source. replacingOccurrences ( of: " ${{ \( key) }} " , with: value)
73+ return removeAllWhitespaceInsidePlaceholders ( in: source)
74+ . replacingOccurrences ( of: " ${{ \( key) }} " , with: value)
75+ }
76+
77+ fileprivate static func removeAllWhitespaceInsidePlaceholders( in input: String ) -> String {
78+ let pattern = " \\ $ \\ { \\ { \\ s*(.*?) \\ s* \\ } \\ } "
79+ let regex = try ! NSRegularExpression ( pattern: pattern, options: [ ] )
80+
81+ let nsrange = NSRange ( input. startIndex..< input. endIndex, in: input)
82+ var result = input
83+ var offset = 0
84+
85+ regex. enumerateMatches ( in: input, options: [ ] , range: nsrange) { match, _, _ in
86+ guard let match = match, match. numberOfRanges == 2 ,
87+ let innerRange = Range ( match. range ( at: 1 ) , in: input)
88+ else {
89+ return
90+ }
91+
92+ let originalInner = String ( input [ innerRange] )
93+ let cleaned = originalInner. replacingOccurrences (
94+ of: #"\s+"# ,
95+ with: " " ,
96+ options: . regularExpression
97+ )
98+
99+ // Rebuild the full match range considering offset due to replacements
100+ let fullMatchRange = match. range ( at: 0 )
101+ if let rangeToReplace = Range (
102+ NSRange ( location: fullMatchRange. location + offset, length: fullMatchRange. length) ,
103+ in: result
104+ ) {
105+ result. replaceSubrange ( rangeToReplace, with: " ${{ \( cleaned) }} " )
106+ offset += " ${{ \( cleaned) }} " . count - fullMatchRange. length
107+ }
108+ }
109+
110+ return result
74111 }
75112}
0 commit comments