Skip to content

Commit 7b3a8a3

Browse files
committed
Add convenience initializers for simple expr values
1 parent 1888022 commit 7b3a8a3

8 files changed

+182
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
import SwiftSyntax
14+
15+
extension BooleanLiteralExpr {
16+
public init(_ value: Bool) {
17+
self.init(booleanLiteral: value ? Tokens.true : Tokens.false)
18+
}
19+
}
20+
21+
extension BooleanLiteralExpr: ExpressibleByBooleanLiteral {
22+
public init(booleanLiteral value: Bool) {
23+
self.init(value)
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
import SwiftSyntax
14+
15+
extension FloatLiteralExpr {
16+
public init(_ value: Float) {
17+
self.init(floatingDigits: SyntaxFactory.makeFloatingLiteral(String(value)))
18+
}
19+
}
20+
21+
extension FloatLiteralExpr: ExpressibleByFloatLiteral {
22+
public init(floatLiteral value: Float) {
23+
self.init(value)
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
import SwiftSyntax
14+
15+
extension IntegerLiteralExpr {
16+
public init(_ value: Int) {
17+
self.init(digits: SyntaxFactory.makeIntegerLiteral(String(value)))
18+
}
19+
}
20+
21+
extension IntegerLiteralExpr: ExpressibleByIntegerLiteral {
22+
public init(integerLiteral value: Int) {
23+
self.init(value)
24+
}
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
import SwiftSyntax
14+
15+
extension StringLiteralExpr {
16+
public init(_ value: String) {
17+
let content = SyntaxFactory.makeToken(TokenKind.stringSegment(value), presence: .present)
18+
let segment = StringSegment(content: content)
19+
let segments = StringLiteralSegments([segment])
20+
21+
self.init(openQuote: Tokens.stringQuote,
22+
segments: segments,
23+
closeQuote: Tokens.stringQuote)
24+
}
25+
}
26+
27+
extension StringLiteralExpr: ExpressibleByStringLiteral {
28+
public init(stringLiteral value: String) {
29+
self.init(value)
30+
}
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import XCTest
2+
import SwiftSyntax
3+
4+
import SwiftSyntaxBuilder
5+
6+
final class BooleanLiteralTests: XCTestCase {
7+
func testBooleanLiteral() {
8+
let leadingTrivia = Trivia.garbageText("")
9+
10+
let testCases: [UInt: (BooleanLiteralExpr, String)] = [
11+
#line: (BooleanLiteralExpr(booleanLiteral: Tokens.true), "␣true "),
12+
#line: (BooleanLiteralExpr(booleanLiteral: Tokens.false), "␣false "),
13+
#line: (BooleanLiteralExpr(true), "␣true "),
14+
#line: (BooleanLiteralExpr(false), "␣false "),
15+
#line: (false, "␣false "),
16+
#line: (true, "␣true "),
17+
]
18+
19+
for (line, testCase) in testCases {
20+
let (builder, expected) = testCase
21+
let syntax = builder.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
22+
23+
var text = ""
24+
syntax.write(to: &text)
25+
26+
XCTAssertEqual(text, expected, line: line)
27+
}
28+
}
29+
}
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 FloatLiteralTests: XCTestCase {
7+
func testFloatLiteral() {
8+
let leadingTrivia = Trivia.garbageText("")
9+
10+
let testCases: [UInt: (FloatLiteralExpr, String)] = [
11+
#line: (FloatLiteralExpr(floatingDigits: SyntaxFactory.makeFloatingLiteral(String(123.321))), "␣123.321"),
12+
#line: (FloatLiteralExpr(floatingDigits: SyntaxFactory.makeFloatingLiteral(String(-123.321))), "␣-123.321"),
13+
#line: (123.321, "␣123.321"),
14+
#line: (-123.321, "␣-123.321"),
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+
}

Tests/SwiftSyntaxBuilderTest/IntegerLiteralTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ final class IntegerLiteralTests: XCTestCase {
1010
let testCases: [UInt: (IntegerLiteralExpr, String)] = [
1111
#line: (IntegerLiteralExpr(digits: SyntaxFactory.makeIntegerLiteral(String(123))), "␣123"),
1212
#line: (IntegerLiteralExpr(digits: SyntaxFactory.makeIntegerLiteral(String(-123))), "␣-123"),
13+
#line: (123, "␣123"),
14+
#line: (-123, "␣-123"),
1315
]
1416

1517
for (line, testCase) in testCases {

Tests/SwiftSyntaxBuilderTest/StringLiteralTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,22 @@ final class StringLiteralTests: XCTestCase {
2626
XCTAssertEqual(text, expected, line: line)
2727
}
2828
}
29+
30+
func testStringConvenienceInitializers() {
31+
let leadingTrivia = Trivia.garbageText("")
32+
let testCases: [UInt: (StringLiteralExpr, String)] = [
33+
#line: ("", #"␣"""#),
34+
#line: ("asdf", #"␣"asdf""#),
35+
]
36+
37+
for (line, testCase) in testCases {
38+
let (builder, expected) = testCase
39+
let syntax = builder.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
40+
41+
var text = ""
42+
syntax.write(to: &text)
43+
44+
XCTAssertEqual(text, expected, line: line)
45+
}
46+
}
2947
}

0 commit comments

Comments
 (0)