1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 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
import XCTest
class SyntaxTests: XCTestCase {
public func testHasError() {
XCTAssertTrue(TokenSyntax.keyword(.func, presence: .missing).hasError)
XCTAssertFalse(TokenSyntax.keyword(.func, presence: .present).hasError)
XCTAssertTrue(UnexpectedNodesSyntax([]).hasError)
XCTAssertTrue(MissingExprSyntax().hasError)
XCTAssertFalse(CodeBlockItemListSyntax([]).hasError)
XCTAssertTrue(
FunctionDeclSyntax(
funcKeyword: TokenSyntax.keyword(.func, presence: .missing),
name: .identifier("foo"),
signature: FunctionSignatureSyntax(parameterClause: FunctionParameterClauseSyntax(parameters: []))
).hasError
)
XCTAssertFalse(
FunctionDeclSyntax(
funcKeyword: TokenSyntax.keyword(.func, presence: .present),
name: .identifier("foo"),
signature: FunctionSignatureSyntax(parameterClause: FunctionParameterClauseSyntax(parameters: []))
).hasError
)
}
public func testDetached() {
let s = StructDeclSyntax(
structKeyword: .keyword(.struct),
name: .identifier("someStruct"),
memberBlock: MemberBlockSyntax(leftBrace: .leftBraceToken(), members: [], rightBrace: .rightBraceToken())
)
XCTAssertEqual(Syntax(s), s.memberBlock.parent)
XCTAssertNil(s.memberBlock.detached.parent)
}
public func testCasting() {
let integerExpr = IntegerLiteralExprSyntax(
literal: .integerLiteral("1", trailingTrivia: .space)
)
let expr = ExprSyntax(integerExpr)
let node = Syntax(expr)
XCTAssertTrue(expr.is(IntegerLiteralExprSyntax.self))
XCTAssertTrue(node.is(IntegerLiteralExprSyntax.self))
XCTAssertTrue(node.as(ExprSyntax.self)!.is(IntegerLiteralExprSyntax.self))
XCTAssertTrue(node.isProtocol(ExprSyntaxProtocol.self))
XCTAssertTrue(node.asProtocol(ExprSyntaxProtocol.self) is IntegerLiteralExprSyntax)
XCTAssertTrue(expr.asProtocol(ExprSyntaxProtocol.self) is IntegerLiteralExprSyntax)
XCTAssertTrue(expr.asProtocol(ExprSyntaxProtocol.self) as? IntegerLiteralExprSyntax == integerExpr)
XCTAssertFalse(node.isProtocol(BracedSyntax.self))
XCTAssertNil(node.asProtocol(BracedSyntax.self))
XCTAssertFalse(expr.isProtocol(BracedSyntax.self))
XCTAssertNil(expr.asProtocol(BracedSyntax.self))
let classDecl = CodeBlockSyntax(
leftBrace: TokenSyntax.leftBraceToken(),
statements: CodeBlockItemListSyntax([]),
rightBrace: TokenSyntax.rightBraceToken()
)
XCTAssertTrue(classDecl.isProtocol(BracedSyntax.self))
XCTAssertNotNil(classDecl.asProtocol(BracedSyntax.self))
let optNode: Syntax? = node
switch optNode?.as(SyntaxEnum.self) {
case .integerLiteralExpr: break
default: XCTFail("failed to convert to SyntaxEnum")
}
switch expr.as(ExprSyntaxEnum.self) {
case .integerLiteralExpr: break
default: XCTFail("failed to convert to ExprSyntaxEnum")
}
XCTAssertNil(ExprSyntax(nil as IntegerLiteralExprSyntax?))
XCTAssertEqual(ExprSyntax(integerExpr).as(IntegerLiteralExprSyntax.self)!, integerExpr)
}
public func testNodeType() {
let integerExpr = IntegerLiteralExprSyntax(
literal: TokenSyntax.integerLiteral("1", trailingTrivia: .space)
)
let expr = ExprSyntax(integerExpr)
let node = Syntax(expr)
XCTAssertTrue(integerExpr.syntaxNodeType == expr.syntaxNodeType)
XCTAssertTrue(integerExpr.syntaxNodeType == node.syntaxNodeType)
XCTAssertEqual("\(integerExpr.syntaxNodeType)", "IntegerLiteralExprSyntax")
}
public func testConstructFromSyntaxProtocol() {
let integerExpr = IntegerLiteralExprSyntax(
literal: .integerLiteral("1", trailingTrivia: .space)
)
XCTAssertEqual(Syntax(integerExpr), Syntax(fromProtocol: integerExpr as SyntaxProtocol))
XCTAssertEqual(Syntax(integerExpr), Syntax(fromProtocol: integerExpr as ExprSyntaxProtocol))
}
public func testPositions() {
let leading = Trivia(pieces: [.spaces(2)])
let trailing = Trivia(pieces: [.spaces(1)])
let funcKW = TokenSyntax.keyword(
.func,
leadingTrivia: leading,
trailingTrivia: trailing
)
XCTAssertEqual("\(funcKW)", " func ")
XCTAssertEqual(funcKW.position, AbsolutePosition(utf8Offset: 0))
XCTAssertEqual(funcKW.positionAfterSkippingLeadingTrivia, AbsolutePosition(utf8Offset: 2))
XCTAssertEqual(funcKW.endPositionBeforeTrailingTrivia, AbsolutePosition(utf8Offset: 6))
XCTAssertEqual(funcKW.endPosition, AbsolutePosition(utf8Offset: 7))
XCTAssertEqual(funcKW.trimmedLength, SourceLength(utf8Length: 4))
}
public func testEnumCaseParameterSyntaxConvenienceInit() {
let noFirstName = EnumCaseParameterSyntax(type: TypeSyntax("MyType"))
XCTAssertEqual(noFirstName.formatted().description, "MyType")
let node = EnumCaseParameterSyntax(firstName: "label", type: TypeSyntax("MyType"))
XCTAssertEqual(node.formatted().description, "label: MyType")
}
public func testClosureCaptureSyntaxConvenienceInitWithEqual() {
let noNameClosureCapture = ClosureCaptureSyntax(expression: ExprSyntax("123"))
XCTAssertEqual(noNameClosureCapture.formatted().description, "123")
let node = ClosureCaptureSyntax(name: "test", expression: ExprSyntax("123"))
XCTAssertEqual(node.formatted().description, "test = 123")
}
func testShareSyntaxIndexInTreeBetweenTrees() throws {
let source = "func foo() {}"
let tree1 = DeclSyntax(stringLiteral: source)
let tree2 = DeclSyntax(stringLiteral: source)
let funcKeywordInTree1 = try XCTUnwrap(tree1.firstToken(viewMode: .sourceAccurate))
XCTAssertEqual(funcKeywordInTree1.tokenKind, .keyword(.func))
let opaqueIndexInTree1 = funcKeywordInTree1.id.indexInTree.toOpaque()
let funcKeywordIdentifierInTree2 = try XCTUnwrap(
SyntaxIdentifier.fromIndexInTree(
SyntaxIdentifier.SyntaxIndexInTree(fromOpaque: opaqueIndexInTree1),
relativeToRoot: tree2
)
)
let funcKeywordInTree2 = tree2.node(at: funcKeywordIdentifierInTree2)
XCTAssertEqual(funcKeywordInTree2?.as(TokenSyntax.self)?.tokenKind, .keyword(.func))
XCTAssertNotEqual(funcKeywordInTree1.id, funcKeywordInTree2?.id)
}
}
|