Skip to content

Commit c1d767e

Browse files
authored
Merge pull request #356 from kimdv/kimdv/add-protocol-test
Add `ProtocolDecl` test cases
2 parents 099950a + 5f01be6 commit c1d767e

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

Sources/SwiftSyntaxBuilder/gyb_generated/ExpressibleAsProtocols.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,17 @@ public extension ExpressibleAsSyntaxBuildable {
8282
}
8383
}
8484

85-
public protocol ExpressibleAsTypeBuildable {
85+
public protocol ExpressibleAsTypeBuildable: ExpressibleAsReturnClause {
8686
func createTypeBuildable() -> TypeBuildable
8787
}
8888

89+
public extension ExpressibleAsTypeBuildable {
90+
/// Conformance to `ExpressibleAsReturnClause`.
91+
func createReturnClause() -> ReturnClause {
92+
return ReturnClause(returnType: self)
93+
}
94+
}
95+
8996
public protocol ExpressibleAsCodeBlockItem: ExpressibleAsCodeBlockItemList {
9097
func createCodeBlockItem() -> CodeBlockItem
9198
}

Sources/SwiftSyntaxBuilder/gyb_helpers/ExpressibleAsConformances.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
'BinaryOperatorExpr',
3434
'DeclModifier',
3535
'IdentifierExpr'
36+
],
37+
'TypeBuildable': [
38+
'ReturnClause'
3639
]
3740
}
3841

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import XCTest
2+
import SwiftSyntax
3+
import SwiftSyntaxBuilder
4+
5+
final class ProtocolDeclTests: XCTestCase {
6+
func testProtocolDecl() {
7+
let returnType = ArrayType(elementType: "DeclSyntax")
8+
let input = ParameterClause(parameterListBuilder: {
9+
FunctionParameter(firstName: .identifier("format"), colon: .colon, type: "Format", trailingComma: .comma, attributesBuilder: {})
10+
FunctionParameter(firstName: .identifier("leadingTrivia"), colon: .colon, type: OptionalType(wrappedType: "Trivia"), attributesBuilder: {})
11+
})
12+
let functionSignature = FunctionSignature(input: input, output: returnType)
13+
14+
// FIXME: We need to add the `modifiersBuilder` with a non-empty value, otherwise will the builder omit newline.
15+
let functionDecl = FunctionDecl(identifier: .identifier("buildDeclList"), signature: functionSignature, modifiersBuilder: { TokenSyntax.public })
16+
let buildable = ProtocolDecl(modifiers: TokenSyntax.public, identifier: .identifier("DeclListBuildable"), members: functionDecl)
17+
18+
let syntax = buildable.buildSyntax(format: Format())
19+
20+
var text = ""
21+
syntax.write(to: &text)
22+
23+
XCTAssertEqual(text, """
24+
public protocol DeclListBuildable{
25+
public func buildDeclList(format: Format, leadingTrivia: Trivia?)-> [DeclSyntax]
26+
}
27+
""")
28+
}
29+
}

Tests/SwiftSyntaxBuilderTest/ReturnClauseTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ final class ReturnClauseTests: XCTestCase {
88

99
let testCases: [UInt: (ExpressibleAsReturnClause, String)] = [
1010
#line: (ReturnClause(returnType: "Int"), "␣-> Int"),
11+
#line: (ArrayType(elementType: "Int"), "␣-> [Int]"),
1112
#line: ("Int", "␣-> Int"),
1213
]
1314

0 commit comments

Comments
 (0)