Skip to content
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

Add convenience methods to read the value of float and int literals #148

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions Sources/SwiftSyntax/SyntaxConvenienceMethods.swift
@@ -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 {
ahoppen marked this conversation as resolved.
Show resolved Hide resolved
// 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
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
@@ -0,0 +1,22 @@
import XCTest
import SwiftSyntax

public class SyntaxConvenienceMethodsTestCase: XCTestCase {

public static let allTests = [
ahoppen marked this conversation as resolved.
Show resolved Hide resolved
("testFloatingValue", testFloatingValue),
("testIntegerValue", testIntegerValue),
]

public func testFloatingValue() {
let digits = SyntaxFactory.makeFloatingLiteral("5.38")
ahoppen marked this conversation as resolved.
Show resolved Hide resolved
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
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",
]
}
}