Skip to content

Commit 7c03c62

Browse files
committed
Add tests for if-else statement
1 parent abfb866 commit 7c03c62

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import XCTest
2+
import SwiftSyntax
3+
import SwiftSyntaxBuilder
4+
5+
final class IfStmtTests: XCTestCase {
6+
func testEmptyIfStmt() {
7+
// Use the convenience initializer from IfStmtConvenienceInitializers. This is
8+
// disambiguated by the absence of a labelName parameter and the use of a
9+
// trailing closure.
10+
let buildable = IfStmt(conditions: ExprList([BooleanLiteralExpr(false)])) {}
11+
let syntax = buildable.buildSyntax(format: Format())
12+
XCTAssertEqual(syntax.description, """
13+
if false {
14+
}
15+
""")
16+
}
17+
18+
func testIfElseStmt() {
19+
// Use the convenience initializer from IfStmtConvenienceInitializers
20+
// with an else branch expressed by a second trailing closure.
21+
let buildable = IfStmt(conditions: ExprList([BooleanLiteralExpr(true)])) {
22+
FunctionCallExpr("print") {
23+
TupleExprElement(expression: StringLiteralExpr("Hello from the if-branch!"))
24+
}
25+
} elseBody: {
26+
FunctionCallExpr("print") {
27+
TupleExprElement(expression: StringLiteralExpr("Hello from the else-branch!"))
28+
}
29+
}
30+
let syntax = buildable.buildSyntax(format: Format())
31+
XCTAssertEqual(syntax.description, """
32+
if true {
33+
print("Hello from the if-branch!")
34+
} else {
35+
print("Hello from the else-branch!")
36+
}
37+
""")
38+
}
39+
}

0 commit comments

Comments
 (0)