File tree Expand file tree Collapse file tree 2 files changed +73
-0
lines changed
Sources/SwiftSyntaxBuilder
Tests/SwiftSyntaxBuilderTest Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ import SwiftSyntax
14
+
15
+ public extension IfStmt {
16
+ /// A convenience initializer that uses builder closures to express an
17
+ /// if body, potentially with a second trailing builder closure for an else
18
+ /// body.
19
+ init (
20
+ leadingTrivia: Trivia = [ ] ,
21
+ conditions: ExpressibleAsConditionElementList ,
22
+ @CodeBlockItemListBuilder body: ( ) -> ExpressibleAsCodeBlockItemList ,
23
+ @CodeBlockItemListBuilder elseBody: ( ) -> ExpressibleAsCodeBlockItemList ? = { nil }
24
+ ) {
25
+ let generatedElseBody = elseBody ( )
26
+ self . init (
27
+ leadingTrivia: leadingTrivia,
28
+ conditions: conditions,
29
+ body: body ( ) ,
30
+ elseKeyword: generatedElseBody == nil ? nil : SyntaxFactory . makeElseKeyword ( leadingTrivia: . space, trailingTrivia: [ ] ) ,
31
+ elseBody: generatedElseBody. map { CodeBlock ( statements: $0) }
32
+ )
33
+ }
34
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments