Skip to content

Commit 574288c

Browse files
authored
Merge pull request #291 from kimdv/kimdv/add-handwritten-convenience-initializers
Add handwritten convenience initializers
2 parents 6d5968e + e5d00df commit 574288c

8 files changed

+143
-4
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: 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: 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+
}

Tests/SwiftSyntaxBuilderTest/BooleanLiteralTests.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ final class BooleanLiteralTests: XCTestCase {
99

1010
let testCases: [UInt: (BooleanLiteralExpr, String)] = [
1111
#line: (BooleanLiteralExpr(booleanLiteral: Tokens.true), "␣true "),
12-
#line: (BooleanLiteralExpr(booleanLiteral: Tokens.false), "␣false ")
12+
#line: (BooleanLiteralExpr(booleanLiteral: Tokens.false), "␣false "),
13+
#line: (BooleanLiteralExpr(true), "␣true "),
14+
#line: (BooleanLiteralExpr(false), "␣false "),
15+
#line: (true, "␣true "),
16+
#line: (false, "␣false ")
1317
]
1418

1519
for (line, testCase) in testCases {

Tests/SwiftSyntaxBuilderTest/FloatLiteralTests.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ final class FloatLiteralTests: XCTestCase {
1111
#line: (FloatLiteralExpr(floatingDigits: Tokens.floatingLiteral(String(123.321))), "␣123.321"),
1212
#line: (FloatLiteralExpr(floatingDigits: Tokens.floatingLiteral(String(-123.321))), "␣-123.321"),
1313
#line: (FloatLiteralExpr(floatingDigits: "2_123.321"), "␣2_123.321"),
14-
#line: (FloatLiteralExpr(floatingDigits: "-2_123.321"), "␣-2_123.321")
14+
#line: (FloatLiteralExpr(floatingDigits: "-2_123.321"), "␣-2_123.321"),
15+
#line: (FloatLiteralExpr(2_123.321), "␣2123.321"),
16+
#line: (FloatLiteralExpr(-2_123.321), "␣-2123.321"),
17+
#line: (2_123.321, "␣2123.321"),
18+
#line: (-2_123.321, "␣-2123.321")
1519
]
1620

1721
for (line, testCase) in testCases {

Tests/SwiftSyntaxBuilderTest/IntegerLiteralTests.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ final class IntegerLiteralTests: XCTestCase {
1111
#line: (IntegerLiteralExpr(digits: Tokens.integerLiteral(String(123))), "␣123"),
1212
#line: (IntegerLiteralExpr(digits: Tokens.integerLiteral(String(-123))), "␣-123"),
1313
#line: (IntegerLiteralExpr(digits: "1_000"), "␣1_000"),
14-
#line: (IntegerLiteralExpr(digits: "-1_000"), "␣-1_000")
14+
#line: (IntegerLiteralExpr(digits: "-1_000"), "␣-1_000"),
15+
#line: (IntegerLiteralExpr(1_000), "␣1000"),
16+
#line: (IntegerLiteralExpr(-1_000), "␣-1000"),
17+
#line: (1_000, "␣1000"),
18+
#line: (-1_000, "␣-1000")
1519
]
1620

1721
for (line, testCase) in testCases {

Tests/SwiftSyntaxBuilderTest/StringLiteralTests.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ final class StringLiteralTests: XCTestCase {
88
let leadingTrivia = Trivia.garbageText("")
99
let testCases: [UInt: (String, String)] = [
1010
#line: ("", #"␣"""#),
11-
#line: ("asdf", #"␣"asdf""#),
11+
#line: ("asdf", #"␣"asdf""#)
1212
]
1313

1414
for (line, testCase) in testCases {
@@ -26,4 +26,25 @@ final class StringLiteralTests: XCTestCase {
2626
XCTAssertEqual(text, expected, line: line)
2727
}
2828
}
29+
30+
func testStringLiteralConvenienceInitializers() {
31+
let leadingTrivia = Trivia.garbageText("")
32+
let testCases: [UInt: (StringLiteralExpr, String)] = [
33+
#line: (StringLiteralExpr(""), #"␣"""#),
34+
#line: (StringLiteralExpr("asdf"), #"␣"asdf""#),
35+
#line: ("", #"␣"""#),
36+
#line: ("asdf", #"␣"asdf""#),
37+
]
38+
39+
for (line, testCase) in testCases {
40+
let (builder, expected) = testCase
41+
42+
let syntax = builder.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
43+
44+
var text = ""
45+
syntax.write(to: &text)
46+
47+
XCTAssertEqual(text, expected, line: line)
48+
}
49+
}
2950
}

0 commit comments

Comments
 (0)