Skip to content

Commit 385f1f7

Browse files
authored
Merge pull request #315 from kimdv/kimdv/add-identifier-expr-convenience-initializers
Add convenience initializers for IdentifierExpr
2 parents a1574c3 + 85a7d28 commit 385f1f7

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 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+
extension IdentifierExpr {
14+
public init(_ identifier: String) {
15+
self.init(identifier: .identifier(identifier))
16+
}
17+
}
18+
19+
extension IdentifierExpr: ExpressibleByStringLiteral {
20+
public init(stringLiteral value: String) {
21+
self.init(value)
22+
}
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import XCTest
2+
import SwiftSyntax
3+
4+
import SwiftSyntaxBuilder
5+
6+
final class IdentifierExprTests: XCTestCase {
7+
func testStringLiteral() {
8+
let leadingTrivia = Trivia.garbageText("")
9+
10+
let testCases: [UInt: (IdentifierExpr, String)] = [
11+
#line: (IdentifierExpr(identifier: .identifier("Test")), "␣Test"),
12+
#line: (IdentifierExpr(stringLiteral: "Test"), "␣Test"),
13+
#line: (IdentifierExpr("Test"), "␣Test"),
14+
#line: ("Test", "␣Test")
15+
]
16+
17+
for (line, testCase) in testCases {
18+
let (builder, expected) = testCase
19+
let syntax = builder.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
20+
21+
var text = ""
22+
syntax.write(to: &text)
23+
24+
XCTAssertEqual(text, expected, line: line)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)