|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 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 | +@_spi(RawSyntax) import SwiftSyntax |
| 14 | +import XCTest |
| 15 | + |
| 16 | +class IdentifierTests: XCTestCase { |
| 17 | + public func testIdentifierInit() { |
| 18 | + let someToken = TokenSyntax(stringLiteral: "someToken") |
| 19 | + XCTAssertNotNil(Identifier(someToken)) |
| 20 | + |
| 21 | + let nonIdentifierToken = DeclSyntax("let a = 1").firstToken(viewMode: .all)! |
| 22 | + XCTAssertNil(Identifier(nonIdentifierToken)) |
| 23 | + } |
| 24 | + |
| 25 | + public func testName() { |
| 26 | + let basicToken = TokenSyntax(stringLiteral: "basicToken") |
| 27 | + XCTAssertEqual(Identifier(basicToken)?.name, "basicToken") |
| 28 | + |
| 29 | + let backtickedToken = TokenSyntax(stringLiteral: "`backtickedToken`") |
| 30 | + XCTAssertEqual(Identifier(backtickedToken)?.name, "backtickedToken") |
| 31 | + |
| 32 | + let multiBacktickedToken = TokenSyntax(stringLiteral: "```multiBacktickedToken```") |
| 33 | + XCTAssertEqual(Identifier(multiBacktickedToken)?.name, "``multiBacktickedToken``") |
| 34 | + |
| 35 | + let unicodeNormalizedToken = TokenSyntax(stringLiteral: "\u{e0}") // "a`" |
| 36 | + XCTAssertEqual(Identifier(unicodeNormalizedToken)?.name, "\u{61}\u{300}") // "à" |
| 37 | + } |
| 38 | + |
| 39 | + public func testIdentifier() { |
| 40 | + let token = TokenSyntax(stringLiteral: "sometoken") |
| 41 | + withExtendedLifetime(token) { token in |
| 42 | + XCTAssertEqual(token.identifier?.raw.name, SyntaxText("sometoken")) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public func testTokenSyntaxIdentifier() throws { |
| 47 | + let tokenSyntax = TokenSyntax(stringLiteral: "sometoken") |
| 48 | + XCTAssertEqual(tokenSyntax.identifier, Identifier(tokenSyntax)) |
| 49 | + |
| 50 | + let nonIdentifierToken = try XCTUnwrap(DeclSyntax("let a = 1").firstToken(viewMode: .all)) |
| 51 | + XCTAssertNil(nonIdentifierToken.identifier) |
| 52 | + } |
| 53 | +} |
0 commit comments