Skip to content

Commit 3781538

Browse files
committed
Add literal expression builders
1 parent e4d795d commit 3781538

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 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 struct IntegerLiteral: ExprBuildable {
16+
let value: Int
17+
18+
public init(_ value: Int) {
19+
self.value = value
20+
}
21+
22+
public func buildExpr(format: Format, leadingTrivia: Trivia) -> ExprSyntax {
23+
SyntaxFactory.makeIntegerLiteralExpr(
24+
digits: SyntaxFactory.makeIntegerLiteral(String(value))
25+
)
26+
}
27+
}
28+
29+
extension IntegerLiteral: ExpressibleByIntegerLiteral {
30+
public init(integerLiteral value: Int) {
31+
self.init(value)
32+
}
33+
}
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 - 2019 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 struct StringLiteral: ExprBuildable {
16+
let value: String
17+
18+
public init(_ value: String) {
19+
self.value = value
20+
}
21+
22+
public func buildExpr(format: Format, leadingTrivia: Trivia) -> ExprSyntax {
23+
SyntaxFactory.makeStringLiteralExpr(value)
24+
}
25+
}
26+
27+
extension StringLiteral: ExpressibleByStringLiteral {
28+
public init(stringLiteral value: String) {
29+
self.init(value)
30+
}
31+
}
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+
@testable import SwiftSyntaxBuilder
5+
6+
final class IntegerLiteralTests: XCTestCase {
7+
let format = Format(indentWidth: 2).indented()
8+
9+
func testIntegerLiteral() {
10+
let testCases: [UInt: (IntegerLiteral, String)] = [
11+
#line: (IntegerLiteral(123), "123"),
12+
#line: (IntegerLiteral(-123), "-123"),
13+
#line: (123, "123"),
14+
#line: (-123, "-123"),
15+
]
16+
17+
for (line, testCase) in testCases {
18+
let (builder, expected) = testCase
19+
let syntax = builder.buildSyntax(format: format, leadingTrivia: .zero)
20+
21+
var text = ""
22+
syntax.write(to: &text)
23+
24+
XCTAssertEqual(text, expected, line: line)
25+
}
26+
}
27+
}
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+
@testable import SwiftSyntaxBuilder
5+
6+
final class StringLiteralTests: XCTestCase {
7+
let format = Format(indentWidth: 2).indented()
8+
9+
func testStringLiteral() {
10+
let testCases: [UInt: (StringLiteral, String)] = [
11+
#line: (StringLiteral(""), "\"\""),
12+
#line: (StringLiteral("asdf"), #""asdf""#),
13+
#line: ("", "\"\""),
14+
#line: ("asdf", #""asdf""#),
15+
]
16+
17+
for (line, testCase) in testCases {
18+
let (builder, expected) = testCase
19+
let syntax = builder.buildSyntax(format: format, leadingTrivia: .zero)
20+
21+
var text = ""
22+
syntax.write(to: &text)
23+
24+
XCTAssertEqual(text, expected, line: line)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)