|
| 1 | +import XCTest |
| 2 | +import SwiftSyntax |
| 3 | +import SwiftSyntaxBuilder |
| 4 | + |
| 5 | +final class FunctionTests: XCTestCase { |
| 6 | + func testEmptyStruct() { |
| 7 | + let leadingTrivia = Trivia.garbageText("␣") |
| 8 | + |
| 9 | + let input = ParameterClause(parameterListBuilder: { |
| 10 | + FunctionParameter(firstName: .wildcard, secondName: .identifier("n"), colon: .colon, type: SimpleTypeIdentifier("Int"), attributesBuilder: {}) |
| 11 | + }) |
| 12 | + |
| 13 | + let ifCodeBlock = CodeBlock(statementsBuilder: { |
| 14 | + ReturnStmt(expression: SequenceExpr(elementsBuilder: { |
| 15 | + IntegerLiteralExpr(digits: "n") |
| 16 | + })) |
| 17 | + }) |
| 18 | + |
| 19 | + let signature = FunctionSignature(input: input, output: ReturnClause(returnType: SimpleTypeIdentifier("Int"))) |
| 20 | + |
| 21 | + |
| 22 | + let codeBlock = CodeBlock(statementsBuilder: { |
| 23 | + IfStmt(labelName: nil, body: ifCodeBlock, conditionsBuilder: { |
| 24 | + ConditionElement(condition: ExprList([ |
| 25 | + IntegerLiteralExpr(digits: "n"), |
| 26 | + |
| 27 | + BinaryOperatorExpr(operatorToken: .unspacedBinaryOperator("<=")), |
| 28 | + |
| 29 | + IntegerLiteralExpr(1) |
| 30 | + ])) |
| 31 | + }) |
| 32 | + |
| 33 | + ReturnStmt(expression: SequenceExpr(elementsBuilder: { |
| 34 | + FunctionCallExpr(calledExpression: IdentifierExpr(identifier: .identifier("fibonacci")), leftParen: .leftParen, rightParen: .rightParen, argumentListBuilder: { |
| 35 | + TupleExprElement(expression: SequenceExpr(elementsBuilder: { |
| 36 | + IntegerLiteralExpr(digits: "n") |
| 37 | + |
| 38 | + BinaryOperatorExpr(operatorToken: .unspacedBinaryOperator("-")) |
| 39 | + |
| 40 | + IntegerLiteralExpr(1) |
| 41 | + })) |
| 42 | + }) |
| 43 | + |
| 44 | + BinaryOperatorExpr(operatorToken: .spacedBinaryOperator("+")) |
| 45 | + |
| 46 | + FunctionCallExpr(calledExpression: IdentifierExpr(identifier: .identifier("fibonacci")), leftParen: .leftParen, rightParen: .rightParen, argumentListBuilder: { |
| 47 | + TupleExprElement(expression: SequenceExpr(elementsBuilder: { |
| 48 | + IntegerLiteralExpr(digits: "n") |
| 49 | + |
| 50 | + BinaryOperatorExpr(operatorToken: .unspacedBinaryOperator("-")) |
| 51 | + |
| 52 | + IntegerLiteralExpr(2) |
| 53 | + })) |
| 54 | + }) |
| 55 | + })) |
| 56 | + }) |
| 57 | + |
| 58 | + let buildable = FunctionDecl(identifier: .identifier("fibonacci"), signature: signature, body: codeBlock, attributesBuilder: {}) |
| 59 | + let syntax = buildable.buildSyntax(format: Format(), leadingTrivia: leadingTrivia) |
| 60 | + |
| 61 | + XCTAssertEqual(syntax.description, """ |
| 62 | + func fibonacci(_ n: Int)-> Int{ |
| 63 | + if n<=1{ |
| 64 | + return n |
| 65 | + } |
| 66 | + return fibonacci(n-1)+fibonacci(n-2) |
| 67 | + } |
| 68 | + """) |
| 69 | + } |
| 70 | +} |
0 commit comments