Skip to content

Add ExtensionDecl test #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ import SwiftSyntax

extension BinaryOperatorExpr {
public init(_ text: String) {
self.init(operatorToken: TokenSyntax.spacedBinaryOperator(text))
self.init(operatorToken: .spacedBinaryOperator(text))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension FunctionCallExpr {
leftParen: TokenSyntax? = nil,
rightParen: TokenSyntax? = nil,
trailingClosure: ExpressibleAsClosureExpr? = nil,
@TupleExprElementListBuilder argumentListBuilder: () -> TupleExprElementList = { .empty },
@TupleExprElementListBuilder argumentListBuilder: () -> ExpressibleAsTupleExprElementList = { TupleExprElementList.empty },
@MultipleTrailingClosureElementListBuilder additionalTrailingClosuresBuilder: () -> MultipleTrailingClosureElementList? = { nil }
) {
self.init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,3 @@ extension IdentifierExpr {
self.init(identifier: .identifier(identifier))
}
}

extension IdentifierExpr: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,3 @@ extension IdentifierPattern {
self.init(identifier: .identifier(identifier))
}
}

extension IdentifierPattern: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ import SwiftSyntax

extension SimpleTypeIdentifier {
public init(_ name: String) {
self.init(name: SyntaxFactory.makeIdentifier(name),
self.init(name: .identifier(name),
genericArgumentClause: nil)
}
}

extension SimpleTypeIdentifier: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import SwiftSyntax

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

Expand All @@ -23,9 +23,3 @@ extension StringLiteralExpr {
closeQuote: closeQuote)
}
}

extension StringLiteralExpr: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,3 @@ extension TypeAnnotation {
self.init(type: SimpleTypeIdentifier(type))
}
}

extension TypeAnnotation: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}
68 changes: 52 additions & 16 deletions Sources/SwiftSyntaxBuilder/gyb_generated/Buildables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public struct CodeBlockItemList: SyntaxBuildable {
}
}

public protocol ExpressibleAsCodeBlockItemList {
public protocol ExpressibleAsCodeBlockItemList: ExpressibleAsCodeBlock {
func createCodeBlockItemList() -> CodeBlockItemList
}

Expand Down Expand Up @@ -1237,7 +1237,7 @@ public struct SequenceExpr: ExprBuildable {
}
}

public protocol ExpressibleAsSequenceExpr {
public protocol ExpressibleAsSequenceExpr: ExpressibleAsCodeBlockItem, ExpressibleAsExprBuildable, ExpressibleAsTupleExprElement {
func createSequenceExpr() -> SequenceExpr
}

Expand Down Expand Up @@ -3005,7 +3005,7 @@ public struct FunctionCallExpr: ExprBuildable {
}
}

public protocol ExpressibleAsFunctionCallExpr {
public protocol ExpressibleAsFunctionCallExpr: ExpressibleAsCodeBlockItem, ExpressibleAsExprBuildable {
func createFunctionCallExpr() -> FunctionCallExpr
}

Expand Down Expand Up @@ -6189,7 +6189,7 @@ public struct AccessorList: SyntaxBuildable {
}
}

public protocol ExpressibleAsAccessorList {
public protocol ExpressibleAsAccessorList: ExpressibleAsAccessorBlock {
func createAccessorList() -> AccessorList
}

Expand Down Expand Up @@ -12604,7 +12604,7 @@ extension VersionTuple: ExpressibleAsVersionTuple {
}
}

extension TokenSyntax: ExpressibleAsIdentifierList, ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList, ExpressibleAsBinaryOperatorExpr {
extension TokenSyntax: ExpressibleAsIdentifierList, ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList, ExpressibleAsBinaryOperatorExpr, ExpressibleAsDeclModifier {
}

// MARK: - Syntax Collection buildable expressible as conformances
Expand Down Expand Up @@ -12857,33 +12857,33 @@ extension ExpressibleAsPrecedenceGroupNameElement {

// MARK: - Syntax buildable expressible as conformances

extension ExpressibleAsStmtBuildable {
extension ExpressibleAsFunctionCallExpr {
public func createCodeBlockItem() -> CodeBlockItem {
CodeBlockItem(item: self)
}
}

extension ExpressibleAsMemberDeclList {
public func createMemberDeclBlock() -> MemberDeclBlock {
MemberDeclBlock(members: self)
extension ExpressibleAsCodeBlockItemList {
public func createCodeBlock() -> CodeBlock {
CodeBlock(statements: self)
}
}

extension TokenSyntax {
public func createBinaryOperatorExpr() -> BinaryOperatorExpr {
BinaryOperatorExpr(operatorToken: self)
extension ExpressibleAsMemberDeclList {
public func createMemberDeclBlock() -> MemberDeclBlock {
MemberDeclBlock(members: self)
}
}

extension ExpressibleAsDeclBuildable {
extension ExpressibleAsSequenceExpr {
public func createCodeBlockItem() -> CodeBlockItem {
CodeBlockItem(item: self)
}
}

extension ExpressibleAsDeclBuildable {
public func createMemberDeclListItem() -> MemberDeclListItem {
MemberDeclListItem(decl: self)
extension ExpressibleAsSequenceExpr {
public func createTupleExprElement() -> TupleExprElement {
TupleExprElement(expression: self)
}
}

Expand All @@ -12905,3 +12905,39 @@ extension ExpressibleAsExprList {
}
}

extension ExpressibleAsStmtBuildable {
public func createCodeBlockItem() -> CodeBlockItem {
CodeBlockItem(item: self)
}
}

extension ExpressibleAsDeclBuildable {
public func createCodeBlockItem() -> CodeBlockItem {
CodeBlockItem(item: self)
}
}

extension ExpressibleAsDeclBuildable {
public func createMemberDeclListItem() -> MemberDeclListItem {
MemberDeclListItem(decl: self)
}
}

extension ExpressibleAsAccessorList {
public func createAccessorBlock() -> AccessorBlock {
AccessorBlock(accessors: self)
}
}

extension TokenSyntax {
public func createBinaryOperatorExpr() -> BinaryOperatorExpr {
BinaryOperatorExpr(operatorToken: self)
}
}

extension TokenSyntax {
public func createDeclModifier() -> DeclModifier {
DeclModifier(name: self)
}
}

4 changes: 2 additions & 2 deletions Tests/SwiftSyntaxBuilderTest/BinaryOperatorExprTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ final class BinaryOperatorExprTests: XCTestCase {
let (builder, expected) = testCase
let binaryOperatorExpr = builder.createBinaryOperatorExpr()
let syntax = binaryOperatorExpr.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)

var text = ""
syntax.write(to: &text)

XCTAssertEqual(text, expected, line: line)
}
}
Expand Down
46 changes: 46 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/ExtensionDeclTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import XCTest
import SwiftSyntax
import SwiftSyntaxBuilder

final class ExtensionDeclTests: XCTestCase {
func testExtensionDecl() {
let keywords = ["associatedtype", "class"].map { keyword -> VariableDecl in
// We need to use `CodeBlock` here to ensure there is braces around.
let body = CodeBlock {
FunctionCallExpr("SyntaxFactory.make\(keyword)Keyword",
leftParen: .leftParen,
rightParen: .rightParen)
}

return VariableDecl(letOrVarKeyword: .var,
modifiersBuilder: { TokenSyntax.public },
bindingsBuilder: {
PatternBinding(pattern: "`\(keyword)`",
typeAnnotation: "TokenSyntax",
initializer: nil,
accessor: body)

})
}
let members = MemberDeclList(keywords)
let buildable = ExtensionDecl(modifiers: nil,
extendedType: "TokenSyntax",
members: members)

let syntax = buildable.buildSyntax(format: Format())

var text = ""
syntax.write(to: &text)

XCTAssertEqual(text, """
extension TokenSyntax{
public var `associatedtype`: TokenSyntax{
SyntaxFactory.makeassociatedtypeKeyword()
}
public var `class`: TokenSyntax{
SyntaxFactory.makeclassKeyword()
}
}
""")
}
}
9 changes: 3 additions & 6 deletions Tests/SwiftSyntaxBuilderTest/FunctionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ import SwiftSyntax
import SwiftSyntaxBuilder

final class FunctionTests: XCTestCase {
func testEmptyStruct() {
func testFibonacci() {
let leadingTrivia = Trivia.garbageText("␣")

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

let ifCodeBlock = CodeBlock(statementsBuilder: {
ReturnStmt(expression: IntegerLiteralExpr(digits: "n"))
})

let ifCodeBlock = ReturnStmt(expression: IntegerLiteralExpr(digits: "n"))

let signature = FunctionSignature(input: input, output: "Int")


let codeBlock = CodeBlock(statementsBuilder: {
IfStmt(conditions: ExprList([
IntegerLiteralExpr(digits: "n"),
Expand Down
1 change: 0 additions & 1 deletion Tests/SwiftSyntaxBuilderTest/IdentifierExprTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ final class IdentifierExprTests: XCTestCase {

let testCases: [UInt: (ExpressibleAsIdentifierExpr, String)] = [
#line: (IdentifierExpr(identifier: .identifier("Test")), "␣Test"),
#line: (IdentifierExpr(stringLiteral: "Test"), "␣Test"),
#line: (IdentifierExpr("Test"), "␣Test"),
#line: ("Test", "␣Test")
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ final class IdentifierPatternTests: XCTestCase {

let testCases: [UInt: (ExpressibleAsIdentifierPattern, String)] = [
#line: (IdentifierPattern(identifier: .identifier("Test")), "␣Test"),
#line: (IdentifierPattern(stringLiteral: "Test"), "␣Test"),
#line: (IdentifierPattern("Test"), "␣Test"),
#line: ("Test", "␣Test")
]
Expand Down
1 change: 0 additions & 1 deletion Tests/SwiftSyntaxBuilderTest/TypeAnnotationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ final class TypeAnnotationTests: XCTestCase {
let testCases: [UInt: (ExpressibleAsTypeAnnotation, String)] = [
#line: (TypeAnnotation(type: "Test"), "␣: Test"),
#line: (TypeAnnotation(type: SimpleTypeIdentifier("Test")), "␣: Test"),
#line: (TypeAnnotation(stringLiteral: "Test"), "␣: Test"),
#line: (TypeAnnotation("Test"), "␣: Test"),
#line: (SimpleTypeIdentifier("Test"), "␣: Test"),
#line: ("Test", "␣: Test")
Expand Down