Skip to content

Commit 5736dff

Browse files
committed
Add convenience initializer for TernaryExpr
1 parent 71f1af4 commit 5736dff

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
extension TernaryExpr {
16+
public init(
17+
if condition: ExpressibleAsExprBuildable,
18+
then firstChoice: ExpressibleAsExprBuildable,
19+
else secondChoice: ExpressibleAsExprBuildable
20+
) {
21+
self.init(
22+
conditionExpression: condition,
23+
questionMark: .infixQuestionMarkToken(leadingTrivia: .space, trailingTrivia: .space),
24+
firstChoice: firstChoice,
25+
colonMark: .colonToken(leadingTrivia: .space),
26+
secondChoice: secondChoice
27+
)
28+
}
29+
}
30+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import XCTest
2+
import SwiftSyntax
3+
import SwiftSyntaxBuilder
4+
5+
final class TernaryExprTests: XCTestCase {
6+
func testTernaryExpr() {
7+
let buildable = TernaryExpr(if: BooleanLiteralExpr(true), then: "a", else: "b")
8+
let syntax = buildable.buildSyntax(format: Format())
9+
XCTAssertEqual(syntax.description, """
10+
true ? a : b
11+
""")
12+
}
13+
}
14+

0 commit comments

Comments
 (0)