forked from swiftlang/swift-syntax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyntaxTests.swift
222 lines (189 loc) · 8.18 KB
/
SyntaxTests.swift
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//===----------------------------------------------------------------------===//
//
// 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)
}
func testIntegerLiteralExprSyntax() {
let testCases: [UInt: (String, Int?)] = [
#line: ("2", 2),
#line: ("02", 2),
#line: ("020", 20),
#line: ("-02", -2),
#line: ("2_00_0000", 2_00_0000),
#line: ("-2_00_0000", -2_00_0000),
#line: ("foo", nil),
#line: ("999999999999999999999999999999999999999999999999999999999999999999999999999999", nil),
#line: ("0b1010101", 85),
#line: ("0xFF", 255),
#line: ("0o777", 511),
#line: ("0b001100", 0b001100),
#line: ("4_2", 4_2),
#line: ("0o3434", 0o3434),
#line: ("0xba11aD", 0xba11aD),
#line: ("🐋", nil),
#line: ("-0xA", nil),
#line: ("-0o7", nil),
#line: ("-0b1", nil),
]
for (line, testCase) in testCases {
let (value, expected) = testCase
let expr = IntegerLiteralExprSyntax(literal: .integerLiteral(value))
XCTAssertEqual(expr.representedLiteralValue, expected, line: line)
}
}
func testFloatLiteralExprSyntax() {
let testCases: [UInt: (String, Double?)] = [
#line: ("2", 2),
#line: ("2_00_00.001", 2_00_00.001),
#line: ("5.3_8", 5.3_8),
#line: ("12e3", 12000.0),
#line: ("32E1", 320.0),
#line: ("0xdEFACE.C0FFEEp+1", 0xdEFACE.C0FFEEp+1),
#line: ("0xaffab1e.e1fP-2", 0xaffab1e.e1fP-2),
#line: ("🥥", nil),
#line: ("12e+3", 12000.0),
#line: ("12e-3", 0.012),
]
for (line, testCase) in testCases {
let (value, expected) = testCase
let expr = FloatLiteralExprSyntax(literal: .floatLiteral(value))
XCTAssertEqual(expr.representedLiteralValue, expected, line: line)
}
}
}