Skip to content

Add handwritten convenience initializers #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

extension BooleanLiteralExpr {
public init(_ value: Bool) {
self.init(booleanLiteral: value ? Tokens.true : Tokens.false)
}
}

extension BooleanLiteralExpr: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self.init(value)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

extension FloatLiteralExpr {
public init(_ value: Float) {
self.init(floatingDigits: String(value))
}
}

extension FloatLiteralExpr: ExpressibleByFloatLiteral {
public init(floatLiteral value: Float) {
self.init(value)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

extension IntegerLiteralExpr {
public init(_ value: Int) {
self.init(digits: String(value))
}
}

extension IntegerLiteralExpr: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Int) {
self.init(value)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

extension StringLiteralExpr {
public init(_ value: String) {
let content = SyntaxFactory.makeToken(TokenKind.stringSegment(value), presence: .present)
let segment = StringSegment(content: content)
let segments = StringLiteralSegments([segment])

self.init(openQuote: Tokens.stringQuote,
segments: segments,
closeQuote: Tokens.stringQuote)
}
}

extension StringLiteralExpr: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}
6 changes: 5 additions & 1 deletion Tests/SwiftSyntaxBuilderTest/BooleanLiteralTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ final class BooleanLiteralTests: XCTestCase {

let testCases: [UInt: (BooleanLiteralExpr, String)] = [
#line: (BooleanLiteralExpr(booleanLiteral: Tokens.true), "␣true "),
#line: (BooleanLiteralExpr(booleanLiteral: Tokens.false), "␣false ")
#line: (BooleanLiteralExpr(booleanLiteral: Tokens.false), "␣false "),
#line: (BooleanLiteralExpr(true), "␣true "),
#line: (BooleanLiteralExpr(false), "␣false "),
#line: (true, "␣true "),
#line: (false, "␣false ")
]

for (line, testCase) in testCases {
Expand Down
6 changes: 5 additions & 1 deletion Tests/SwiftSyntaxBuilderTest/FloatLiteralTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ final class FloatLiteralTests: XCTestCase {
#line: (FloatLiteralExpr(floatingDigits: Tokens.floatingLiteral(String(123.321))), "␣123.321"),
#line: (FloatLiteralExpr(floatingDigits: Tokens.floatingLiteral(String(-123.321))), "␣-123.321"),
#line: (FloatLiteralExpr(floatingDigits: "2_123.321"), "␣2_123.321"),
#line: (FloatLiteralExpr(floatingDigits: "-2_123.321"), "␣-2_123.321")
#line: (FloatLiteralExpr(floatingDigits: "-2_123.321"), "␣-2_123.321"),
#line: (FloatLiteralExpr(2_123.321), "␣2123.321"),
#line: (FloatLiteralExpr(-2_123.321), "␣-2123.321"),
#line: (2_123.321, "␣2123.321"),
#line: (-2_123.321, "␣-2123.321")
]

for (line, testCase) in testCases {
Expand Down
6 changes: 5 additions & 1 deletion Tests/SwiftSyntaxBuilderTest/IntegerLiteralTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ final class IntegerLiteralTests: XCTestCase {
#line: (IntegerLiteralExpr(digits: Tokens.integerLiteral(String(123))), "␣123"),
#line: (IntegerLiteralExpr(digits: Tokens.integerLiteral(String(-123))), "␣-123"),
#line: (IntegerLiteralExpr(digits: "1_000"), "␣1_000"),
#line: (IntegerLiteralExpr(digits: "-1_000"), "␣-1_000")
#line: (IntegerLiteralExpr(digits: "-1_000"), "␣-1_000"),
#line: (IntegerLiteralExpr(1_000), "␣1000"),
#line: (IntegerLiteralExpr(-1_000), "␣-1000"),
#line: (1_000, "␣1000"),
#line: (-1_000, "␣-1000")
]

for (line, testCase) in testCases {
Expand Down
23 changes: 22 additions & 1 deletion Tests/SwiftSyntaxBuilderTest/StringLiteralTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class StringLiteralTests: XCTestCase {
let leadingTrivia = Trivia.garbageText("␣")
let testCases: [UInt: (String, String)] = [
#line: ("", #"␣"""#),
#line: ("asdf", #"␣"asdf""#),
#line: ("asdf", #"␣"asdf""#)
]

for (line, testCase) in testCases {
Expand All @@ -26,4 +26,25 @@ final class StringLiteralTests: XCTestCase {
XCTAssertEqual(text, expected, line: line)
}
}

func testStringLiteralConvenienceInitializers() {
let leadingTrivia = Trivia.garbageText("␣")
let testCases: [UInt: (StringLiteralExpr, String)] = [
#line: (StringLiteralExpr(""), #"␣"""#),
#line: (StringLiteralExpr("asdf"), #"␣"asdf""#),
#line: ("", #"␣"""#),
#line: ("asdf", #"␣"asdf""#),
]

for (line, testCase) in testCases {
let (builder, expected) = testCase

let syntax = builder.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)

var text = ""
syntax.write(to: &text)

XCTAssertEqual(text, expected, line: line)
}
}
}