Skip to content

Commit e1beaa8

Browse files
committed
Add tests cases for failing multi line string
1 parent 83b85ae commit e1beaa8

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

Tests/SwiftSyntaxBuilderTest/StringLiteralTests.swift

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,159 @@ final class StringLiteralTests: XCTestCase {
170170
"""#
171171
)
172172
}
173+
174+
func testStringLiteralInExpr() {
175+
let buildable = ExprSyntax(
176+
#"""
177+
"Validation failures: \(nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n"))"
178+
"""#
179+
)
180+
181+
AssertBuildResult(
182+
buildable,
183+
#"""
184+
"Validation failures: \(nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n"))"
185+
"""#
186+
)
187+
}
188+
189+
func testStringSegmentWithCode() {
190+
let buildable = StringSegmentSyntax(content: .stringSegment(#"\(nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n"))"#))
191+
192+
AssertBuildResult(
193+
buildable,
194+
#"\(nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n"))"#
195+
)
196+
}
197+
198+
func testStringLiteralSegmentWithCode() {
199+
let buildable = StringLiteralSegmentsSyntax {
200+
StringSegmentSyntax(content: .stringSegment(#"Error validating child at index \(index) of \(nodeKind):"#), trailingTrivia: .newline)
201+
StringSegmentSyntax(content: .stringSegment(#"Node did not satisfy any node choice requirement."#), trailingTrivia: .newline)
202+
StringSegmentSyntax(content: .stringSegment(#"Validation failures:"#), trailingTrivia: .newline)
203+
StringSegmentSyntax(content: .stringSegment(#"\(nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n"))"#))
204+
}
205+
206+
AssertBuildResult(
207+
buildable,
208+
#"""
209+
Error validating child at index \(index) of \(nodeKind):
210+
Node did not satisfy any node choice requirement.
211+
Validation failures:
212+
\(nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n"))
213+
"""#
214+
)
215+
}
216+
217+
func testMultiLineStringWithResultBuilder() {
218+
let buildable = StringLiteralExprSyntax(
219+
openQuote: .multilineStringQuoteToken(trailingTrivia: .newline),
220+
segments: StringLiteralSegmentsSyntax {
221+
StringSegmentSyntax(content: .stringSegment(#"Error validating child at index \(index) of \(nodeKind):"#), trailingTrivia: .newline)
222+
StringSegmentSyntax(content: .stringSegment(#"Node did not satisfy any node choice requirement."#), trailingTrivia: .newline)
223+
StringSegmentSyntax(content: .stringSegment(#"Validation failures:"#), trailingTrivia: .newline)
224+
ExpressionSegmentSyntax(
225+
expressions: TupleExprElementListSyntax {
226+
TupleExprElementSyntax(expression: ExprSyntax(#"nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n"))"#))
227+
}
228+
)
229+
},
230+
closeQuote: .multilineStringQuoteToken(leadingTrivia: .newline)
231+
)
232+
233+
AssertBuildResult(
234+
buildable,
235+
#"""
236+
"""
237+
Error validating child at index \(index) of \(nodeKind):
238+
Node did not satisfy any node choice requirement.
239+
Validation failures:
240+
\(nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n"))
241+
"""
242+
"""#
243+
)
244+
}
245+
246+
func testMultiStringLiteralInExpr() {
247+
let buildable = ExprSyntax(
248+
#"""
249+
assertionFailure("""
250+
Error validating child at index \(index) of \(nodeKind):
251+
Node did not satisfy any node choice requirement.
252+
Validation failures:
253+
\(nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n"))
254+
""", file: file, line: line)
255+
"""#
256+
)
257+
258+
AssertBuildResult(
259+
buildable,
260+
#"""
261+
assertionFailure("""
262+
Error validating child at index \(index) of \(nodeKind):
263+
Node did not satisfy any node choice requirement.
264+
Validation failures:
265+
\(nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n"))
266+
""", file: file, line: line)
267+
"""#
268+
)
269+
}
270+
271+
func testMultiStringLiteralInIfExpr() {
272+
let buildable = ExprSyntax(
273+
#"""
274+
if true {
275+
assertionFailure("""
276+
Error validating child at index
277+
Node did not satisfy any node choice requirement.
278+
Validation failures:
279+
""")
280+
}
281+
"""#
282+
)
283+
284+
AssertBuildResult(
285+
buildable,
286+
#"""
287+
if true {
288+
assertionFailure("""
289+
Error validating child at index
290+
Node did not satisfy any node choice requirement.
291+
Validation failures:
292+
""")
293+
}
294+
"""#
295+
)
296+
}
297+
298+
func testMultiStringLiteralOnNewlineInIfExpr() {
299+
let buildable = ExprSyntax(
300+
#"""
301+
if true {
302+
assertionFailure(
303+
"""
304+
Error validating child at index
305+
Node did not satisfy any node choice requirement.
306+
Validation failures:
307+
"""
308+
)
309+
}
310+
"""#
311+
)
312+
313+
AssertBuildResult(
314+
buildable,
315+
#"""
316+
if true {
317+
assertionFailure(
318+
"""
319+
Error validating child at index
320+
Node did not satisfy any node choice requirement.
321+
Validation failures:
322+
"""
323+
)
324+
}
325+
"""#
326+
)
327+
}
173328
}

0 commit comments

Comments
 (0)