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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
//===----------------------------------------------------------------------===//
//
// 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
fileprivate func cannedStructDecl() -> StructDeclSyntax {
let structKW = TokenSyntax.keyword(.struct, trailingTrivia: .space)
let fooID = TokenSyntax.identifier("Foo", trailingTrivia: .space)
let rBrace = TokenSyntax.rightBraceToken(leadingTrivia: .newline)
let memberBlock = MemberBlockSyntax(
leftBrace: .leftBraceToken(),
members: MemberBlockItemListSyntax([]),
rightBrace: rBrace
)
return StructDeclSyntax(
structKeyword: structKW,
name: fooID,
memberBlock: memberBlock
)
}
class SyntaxCreationTests: XCTestCase {
public func testGenerated() {
let structDecl = cannedStructDecl()
XCTAssertEqual(
"\(structDecl)",
"""
struct Foo {
}
"""
)
let forType = TokenSyntax.identifier("`for`", trailingTrivia: .space)
let newBrace = TokenSyntax.rightBraceToken(leadingTrivia: .newlines(2))
let renamed = structDecl.with(\.name, forType)
.with(
\.memberBlock,
structDecl.memberBlock
.with(\.rightBrace, newBrace)
)
XCTAssertEqual(
"\(renamed)",
"""
struct `for` {
}
"""
)
XCTAssertNotEqual(structDecl.memberBlock, renamed.memberBlock)
XCTAssertEqual(structDecl, StructDeclSyntax(structDecl.root))
XCTAssertNil(structDecl.parent)
XCTAssertNotNil(structDecl.memberBlock.parent)
XCTAssertEqual(structDecl.memberBlock.parent.map(StructDeclSyntax.init), structDecl)
XCTAssertEqual(
"\(structDecl.memberBlock.rightBrace)",
"""
}
"""
)
}
public func testTokenSyntax() {
let tok = TokenSyntax.keyword(.struct, trailingTrivia: .space)
XCTAssertEqual("\(tok)", "struct ")
XCTAssertEqual(tok.presence, .present)
let preSpacedTok = tok.with(\.leadingTrivia, .spaces(3))
XCTAssertEqual("\(preSpacedTok)", " struct ")
var mutablePreSpacedTok = tok
mutablePreSpacedTok.leadingTrivia = .spaces(4)
XCTAssertEqual("\(mutablePreSpacedTok)", " struct ")
let postSpacedTok = tok.with(\.trailingTrivia, .spaces(6))
XCTAssertEqual("\(postSpacedTok)", "struct ")
var mutablePostSpacedTok = tok
mutablePostSpacedTok.trailingTrivia = .spaces(3)
XCTAssertEqual("\(mutablePostSpacedTok)", "struct ")
let prePostSpacedTok = preSpacedTok.with(\.trailingTrivia, .spaces(4))
XCTAssertEqual("\(prePostSpacedTok)", " struct ")
mutablePreSpacedTok.trailingTrivia = .spaces(2)
XCTAssertEqual("\(mutablePreSpacedTok)", " struct ")
}
// private func makeStringLiteralExpr(_ text: String, leadingTrivia: Trivia = [], trailingTrivia: Trivia = []) -> StringLiteralExprSyntax {
// return
// }
public func testFunctionCallSyntaxBuilder() {
let string = StringLiteralExprSyntax(
openingQuote: .stringQuoteToken(),
segments: StringLiteralSegmentListSyntax([
.stringSegment(StringSegmentSyntax(content: .stringSegment("Hello, world!")))
]),
closingQuote: .stringQuoteToken()
)
let printID = DeclReferenceExprSyntax(baseName: .identifier("print"))
let arg = LabeledExprSyntax(expression: string)
let call = FunctionCallExprSyntax(
calledExpression: printID,
leftParen: .leftParenToken(),
arguments: LabeledExprListSyntax([arg]),
rightParen: .rightParenToken()
)
XCTAssertEqual("\(call)", "print(\"Hello, world!\")")
let emptyString = StringLiteralExprSyntax(
openingQuote: .stringQuoteToken(),
segments: StringLiteralSegmentListSyntax([.stringSegment(StringSegmentSyntax(content: .stringSegment(" ")))]),
closingQuote: .stringQuoteToken()
)
let terminatorArg = LabeledExprSyntax(
label: .identifier("terminator"),
colon: .colonToken(trailingTrivia: .space),
expression: emptyString
)
let callWithTerminator = call.with(
\.arguments,
LabeledExprListSyntax([
arg.with(
\.trailingComma,
.commaToken(trailingTrivia: .space)
),
terminatorArg,
])
)
XCTAssertEqual(
"\(callWithTerminator)",
"print(\"Hello, world!\", terminator: \" \")"
)
}
public func testWithOptionalChild() {
let string = StringLiteralExprSyntax(
openingPounds: nil,
openingQuote: .stringQuoteToken(),
segments: StringLiteralSegmentListSyntax([
.stringSegment(StringSegmentSyntax(content: .stringSegment("Hello, world!")))
]),
closingQuote: .stringQuoteToken(),
closingPounds: nil
)
let printID = DeclReferenceExprSyntax(baseName: .identifier("print"))
let arg = LabeledExprSyntax(expression: string)
let call1 = FunctionCallExprSyntax(
calledExpression: printID,
leftParen: .leftParenToken(),
arguments: LabeledExprListSyntax([arg]),
rightParen: .rightParenToken()
)
XCTAssertNotNil(call1.leftParen)
XCTAssertNotNil(call1.rightParen)
let call2 = call1.with(\.leftParen, nil).with(\.rightParen, nil)
XCTAssertNil(call2.leftParen)
XCTAssertNil(call2.rightParen)
let call3 = FunctionCallExprSyntax(
calledExpression: printID,
arguments: LabeledExprListSyntax([arg])
)
XCTAssertNil(call3.leftParen)
XCTAssertNil(call3.rightParen)
}
public func testMakeStringLiteralExpr() {
let expr = StringLiteralExprSyntax(
openingQuote: .stringQuoteToken(leadingTrivia: [.lineComment("// hello"), .newlines(1)]),
segments: StringLiteralSegmentListSyntax([
.stringSegment(StringSegmentSyntax(content: .stringSegment("Hello, world!")))
]),
closingQuote: .stringQuoteToken()
)
let expected = """
// hello
"Hello, world!"
"""
XCTAssertEqual(expr.description, expected)
}
public func testMakeBinaryOperator() {
let first = IntegerLiteralExprSyntax(
literal: .integerLiteral("1")
)
let second = IntegerLiteralExprSyntax(
literal: .integerLiteral("1")
)
let operatorNames = ["==", "!=", "+", "-", "*", "/", "<", ">", "<=", ">="]
operatorNames.forEach { operatorName in
let operatorToken = TokenSyntax.binaryOperator(operatorName, leadingTrivia: .space, trailingTrivia: .space)
let operatorExpr = BinaryOperatorExprSyntax(operator: operatorToken)
let exprList = ExprListSyntax([
ExprSyntax(first),
ExprSyntax(operatorExpr),
ExprSyntax(second),
])
XCTAssertEqual("\(exprList)", "1 \(operatorName) 1")
}
}
func testTriviaInInitializxerDoesNotOverrideFirstNode() {
let node = ExpressionPatternSyntax(
leadingTrivia: .lineComment("// Outer leading") + .newline,
expression: IntegerLiteralExprSyntax(
leadingTrivia: .lineComment("// Inner leading") + .newline,
literal: .integerLiteral("42"),
trailingTrivia: .newline + .lineComment("// Inner trailing")
),
trailingTrivia: .newline + .lineComment("// Outer trailing")
)
XCTAssertEqual(
node.description,
"""
// Outer leading
// Inner leading
42
// Inner trailing
// Outer trailing
"""
)
}
}
|