Skip to content

Commit 3ea1ad8

Browse files
committed
Add ExtensionDecl test
1 parent f17a5b5 commit 3ea1ad8

15 files changed

+176
-57
lines changed
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 BinaryOperatorExpr {
16+
public init(_ text: String) {
17+
self.init(operatorToken: TokenSyntax.spacedBinaryOperator(text))
18+
}
19+
}

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: TokenSyntax? = nil,
2020
rightParen: TokenSyntax? = 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(

Sources/SwiftSyntaxBuilder/IdentifierExprConvenienceInitializers.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,3 @@ extension IdentifierExpr {
1515
self.init(identifier: .identifier(identifier))
1616
}
1717
}
18-
19-
extension IdentifierExpr: ExpressibleByStringLiteral {
20-
public init(stringLiteral value: String) {
21-
self.init(value)
22-
}
23-
}

Sources/SwiftSyntaxBuilder/IdentifierPatternConvenienceInitializers.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,3 @@ extension IdentifierPattern {
1515
self.init(identifier: .identifier(identifier))
1616
}
1717
}
18-
19-
extension IdentifierPattern: ExpressibleByStringLiteral {
20-
public init(stringLiteral value: String) {
21-
self.init(value)
22-
}
23-
}

Sources/SwiftSyntaxBuilder/SimpleTypeIdentifierConvenienceInitializers.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@ import SwiftSyntax
1414

1515
extension SimpleTypeIdentifier {
1616
public init(_ name: String) {
17-
self.init(name: SyntaxFactory.makeIdentifier(name),
17+
self.init(name: .identifier(name),
1818
genericArgumentClause: nil)
1919
}
2020
}
21-
22-
extension SimpleTypeIdentifier: ExpressibleByStringLiteral {
23-
public init(stringLiteral value: String) {
24-
self.init(value)
25-
}
26-
}

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(returnType: self)
46+
}
47+
}
48+
3749
/// Default conformance to `ExpressibleByTypeBuildable`
3850
extension String {
3951
public func createTypeBuildable() -> TypeBuildable {

Sources/SwiftSyntaxBuilder/StringLiteralExprConvenienceInitializers.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,3 @@ extension StringLiteralExpr {
2323
closeQuote: closeQuote)
2424
}
2525
}
26-
27-
extension StringLiteralExpr: ExpressibleByStringLiteral {
28-
public init(stringLiteral value: String) {
29-
self.init(value)
30-
}
31-
}

Sources/SwiftSyntaxBuilder/TypeAnnotationConvenienceInitializers.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,3 @@ extension TypeAnnotation {
1515
self.init(type: SimpleTypeIdentifier(type))
1616
}
1717
}
18-
19-
extension TypeAnnotation: ExpressibleByStringLiteral {
20-
public init(stringLiteral value: String) {
21-
self.init(value)
22-
}
23-
}

Sources/SwiftSyntaxBuilder/gyb_generated/Buildables.swift

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ public struct CodeBlockItemList: SyntaxBuildable {
434434
}
435435
}
436436

437-
public protocol ExpressibleAsCodeBlockItemList {
437+
public protocol ExpressibleAsCodeBlockItemList: ExpressibleAsCodeBlock {
438438
func createCodeBlockItemList() -> CodeBlockItemList
439439
}
440440

@@ -1222,7 +1222,7 @@ public struct SequenceExpr: ExprBuildable {
12221222
}
12231223
}
12241224

1225-
public protocol ExpressibleAsSequenceExpr {
1225+
public protocol ExpressibleAsSequenceExpr: ExpressibleAsCodeBlockItem, ExpressibleAsExprBuildable, ExpressibleAsTupleExprElement {
12261226
func createSequenceExpr() -> SequenceExpr
12271227
}
12281228

@@ -2950,7 +2950,7 @@ public struct FunctionCallExpr: ExprBuildable {
29502950
}
29512951
}
29522952

2953-
public protocol ExpressibleAsFunctionCallExpr {
2953+
public protocol ExpressibleAsFunctionCallExpr: ExpressibleAsCodeBlockItem, ExpressibleAsExprBuildable {
29542954
func createFunctionCallExpr() -> FunctionCallExpr
29552955
}
29562956

@@ -6056,7 +6056,7 @@ public struct AccessorList: SyntaxBuildable {
60566056
}
60576057
}
60586058

6059-
public protocol ExpressibleAsAccessorList {
6059+
public protocol ExpressibleAsAccessorList: ExpressibleAsAccessorBlock {
60606060
func createAccessorList() -> AccessorList
60616061
}
60626062

@@ -12318,7 +12318,7 @@ extension VersionTuple: ExpressibleAsVersionTuple {
1231812318
}
1231912319
}
1232012320

12321-
extension TokenSyntax: ExpressibleAsIdentifierList, ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList {
12321+
extension TokenSyntax: ExpressibleAsIdentifierList, ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList, ExpressibleAsBinaryOperatorExpr, ExpressibleAsDeclModifier {
1232212322
}
1232312323

1232412324
// MARK: - Syntax Collection buildable expressible as conformances
@@ -12571,27 +12571,33 @@ extension ExpressibleAsPrecedenceGroupNameElement {
1257112571

1257212572
// MARK: - Syntax buildable expressible as conformances
1257312573

12574-
extension ExpressibleAsStmtBuildable {
12574+
extension ExpressibleAsFunctionCallExpr {
1257512575
public func createCodeBlockItem() -> CodeBlockItem {
1257612576
CodeBlockItem(item: self)
1257712577
}
1257812578
}
1257912579

12580+
extension ExpressibleAsCodeBlockItemList {
12581+
public func createCodeBlock() -> CodeBlock {
12582+
CodeBlock(statements: self)
12583+
}
12584+
}
12585+
1258012586
extension ExpressibleAsMemberDeclList {
1258112587
public func createMemberDeclBlock() -> MemberDeclBlock {
1258212588
MemberDeclBlock(members: self)
1258312589
}
1258412590
}
1258512591

12586-
extension ExpressibleAsDeclBuildable {
12592+
extension ExpressibleAsSequenceExpr {
1258712593
public func createCodeBlockItem() -> CodeBlockItem {
1258812594
CodeBlockItem(item: self)
1258912595
}
1259012596
}
1259112597

12592-
extension ExpressibleAsDeclBuildable {
12593-
public func createMemberDeclListItem() -> MemberDeclListItem {
12594-
MemberDeclListItem(decl: self)
12598+
extension ExpressibleAsSequenceExpr {
12599+
public func createTupleExprElement() -> TupleExprElement {
12600+
TupleExprElement(expression: self)
1259512601
}
1259612602
}
1259712603

@@ -12613,3 +12619,39 @@ extension ExpressibleAsExprList {
1261312619
}
1261412620
}
1261512621

12622+
extension ExpressibleAsStmtBuildable {
12623+
public func createCodeBlockItem() -> CodeBlockItem {
12624+
CodeBlockItem(item: self)
12625+
}
12626+
}
12627+
12628+
extension ExpressibleAsDeclBuildable {
12629+
public func createCodeBlockItem() -> CodeBlockItem {
12630+
CodeBlockItem(item: self)
12631+
}
12632+
}
12633+
12634+
extension ExpressibleAsDeclBuildable {
12635+
public func createMemberDeclListItem() -> MemberDeclListItem {
12636+
MemberDeclListItem(decl: self)
12637+
}
12638+
}
12639+
12640+
extension ExpressibleAsAccessorList {
12641+
public func createAccessorBlock() -> AccessorBlock {
12642+
AccessorBlock(accessors: self)
12643+
}
12644+
}
12645+
12646+
extension TokenSyntax {
12647+
public func createBinaryOperatorExpr() -> BinaryOperatorExpr {
12648+
BinaryOperatorExpr(operatorToken: self)
12649+
}
12650+
}
12651+
12652+
extension TokenSyntax {
12653+
public func createDeclModifier() -> DeclModifier {
12654+
DeclModifier(name: self)
12655+
}
12656+
}
12657+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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("=="), "␣ == "),
10+
#line: ("==", "␣ == "),
11+
]
12+
13+
for (line, testCase) in testCases {
14+
let (builder, expected) = testCase
15+
let binaryOperatorExpr = builder.createBinaryOperatorExpr()
16+
let syntax = binaryOperatorExpr.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
17+
18+
var text = ""
19+
syntax.write(to: &text)
20+
21+
XCTAssertEqual(text, expected, line: line)
22+
}
23+
}
24+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
// We need to use `CodeBlock` here to ensure there is braces around.
17+
let body = CodeBlock(statementsBuilder: {
18+
FunctionCallExpr("SyntaxFactory.make\(keyword)Keyword",
19+
leftParen: TokenSyntax.leftParen,
20+
rightParen: TokenSyntax.rightParen)
21+
})
22+
23+
return VariableDecl(letOrVarKeyword: .var,
24+
modifiersBuilder: { TokenSyntax.public },
25+
bindingsBuilder: {
26+
PatternBinding(pattern: "`\(keyword)`",
27+
typeAnnotation: "TokenSyntax",
28+
initializer: nil,
29+
accessor: body,
30+
trailingComma: nil)
31+
32+
})
33+
}
34+
let members = MemberDeclList(keywords)
35+
let buildable = ExtensionDecl(modifiers: nil,
36+
extendedType: "ExpressibleAsTokenSyntax",
37+
genericWhereClause: genericWhereClause,
38+
members: members)
39+
40+
let syntax = buildable.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
41+
42+
var text = ""
43+
syntax.write(to: &text)
44+
45+
print(text)
46+
47+
XCTAssertEqual(text, """
48+
␣extension ExpressibleAsTokenSyntax where Self == TokenSyntax{
49+
public var `associatedtype`: TokenSyntax{
50+
SyntaxFactory.makeassociatedtypeKeyword()
51+
}
52+
public var `class`: TokenSyntax{
53+
SyntaxFactory.makeclassKeyword()
54+
}
55+
}
56+
""")
57+
}
58+
}

Tests/SwiftSyntaxBuilderTest/FunctionTests.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,22 @@ import SwiftSyntax
33
import SwiftSyntaxBuilder
44

55
final class FunctionTests: XCTestCase {
6-
func testEmptyStruct() {
6+
func testFibonacci() {
77
let leadingTrivia = Trivia.garbageText("")
88

99
let input = ParameterClause(parameterListBuilder: {
1010
FunctionParameter(firstName: .wildcard, secondName: .identifier("n"), colon: .colon, type: "Int", attributesBuilder: {})
1111
})
1212

13-
let ifCodeBlock = CodeBlock(statementsBuilder: {
14-
ReturnStmt(expression: IntegerLiteralExpr(digits: "n"))
15-
})
16-
17-
let signature = FunctionSignature(input: input, output: ReturnClause(returnType: "Int"))
13+
let ifCodeBlock = ReturnStmt(expression: IntegerLiteralExpr(digits: "n"))
14+
15+
let signature = FunctionSignature(input: input, output: "Int")
1816

19-
2017
let codeBlock = CodeBlock(statementsBuilder: {
2118
IfStmt(conditions: ExprList([
2219
IntegerLiteralExpr(digits: "n"),
2320

24-
BinaryOperatorExpr(operatorToken: .unspacedBinaryOperator("<=")),
21+
BinaryOperatorExpr("<="),
2522

2623
IntegerLiteralExpr(1)
2724
]), body: ifCodeBlock)
@@ -31,7 +28,7 @@ final class FunctionTests: XCTestCase {
3128
TupleExprElement(expression: SequenceExpr(elementsBuilder: {
3229
IntegerLiteralExpr(digits: "n")
3330

34-
BinaryOperatorExpr(operatorToken: .unspacedBinaryOperator("-"))
31+
BinaryOperatorExpr("-")
3532

3633
IntegerLiteralExpr(1)
3734
}))
@@ -43,7 +40,7 @@ final class FunctionTests: XCTestCase {
4340
TupleExprElement(expression: SequenceExpr(elementsBuilder: {
4441
IntegerLiteralExpr(digits: "n")
4542

46-
BinaryOperatorExpr(operatorToken: .unspacedBinaryOperator("-"))
43+
BinaryOperatorExpr("-")
4744

4845
IntegerLiteralExpr(2)
4946
}))
@@ -56,10 +53,10 @@ final class FunctionTests: XCTestCase {
5653

5754
XCTAssertEqual(syntax.description, """
5855
func fibonacci(_ n: Int)-> Int{
59-
if n<=1{
56+
if n <= 1{
6057
return n
6158
}
62-
return fibonacci(n-1) + fibonacci(n-2)
59+
return fibonacci(n - 1) + fibonacci(n - 2)
6360
}
6461
""")
6562
}

Tests/SwiftSyntaxBuilderTest/IdentifierExprTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ final class IdentifierExprTests: XCTestCase {
88

99
let testCases: [UInt: (ExpressibleAsIdentifierExpr, String)] = [
1010
#line: (IdentifierExpr(identifier: .identifier("Test")), "␣Test"),
11-
#line: (IdentifierExpr(stringLiteral: "Test"), "␣Test"),
1211
#line: (IdentifierExpr("Test"), "␣Test"),
1312
#line: ("Test", "␣Test")
1413
]

Tests/SwiftSyntaxBuilderTest/IdentifierPatternTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ final class IdentifierPatternTests: XCTestCase {
88

99
let testCases: [UInt: (ExpressibleAsIdentifierPattern, String)] = [
1010
#line: (IdentifierPattern(identifier: .identifier("Test")), "␣Test"),
11-
#line: (IdentifierPattern(stringLiteral: "Test"), "␣Test"),
1211
#line: (IdentifierPattern("Test"), "␣Test"),
1312
#line: ("Test", "␣Test")
1413
]

Tests/SwiftSyntaxBuilderTest/TypeAnnotationTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ final class TypeAnnotationTests: XCTestCase {
99
let testCases: [UInt: (ExpressibleAsTypeAnnotation, String)] = [
1010
#line: (TypeAnnotation(type: "Test"), "␣: Test"),
1111
#line: (TypeAnnotation(type: SimpleTypeIdentifier("Test")), "␣: Test"),
12-
#line: (TypeAnnotation(stringLiteral: "Test"), "␣: Test"),
1312
#line: (TypeAnnotation("Test"), "␣: Test"),
1413
#line: (SimpleTypeIdentifier("Test"), "␣: Test"),
1514
#line: ("Test", "␣: Test")

0 commit comments

Comments
 (0)