Skip to content
Closed
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
25 changes: 25 additions & 0 deletions Sources/SwiftSyntax/SyntaxConvenienceMethods.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===- SyntaxConvenienceMethods.swift - Convenience funcs for syntax nodes ===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 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
//
//===----------------------------------------------------------------------===//

public extension FloatLiteralExprSyntax {
var floatingValue: Double {
// Parsing a floating literal as a Double should never fail
return Double(self.floatingDigits.text)!
}
}

public extension IntegerLiteralExprSyntax {
var integerValue: Int {
// Parsing an integer literal as an Int should never fail
return Int(self.digits.text)!
}
}
1 change: 1 addition & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ XCTMain({ () -> [XCTestCaseEntry] in
testCase(SyntaxTreeModifierTests.allTests),
testCase(TriviaTests.allTests),
testCase(CustomReflectableTests.allTests),
testCase(SyntaxConvenienceMethodsTestCase.allTests)
]
return testCases
}())
22 changes: 22 additions & 0 deletions Tests/SwiftSyntaxTest/SyntaxConvenienceMethodsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import XCTest
import SwiftSyntax

public class SyntaxConvenienceMethodsTestCase: XCTestCase {

public static let allTests = [
("testFloatingValue", testFloatingValue),
("testIntegerValue", testIntegerValue),
]

public func testFloatingValue() {
let digits = SyntaxFactory.makeFloatingLiteral("5.38")
let literalExpr = SyntaxFactory.makeFloatLiteralExpr(floatingDigits: digits)
XCTAssertEqual(literalExpr.floatingValue, 5.38)
}

public func testIntegerValue() {
let digits = SyntaxFactory.makeIntegerLiteral("42")
let literalExpr = SyntaxFactory.makeIntegerLiteralExpr(digits: digits)
XCTAssertEqual(literalExpr.integerValue, 42)
}
}
3 changes: 2 additions & 1 deletion utils/group.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"SyntaxCollections.swift",
"SourcePresence.swift",
"SyntaxChildren.swift",
"SyntaxConvenienceMethods.swift",
],
"Utils": [
"SyntaxBuilders.swift",
Expand Down Expand Up @@ -39,4 +40,4 @@
"Internal": [
"AtomicCounter.swift",
]
}
}