Skip to content

Commit ef898d7

Browse files
committed
Add ExtensionDecl test
1 parent e73e11b commit ef898d7

10 files changed

+274
-46
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 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+
import SwiftSyntax
14+
15+
extension BinaryOperatorExpr {
16+
public init(_ text: String) {
17+
self.init(operatorToken: TokenSyntax.spacedBinaryOperator(text))
18+
}
19+
}
20+
21+
extension BinaryOperatorExpr: ExpressibleByStringLiteral {
22+
public init(stringLiteral value: String) {
23+
self.init(value)
24+
}
25+
}

Sources/SwiftSyntaxBuilder/Buildables.swift.gyb

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ extension ${node.syntax_kind}: ${expressible_as_type} {
242242

243243
% end
244244
% end
245-
public protocol ExpressibleAsTokenSyntax {
245+
public protocol ExpressibleAsTokenSyntax: ExpressibleAsDeclModifier {
246246
func createTokenSyntax() -> TokenSyntax
247247
}
248248

@@ -264,18 +264,6 @@ extension TokenSyntax: ExpressibleAsTokenSyntax {
264264

265265
% end
266266
% end
267-
268-
% for protocol, conformances in SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.items():
269-
% if 'ExpressibleAsConditionElementList' in conformances:
270-
extension ${protocol} {
271-
public func createConditionElementList() -> ConditionElementList {
272-
ConditionElementList([self])
273-
}
274-
}
275-
276-
% end
277-
% end
278-
279267
% for protocol, conformances in SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.items():
280268
% if 'ConditionElement' in conformances:
281269
extension ExpressibleAs${protocol} {
@@ -317,4 +305,52 @@ extension ExpressibleAs${protocol} {
317305
}
318306

319307
% end
308+
% if 'MemberDeclBlock' in conformances:
309+
extension ExpressibleAs${protocol} {
310+
public func createMemberDeclBlock() -> MemberDeclBlock {
311+
MemberDeclBlock(members: self)
312+
}
313+
}
314+
315+
% end
316+
% if 'AccessorBlock' in conformances:
317+
extension ExpressibleAs${protocol} {
318+
public func createAccessorBlock() -> AccessorBlock {
319+
AccessorBlock(accessors: self)
320+
}
321+
}
322+
323+
% end
324+
% if 'DeclModifier' in conformances:
325+
extension ExpressibleAs${protocol} {
326+
public func createDeclModifier() -> DeclModifier {
327+
DeclModifier(name: self)
328+
}
329+
}
330+
331+
% end
332+
% if 'BinaryOperatorExpr' in conformances:
333+
extension ExpressibleAs${protocol} {
334+
public func createBinaryOperatorExpr() -> BinaryOperatorExpr {
335+
BinaryOperatorExpr(operatorToken: self)
336+
}
337+
}
338+
339+
% end
340+
% if 'TupleExprElement' in conformances:
341+
extension ExpressibleAs${protocol} {
342+
public func createTupleExprElement() -> TupleExprElement {
343+
TupleExprElement(expression: self)
344+
}
345+
}
346+
347+
% end
348+
% if 'ReturnStmt' in conformances:
349+
extension ExpressibleAs${protocol} {
350+
public func createReturnStmt() -> ReturnStmt {
351+
ReturnStmt(expression: self)
352+
}
353+
}
354+
355+
% end
320356
% end

Sources/SwiftSyntaxBuilder/FunctionCallExprConvenienceInitializers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension FunctionCallExpr {
1919
leftParen: ExpressibleAsTokenSyntax? = nil,
2020
rightParen: ExpressibleAsTokenSyntax? = nil,
2121
trailingClosure: ExpressibleAsClosureExpr? = nil,
22-
@TupleExprElementListBuilder argumentListBuilder: () -> TupleExprElementList = { .empty },
22+
@TupleExprElementListBuilder argumentListBuilder: () -> ExpressibleAsTupleExprElementList = { TupleExprElementList.empty },
2323
@MultipleTrailingClosureElementListBuilder additionalTrailingClosuresBuilder: () -> MultipleTrailingClosureElementList? = { nil }
2424
) {
2525
self.init(
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 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+
import SwiftSyntax
14+
15+
extension ReturnClause: ExpressibleByStringLiteral {
16+
public init(stringLiteral value: String) {
17+
self.init(returnType: value)
18+
}
19+
}

Sources/SwiftSyntaxBuilder/StringConvenienceInitializers.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ extension String: ExpressibleAsStringLiteralExpr {
3434
}
3535
}
3636

37+
extension String: ExpressibleAsBinaryOperatorExpr {
38+
public func createBinaryOperatorExpr() -> BinaryOperatorExpr {
39+
BinaryOperatorExpr(self)
40+
}
41+
}
42+
43+
extension String: ExpressibleAsReturnClause {
44+
public func createReturnClause() -> ReturnClause {
45+
ReturnClause(stringLiteral: self)
46+
}
47+
}
48+
3749
/// Default conformance to `ExpressibleByTypeBuildable`
3850
extension String {
3951
public func createTypeBuildable() -> TypeBuildable {

Sources/SwiftSyntaxBuilder/gyb_generated/Buildables.swift

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ public struct SequenceExpr: ExprBuildable {
11501150
}
11511151
}
11521152

1153-
public protocol ExpressibleAsSequenceExpr {
1153+
public protocol ExpressibleAsSequenceExpr: ExpressibleAsCodeBlockItem, ExpressibleAsExprBuildable, ExpressibleAsReturnStmt, ExpressibleAsTupleExprElement {
11541154
func createSequenceExpr() -> SequenceExpr
11551155
}
11561156

@@ -2720,7 +2720,7 @@ public struct FunctionCallExpr: ExprBuildable {
27202720
}
27212721
}
27222722

2723-
public protocol ExpressibleAsFunctionCallExpr {
2723+
public protocol ExpressibleAsFunctionCallExpr: ExpressibleAsCodeBlockItem, ExpressibleAsExprBuildable, ExpressibleAsReturnStmt {
27242724
func createFunctionCallExpr() -> FunctionCallExpr
27252725
}
27262726

@@ -4686,7 +4686,7 @@ public struct MemberDeclList: SyntaxBuildable {
46864686
}
46874687
}
46884688

4689-
public protocol ExpressibleAsMemberDeclList {
4689+
public protocol ExpressibleAsMemberDeclList: ExpressibleAsMemberDeclBlock {
46904690
func createMemberDeclList() -> MemberDeclList
46914691
}
46924692

@@ -5502,7 +5502,7 @@ public struct AccessorList: SyntaxBuildable {
55025502
}
55035503
}
55045504

5505-
public protocol ExpressibleAsAccessorList {
5505+
public protocol ExpressibleAsAccessorList: ExpressibleAsAccessorBlock {
55065506
func createAccessorList() -> AccessorList
55075507
}
55085508

@@ -11166,7 +11166,7 @@ extension VersionTuple: ExpressibleAsVersionTuple {
1116611166
}
1116711167
}
1116811168

11169-
public protocol ExpressibleAsTokenSyntax {
11169+
public protocol ExpressibleAsTokenSyntax: ExpressibleAsDeclModifier {
1117011170
func createTokenSyntax() -> TokenSyntax
1117111171
}
1117211172

@@ -11424,29 +11424,39 @@ extension TokenSyntax: ExpressibleAsTokenSyntax {
1142411424
}
1142511425
}
1142611426

11427+
extension ExpressibleAsAccessorList {
11428+
public func createAccessorBlock() -> AccessorBlock {
11429+
AccessorBlock(accessors: self)
11430+
}
11431+
}
1142711432

11433+
extension ExpressibleAsStmtBuildable {
11434+
public func createCodeBlockItem() -> CodeBlockItem {
11435+
CodeBlockItem(item: self)
11436+
}
11437+
}
1142811438

11429-
extension ExpressibleAsDeclBuildable {
11439+
extension ExpressibleAsFunctionCallExpr {
1143011440
public func createCodeBlockItem() -> CodeBlockItem {
1143111441
CodeBlockItem(item: self)
1143211442
}
1143311443
}
1143411444

11435-
extension ExpressibleAsDeclBuildable {
11436-
public func createMemberDeclListItem() -> MemberDeclListItem {
11437-
MemberDeclListItem(decl: self)
11445+
extension ExpressibleAsFunctionCallExpr {
11446+
public func createReturnStmt() -> ReturnStmt {
11447+
ReturnStmt(expression: self)
1143811448
}
1143911449
}
1144011450

11441-
extension ExpressibleAsStmtBuildable {
11451+
extension ExpressibleAsDeclBuildable {
1144211452
public func createCodeBlockItem() -> CodeBlockItem {
1144311453
CodeBlockItem(item: self)
1144411454
}
1144511455
}
1144611456

11447-
extension ExpressibleAsExprList {
11448-
public func createConditionElement() -> ConditionElement {
11449-
ConditionElement(condition: self)
11457+
extension ExpressibleAsDeclBuildable {
11458+
public func createMemberDeclListItem() -> MemberDeclListItem {
11459+
MemberDeclListItem(decl: self)
1145011460
}
1145111461
}
1145211462

@@ -11462,3 +11472,45 @@ extension ExpressibleAsSimpleTypeIdentifier {
1146211472
}
1146311473
}
1146411474

11475+
extension ExpressibleAsMemberDeclList {
11476+
public func createMemberDeclBlock() -> MemberDeclBlock {
11477+
MemberDeclBlock(members: self)
11478+
}
11479+
}
11480+
11481+
extension ExpressibleAsExprList {
11482+
public func createConditionElement() -> ConditionElement {
11483+
ConditionElement(condition: self)
11484+
}
11485+
}
11486+
11487+
extension ExpressibleAsSequenceExpr {
11488+
public func createCodeBlockItem() -> CodeBlockItem {
11489+
CodeBlockItem(item: self)
11490+
}
11491+
}
11492+
11493+
extension ExpressibleAsSequenceExpr {
11494+
public func createTupleExprElement() -> TupleExprElement {
11495+
TupleExprElement(expression: self)
11496+
}
11497+
}
11498+
11499+
extension ExpressibleAsSequenceExpr {
11500+
public func createReturnStmt() -> ReturnStmt {
11501+
ReturnStmt(expression: self)
11502+
}
11503+
}
11504+
11505+
extension ExpressibleAsTokenSyntax {
11506+
public func createDeclModifier() -> DeclModifier {
11507+
DeclModifier(name: self)
11508+
}
11509+
}
11510+
11511+
extension ExpressibleAsTokenSyntax {
11512+
public func createBinaryOperatorExpr() -> BinaryOperatorExpr {
11513+
BinaryOperatorExpr(operatorToken: self)
11514+
}
11515+
}
11516+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import XCTest
2+
import SwiftSyntax
3+
import SwiftSyntaxBuilder
4+
5+
final class BinaryOperatorExprTests: XCTestCase {
6+
func testBinaryOperatorExprConvenienceInitializers() {
7+
let leadingTrivia = Trivia.garbageText("")
8+
let testCases: [UInt: (ExpressibleAsBinaryOperatorExpr, String)] = [
9+
#line: (BinaryOperatorExpr(stringLiteral: "=="), "␣ == "),
10+
#line: (BinaryOperatorExpr("=="), "␣ == "),
11+
#line: ("==", "␣ == "),
12+
]
13+
14+
for (line, testCase) in testCases {
15+
let (builder, expected) = testCase
16+
let binaryOperatorExpr = builder.createBinaryOperatorExpr()
17+
let syntax = binaryOperatorExpr.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
18+
19+
var text = ""
20+
syntax.write(to: &text)
21+
22+
XCTAssertEqual(text, expected, line: line)
23+
}
24+
}
25+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import XCTest
2+
import SwiftSyntax
3+
import SwiftSyntaxBuilder
4+
5+
final class ExtensionDeclTests: XCTestCase {
6+
func testExtensionDecl() {
7+
let leadingTrivia = Trivia.garbageText("")
8+
let genericWhereClause = GenericWhereClause(whereKeyword: TokenSyntax.where.withLeadingTrivia(.spaces(1))) {
9+
GenericRequirement(body: ExprList([
10+
TypeExpr(type: "Self"),
11+
BinaryOperatorExpr("=="),
12+
TypeExpr(type: "TokenSyntax")
13+
]))
14+
}
15+
let keywords = ["associatedtype", "class"].map { keyword -> VariableDecl in
16+
let body = CodeBlock(statementsBuilder: {
17+
FunctionCallExpr("SyntaxFactory.make\(keyword)Keyword",
18+
leftParen: TokenSyntax.leftParen,
19+
rightParen: TokenSyntax.rightParen)
20+
})
21+
22+
return VariableDecl(letOrVarKeyword: .var,
23+
modifiersBuilder: { TokenSyntax.public },
24+
bindingsBuilder: {
25+
PatternBinding(pattern: "`\(keyword)`",
26+
typeAnnotation: "TokenSyntax",
27+
initializer: nil,
28+
accessor: body,
29+
trailingComma: nil)
30+
31+
})
32+
}
33+
let members = MemberDeclList(keywords)
34+
let buildable = ExtensionDecl(modifiers: nil,
35+
extendedType: "ExpressibleAsTokenSyntax",
36+
genericWhereClause: genericWhereClause,
37+
members: members)
38+
39+
let syntax = buildable.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
40+
41+
var text = ""
42+
syntax.write(to: &text)
43+
44+
print(text)
45+
46+
XCTAssertEqual(text, """
47+
␣extension ExpressibleAsTokenSyntax where Self == TokenSyntax{
48+
public var `associatedtype`: TokenSyntax{
49+
SyntaxFactory.makeassociatedtypeKeyword()
50+
}
51+
public var `class`: TokenSyntax{
52+
SyntaxFactory.makeclassKeyword()
53+
}
54+
}
55+
""")
56+
}
57+
}

0 commit comments

Comments
 (0)