Skip to content

Commit 24316cc

Browse files
authored
Merge pull request #331 from kimdv/kimdv/add-extension-test
Add `ExtensionDecl` test
2 parents 8b60985 + bfe458a commit 24316cc

14 files changed

+107
-61
lines changed

Sources/SwiftSyntaxBuilder/BinaryOperatorExprConvenienceInitializers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ import SwiftSyntax
1414

1515
extension BinaryOperatorExpr {
1616
public init(_ text: String) {
17-
self.init(operatorToken: TokenSyntax.spacedBinaryOperator(text))
17+
self.init(operatorToken: .spacedBinaryOperator(text))
1818
}
1919
}

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/StringLiteralExprConvenienceInitializers.swift

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

1515
extension StringLiteralExpr {
1616
public init(_ value: String, openQuote: TokenSyntax = .stringQuote, closeQuote: TokenSyntax = .stringQuote) {
17-
let content = SyntaxFactory.makeToken(TokenKind.stringSegment(value), presence: .present)
17+
let content = SyntaxFactory.makeToken(.stringSegment(value), presence: .present)
1818
let segment = StringSegment(content: content)
1919
let segments = StringLiteralSegments([segment])
2020

@@ -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 & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ public struct CodeBlockItemList: SyntaxBuildable {
435435
}
436436
}
437437

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

@@ -1237,7 +1237,7 @@ public struct SequenceExpr: ExprBuildable {
12371237
}
12381238
}
12391239

1240-
public protocol ExpressibleAsSequenceExpr {
1240+
public protocol ExpressibleAsSequenceExpr: ExpressibleAsCodeBlockItem, ExpressibleAsExprBuildable, ExpressibleAsTupleExprElement {
12411241
func createSequenceExpr() -> SequenceExpr
12421242
}
12431243

@@ -3005,7 +3005,7 @@ public struct FunctionCallExpr: ExprBuildable {
30053005
}
30063006
}
30073007

3008-
public protocol ExpressibleAsFunctionCallExpr {
3008+
public protocol ExpressibleAsFunctionCallExpr: ExpressibleAsCodeBlockItem, ExpressibleAsExprBuildable {
30093009
func createFunctionCallExpr() -> FunctionCallExpr
30103010
}
30113011

@@ -6189,7 +6189,7 @@ public struct AccessorList: SyntaxBuildable {
61896189
}
61906190
}
61916191

6192-
public protocol ExpressibleAsAccessorList {
6192+
public protocol ExpressibleAsAccessorList: ExpressibleAsAccessorBlock {
61936193
func createAccessorList() -> AccessorList
61946194
}
61956195

@@ -12604,7 +12604,7 @@ extension VersionTuple: ExpressibleAsVersionTuple {
1260412604
}
1260512605
}
1260612606

12607-
extension TokenSyntax: ExpressibleAsIdentifierList, ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList, ExpressibleAsBinaryOperatorExpr {
12607+
extension TokenSyntax: ExpressibleAsIdentifierList, ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList, ExpressibleAsBinaryOperatorExpr, ExpressibleAsDeclModifier {
1260812608
}
1260912609

1261012610
// MARK: - Syntax Collection buildable expressible as conformances
@@ -12857,33 +12857,33 @@ extension ExpressibleAsPrecedenceGroupNameElement {
1285712857

1285812858
// MARK: - Syntax buildable expressible as conformances
1285912859

12860-
extension ExpressibleAsStmtBuildable {
12860+
extension ExpressibleAsFunctionCallExpr {
1286112861
public func createCodeBlockItem() -> CodeBlockItem {
1286212862
CodeBlockItem(item: self)
1286312863
}
1286412864
}
1286512865

12866-
extension ExpressibleAsMemberDeclList {
12867-
public func createMemberDeclBlock() -> MemberDeclBlock {
12868-
MemberDeclBlock(members: self)
12866+
extension ExpressibleAsCodeBlockItemList {
12867+
public func createCodeBlock() -> CodeBlock {
12868+
CodeBlock(statements: self)
1286912869
}
1287012870
}
1287112871

12872-
extension TokenSyntax {
12873-
public func createBinaryOperatorExpr() -> BinaryOperatorExpr {
12874-
BinaryOperatorExpr(operatorToken: self)
12872+
extension ExpressibleAsMemberDeclList {
12873+
public func createMemberDeclBlock() -> MemberDeclBlock {
12874+
MemberDeclBlock(members: self)
1287512875
}
1287612876
}
1287712877

12878-
extension ExpressibleAsDeclBuildable {
12878+
extension ExpressibleAsSequenceExpr {
1287912879
public func createCodeBlockItem() -> CodeBlockItem {
1288012880
CodeBlockItem(item: self)
1288112881
}
1288212882
}
1288312883

12884-
extension ExpressibleAsDeclBuildable {
12885-
public func createMemberDeclListItem() -> MemberDeclListItem {
12886-
MemberDeclListItem(decl: self)
12884+
extension ExpressibleAsSequenceExpr {
12885+
public func createTupleExprElement() -> TupleExprElement {
12886+
TupleExprElement(expression: self)
1288712887
}
1288812888
}
1288912889

@@ -12905,3 +12905,39 @@ extension ExpressibleAsExprList {
1290512905
}
1290612906
}
1290712907

12908+
extension ExpressibleAsStmtBuildable {
12909+
public func createCodeBlockItem() -> CodeBlockItem {
12910+
CodeBlockItem(item: self)
12911+
}
12912+
}
12913+
12914+
extension ExpressibleAsDeclBuildable {
12915+
public func createCodeBlockItem() -> CodeBlockItem {
12916+
CodeBlockItem(item: self)
12917+
}
12918+
}
12919+
12920+
extension ExpressibleAsDeclBuildable {
12921+
public func createMemberDeclListItem() -> MemberDeclListItem {
12922+
MemberDeclListItem(decl: self)
12923+
}
12924+
}
12925+
12926+
extension ExpressibleAsAccessorList {
12927+
public func createAccessorBlock() -> AccessorBlock {
12928+
AccessorBlock(accessors: self)
12929+
}
12930+
}
12931+
12932+
extension TokenSyntax {
12933+
public func createBinaryOperatorExpr() -> BinaryOperatorExpr {
12934+
BinaryOperatorExpr(operatorToken: self)
12935+
}
12936+
}
12937+
12938+
extension TokenSyntax {
12939+
public func createDeclModifier() -> DeclModifier {
12940+
DeclModifier(name: self)
12941+
}
12942+
}
12943+

Tests/SwiftSyntaxBuilderTest/BinaryOperatorExprTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ final class BinaryOperatorExprTests: XCTestCase {
1515
let (builder, expected) = testCase
1616
let binaryOperatorExpr = builder.createBinaryOperatorExpr()
1717
let syntax = binaryOperatorExpr.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
18-
18+
1919
var text = ""
2020
syntax.write(to: &text)
21-
21+
2222
XCTAssertEqual(text, expected, line: line)
2323
}
2424
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import XCTest
2+
import SwiftSyntax
3+
import SwiftSyntaxBuilder
4+
5+
final class ExtensionDeclTests: XCTestCase {
6+
func testExtensionDecl() {
7+
let keywords = ["associatedtype", "class"].map { keyword -> VariableDecl in
8+
// We need to use `CodeBlock` here to ensure there is braces around.
9+
let body = CodeBlock {
10+
FunctionCallExpr("SyntaxFactory.make\(keyword)Keyword",
11+
leftParen: .leftParen,
12+
rightParen: .rightParen)
13+
}
14+
15+
return VariableDecl(letOrVarKeyword: .var,
16+
modifiersBuilder: { TokenSyntax.public },
17+
bindingsBuilder: {
18+
PatternBinding(pattern: "`\(keyword)`",
19+
typeAnnotation: "TokenSyntax",
20+
initializer: nil,
21+
accessor: body)
22+
23+
})
24+
}
25+
let members = MemberDeclList(keywords)
26+
let buildable = ExtensionDecl(modifiers: nil,
27+
extendedType: "TokenSyntax",
28+
members: members)
29+
30+
let syntax = buildable.buildSyntax(format: Format())
31+
32+
var text = ""
33+
syntax.write(to: &text)
34+
35+
XCTAssertEqual(text, """
36+
extension TokenSyntax{
37+
public var `associatedtype`: TokenSyntax{
38+
SyntaxFactory.makeassociatedtypeKeyword()
39+
}
40+
public var `class`: TokenSyntax{
41+
SyntaxFactory.makeclassKeyword()
42+
}
43+
}
44+
""")
45+
}
46+
}

Tests/SwiftSyntaxBuilderTest/FunctionTests.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@ 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-
13+
let ifCodeBlock = ReturnStmt(expression: IntegerLiteralExpr(digits: "n"))
14+
1715
let signature = FunctionSignature(input: input, output: "Int")
1816

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

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)