|
| 1 | +// |
| 2 | +// Copyright © 2024 Sage. |
| 3 | +// All Rights Reserved. |
| 4 | + |
| 5 | +import Foundation |
| 6 | +import SwiftSyntax |
| 7 | +import SwiftSyntaxMacros |
| 8 | +import SwiftSyntaxBuilder |
| 9 | + |
| 10 | +// Creates the mock for each function with its parameters calls retun value etc... |
| 11 | +struct ClassMockForFunctionBuilder { |
| 12 | + let funcData: FunctionsMockData |
| 13 | + |
| 14 | + var parametersName: String { "Parameters" } |
| 15 | + var callsName: String { "calls" } |
| 16 | + |
| 17 | + init(funcData: FunctionsMockData) { |
| 18 | + self.funcData = funcData |
| 19 | + } |
| 20 | + |
| 21 | + func build() -> ClassDeclSyntax { |
| 22 | + ClassDeclSyntax( |
| 23 | + modifiers: .init( |
| 24 | + itemsBuilder: { |
| 25 | + .init(name: funcData.accessLevel) |
| 26 | + } |
| 27 | + ), |
| 28 | + name: funcData.className.tokenSyntax, |
| 29 | + memberBlock: .init( |
| 30 | + members: .init( |
| 31 | + itemsBuilder: { |
| 32 | + if let parametersStruct = buildParameters() { |
| 33 | + parametersStruct |
| 34 | + } |
| 35 | + |
| 36 | + buildCalls() |
| 37 | + buildLastCall() |
| 38 | + |
| 39 | + buildCalled() |
| 40 | + |
| 41 | + if let returnValue = funcData.returnValue { |
| 42 | + VariableDeclSyntax( |
| 43 | + modifiers: .init(itemsBuilder: { |
| 44 | + .init(name: funcData.accessLevel) |
| 45 | + }), |
| 46 | + Keyword.var, |
| 47 | + name: "returnValue", |
| 48 | + type: TypeAnnotationSyntax( |
| 49 | + type: TypeSyntax(stringLiteral: returnValue) |
| 50 | + ) |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + InitializerDeclSyntax( |
| 55 | + signature: .init(parameterClause: .init( |
| 56 | + parameters: .init(itemsBuilder: { |
| 57 | + |
| 58 | + }) |
| 59 | + )), |
| 60 | + body: .init(statements: .init(itemsBuilder: { |
| 61 | + |
| 62 | + })) |
| 63 | + ) |
| 64 | + } |
| 65 | + ) |
| 66 | + ) |
| 67 | + ) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +extension ClassMockForFunctionBuilder { |
| 72 | + func buildParameters() -> StructDeclSyntax? { |
| 73 | + return StructDeclSyntax( |
| 74 | + modifiers: .init(itemsBuilder: { |
| 75 | + .init(name: funcData.accessLevel) |
| 76 | + }), |
| 77 | + name: parametersName.tokenSyntax, |
| 78 | + memberBlock: .init(members: .init(itemsBuilder: { |
| 79 | + for parameter in funcData.params { |
| 80 | + VariableDeclSyntax( |
| 81 | + modifiers: .init(itemsBuilder: { |
| 82 | + .init(name: funcData.accessLevel) |
| 83 | + }), |
| 84 | + Keyword.let, |
| 85 | + name: PatternSyntax(stringLiteral: parameter.name), |
| 86 | + type: TypeAnnotationSyntax( |
| 87 | + type: TypeSyntax(stringLiteral: parameter.noEscapingType) |
| 88 | + ) |
| 89 | + ) |
| 90 | + } |
| 91 | + })) |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + func buildCalls() -> VariableDeclSyntax { |
| 96 | + return VariableDeclSyntax( |
| 97 | + modifiers: .init(itemsBuilder: { |
| 98 | + .init(name: funcData.accessLevel) |
| 99 | + }), |
| 100 | + Keyword.var, |
| 101 | + name: .init(stringLiteral: callsName), |
| 102 | + type: TypeAnnotationSyntax( |
| 103 | + type: TypeSyntax(stringLiteral: "[\(parametersName)]") |
| 104 | + ), |
| 105 | + initializer: InitializerClauseSyntax( |
| 106 | + value: ArrayExprSyntax(elements: .init(itemsBuilder: { |
| 107 | + |
| 108 | + })) |
| 109 | + ) |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + func buildCalled() -> VariableDeclSyntax { |
| 114 | + return VariableDeclSyntax( |
| 115 | + modifiers: .init(itemsBuilder: { |
| 116 | + .init(name: funcData.accessLevel) |
| 117 | + }), |
| 118 | + bindingSpecifier: .keyword(.var), |
| 119 | + bindings: .init(itemsBuilder: { |
| 120 | + PatternBindingSyntax( |
| 121 | + pattern: IdentifierPatternSyntax(identifier: "called"), |
| 122 | + typeAnnotation: TypeAnnotationSyntax(type: IdentifierTypeSyntax(name: "Bool")), |
| 123 | + accessorBlock: AccessorBlockSyntax( |
| 124 | + leftBrace: .leftBraceToken(), |
| 125 | + accessors: .init(CodeBlockItemListSyntax(itemsBuilder: { |
| 126 | + ReturnStmtSyntax( |
| 127 | + returnKeyword: .keyword(.return), |
| 128 | + expression: DeclReferenceExprSyntax( |
| 129 | + baseName: "self.lastCall != nil".tokenSyntax |
| 130 | + ) |
| 131 | + ) |
| 132 | + })), |
| 133 | + rightBrace: .rightBraceToken() |
| 134 | + ) |
| 135 | + ) |
| 136 | + }) |
| 137 | + ) |
| 138 | + } |
| 139 | + |
| 140 | + func buildLastCall() -> VariableDeclSyntax { |
| 141 | + return VariableDeclSyntax( |
| 142 | + modifiers: .init(itemsBuilder: { |
| 143 | + .init(name: funcData.accessLevel) |
| 144 | + }), |
| 145 | + bindingSpecifier: .keyword(.var), |
| 146 | + bindings: .init(itemsBuilder: { |
| 147 | + PatternBindingSyntax( |
| 148 | + pattern: IdentifierPatternSyntax(identifier: "lastCall"), |
| 149 | + typeAnnotation: TypeAnnotationSyntax( |
| 150 | + type: OptionalTypeSyntax( |
| 151 | + wrappedType: IdentifierTypeSyntax(name: parametersName.tokenSyntax) |
| 152 | + ) |
| 153 | + ), |
| 154 | + accessorBlock: AccessorBlockSyntax( |
| 155 | + leftBrace: .leftBraceToken(), |
| 156 | + accessors: .init(CodeBlockItemListSyntax(itemsBuilder: { |
| 157 | + ReturnStmtSyntax( |
| 158 | + returnKeyword: .keyword(.return), |
| 159 | + expression: DeclReferenceExprSyntax( |
| 160 | + baseName: "self.calls.last".tokenSyntax |
| 161 | + ) |
| 162 | + ) |
| 163 | + })), |
| 164 | + rightBrace: .rightBraceToken() |
| 165 | + ) |
| 166 | + ) |
| 167 | + }) |
| 168 | + ) |
| 169 | + } |
| 170 | +} |
0 commit comments