|
1 |
| -<%# ================================================== SETUP -%><%_ -%> |
2 |
| -<%_ var all = types.all |
3 |
| - all += types.protocols.map { $0 } |
4 |
| - all += types.protocolCompositions.map { $0 } |
| 1 | +func generate(type: Type, methodRegistrar: MethodRegistrar, subscriptRegistrar: SubscriptRegistrar) async -> String { |
| 2 | + var bufferStr: String = "" |
5 | 3 | var mockedCount = 0
|
6 |
| --%> |
7 |
| - |
8 |
| -<%_ for type in all { -%><%_ -%> |
9 |
| -<%_ let autoMockable: Bool = type.inheritedTypes.contains("AutoMockable") || type.annotations["AutoMockable"] != nil |
| 4 | + let autoMockable: Bool = type.inheritedTypes.contains("AutoMockable") || type.annotations["AutoMockable"] != nil |
10 | 5 | let protocolToDecorate = types.protocols.first(where: { $0.name == (type.annotations["mock"] as? String) })
|
11 |
| - let inlineMockable = protocolToDecorate != nil |
12 |
| - guard let aProtocol = autoMockable ? type : protocolToDecorate else { continue } |
| 6 | + guard let aProtocol = autoMockable ? type : protocolToDecorate else { return bufferStr } |
13 | 7 | mockedCount += 1
|
14 |
| - |
| 8 | + var current = Current() |
15 | 9 | let associatedTypes: [String]? = Helpers.extractAssociatedTypes(from: aProtocol)
|
16 | 10 | let attributes: String = Helpers.extractAttributes(from: type.attributes)
|
17 | 11 | let typeAliases: [String] = Helpers.extractTypealiases(from: aProtocol)
|
18 | 12 | let genericTypesModifier: String = Helpers.extractGenericTypesModifier(associatedTypes)
|
19 | 13 | let genericTypesConstraints: String = Helpers.extractGenericTypesConstraints(associatedTypes)
|
20 | 14 | let allSubscripts = aProtocol.allSubscripts
|
21 | 15 | let allVariables = uniques(variables: aProtocol.allVariables.filter({ !$0.isStatic }))
|
22 |
| - let containsVariables = !allVariables.isEmpty |
23 | 16 | let allStaticVariables = uniques(variables: aProtocol.allVariables.filter({ $0.isStatic }))
|
24 |
| - let containsStaticVariables = !allStaticVariables.isEmpty |
25 | 17 | let allMethods = uniques(methods: aProtocol.allMethods.filter({ !$0.isStatic || $0.isInitializer }))
|
26 |
| - let selfConstrained = allMethods.map(wrapMethod).contains(where: { $0.returnsGenericConstrainedToSelf || $0.parametersContainsSelf }) |
| 18 | + let selfConstrained = allMethods.map { wrapMethod($0, current: current, methodRegistrar: methodRegistrar) }.contains(where: { $0.returnsGenericConstrainedToSelf || $0.parametersContainsSelf }) |
27 | 19 | let accessModifier: String = selfConstrained ? "public final" : "open"
|
28 |
| - Current.accessModifier = accessModifier // TODO: Temporary workaround for access modifiers |
| 20 | + current.accessModifier = accessModifier // TODO: Temporary workaround for access modifiers |
29 | 21 | let inheritFromNSObject = type.annotations["ObjcProtocol"] != nil || attributes.contains("@objc")
|
30 | 22 | let allMethodsForMethodType = uniquesWithoutGenericConstraints(methods: aProtocol.allMethods.filter({ !$0.isStatic }))
|
31 | 23 | let allStaticMethods = uniques(methods: aProtocol.allMethods.filter({ $0.isStatic && !$0.isInitializer }))
|
32 | 24 | let allStaticMethodsForMethodType = uniquesWithoutGenericConstraints(methods: aProtocol.allMethods.filter({ $0.isStatic }))
|
33 |
| - let conformsToStaticMock = !allStaticMethods.isEmpty || !allStaticVariables.isEmpty |
34 |
| - let conformsToMock = !allMethods.isEmpty || !allVariables.isEmpty -%><%_ -%><%_ -%> |
| 25 | + let conformsToStaticMock = !allStaticMethods.isEmpty || !allStaticVariables.isEmpty-%><%_ -%><%_ -%> |
35 | 26 | <%_ if autoMockable { -%>
|
36 | 27 | // MARK: - <%= type.name %>
|
37 | 28 | <%= attributes %>
|
|
105 | 96 | <%# ================================================== VARIABLES -%><%_ -%>
|
106 | 97 | <%_ for variable in allVariables { -%>
|
107 | 98 | <%_ if autoMockable { -%>
|
108 |
| - <%= stubProperty(variable,"\(type.name)\(mockTypeName)") %> |
| 99 | + <%= stubProperty(variable,"\(type.name)\(mockTypeName)", current: current) %> |
109 | 100 | <%_ } else { %>
|
110 |
| - <%= stubProperty(variable,"\(type.name)") %> |
| 101 | + <%= stubProperty(variable,"\(type.name)", current: current) %> |
111 | 102 | <%_ } %>
|
112 | 103 | <%_ } %> <%_ -%>
|
113 | 104 |
|
114 | 105 | <%# ================================================== STATIC VARIABLES -%><%_ -%>
|
115 | 106 | <%_ for variable in allStaticVariables { -%>
|
116 | 107 | <%_ if autoMockable { -%>
|
117 |
| - <%= stubProperty(variable,"\(type.name)\(mockTypeName)") %> |
| 108 | + <%= stubProperty(variable,"\(type.name)\(mockTypeName)", current: current) %> |
118 | 109 | <%_ } else { %>
|
119 |
| - <%= stubProperty(variable,"\(type.name)") %> |
| 110 | + <%= stubProperty(variable,"\(type.name)", current: current) %> |
120 | 111 | <%_ } %>
|
121 | 112 | <%_ } %> <%_ -%>
|
122 | 113 |
|
123 | 114 | <%# ================================================== METHOD REGISTRATIONS -%><%_ -%>
|
124 |
| - <%_ MethodWrapper.clear() -%> |
125 |
| - <%_ SubscriptWrapper.clear() -%> |
126 | 115 | <%_ if autoMockable { -%>
|
127 |
| - <%_ Current.selfType = "\(type.name)\(mockTypeName)\(genericTypesModifier)" -%> |
| 116 | + <%_ current.selfType = "\(type.name)\(mockTypeName)\(genericTypesModifier)" -%> |
128 | 117 | <%_ } else { %>
|
129 |
| - <%_ Current.selfType = "\(type.name)\(mockTypeName)\(genericTypesModifier)" -%> |
| 118 | + <%_ current.selfType = "\(type.name)\(mockTypeName)\(genericTypesModifier)" -%> |
130 | 119 | <%_ } %>
|
131 |
| - <%_ let wrappedSubscripts = allSubscripts.map(wrapSubscript) -%> |
132 |
| - <%_ let wrappedMethods = allMethods.map(wrapMethod).filter({ $0.wrappedInMethodType() }) -%> |
133 |
| - <%_ let wrappedVariables = allVariables.map(justWrap) -%> |
134 |
| - <%_ let wrappedMethodsForMethodType = allMethodsForMethodType.map(wrapMethod).filter({ $0.wrappedInMethodType() }) -%> |
135 |
| - <%_ let wrappedInitializers = allMethods.map(wrapMethod).filter({ $0.method.isInitializer }) -%> |
136 |
| - <%_ let wrappedStaticMethods = allStaticMethods.map(wrapMethod).filter({ $0.wrappedInMethodType() }) -%> |
137 |
| - <%_ let wrappedStaticVariables = allStaticVariables.map(justWrap) -%> |
138 |
| - <%_ let wrappedStaticMethodsForMethodType = allStaticMethodsForMethodType.map(wrapMethod).filter({ $0.wrappedInMethodType() }) -%> |
139 |
| - <%_ for variable in allVariables { propertyRegister(variable) } -%> |
140 |
| - <%_ for variable in allStaticVariables { propertyRegister(variable) } -%> |
| 120 | + <%_ let wrappedSubscripts = allSubscripts.map { wrapSubscript($0, current: current, subscriptRegistrar: subscriptRegistrar) } -%> |
| 121 | + <%_ let wrappedMethods = allMethods.map { wrapMethod($0, current: current, methodRegistrar: methodRegistrar) }.filter({ $0.wrappedInMethodType() }) -%> |
| 122 | + <%_ let wrappedVariables = allVariables.map { justWrap($0, current: current) } -%> |
| 123 | + <%_ let wrappedMethodsForMethodType = allMethodsForMethodType.map { wrapMethod($0, current: current, methodRegistrar: methodRegistrar) }.filter({ $0.wrappedInMethodType() }) -%> |
| 124 | + <%_ let wrappedInitializers = allMethods.map { wrapMethod($0, current: current, methodRegistrar: methodRegistrar) }.filter({ $0.method.isInitializer }) -%> |
| 125 | + <%_ let wrappedStaticMethods = allStaticMethods.map { wrapMethod($0, current: current, methodRegistrar: methodRegistrar) }.filter({ $0.wrappedInMethodType() }) -%> |
| 126 | + <%_ let wrappedStaticVariables = allStaticVariables.map { justWrap($0, current: current) } -%> |
| 127 | + <%_ let wrappedStaticMethodsForMethodType = allStaticMethodsForMethodType.map { wrapMethod($0, current: current, methodRegistrar: methodRegistrar) }.filter({ $0.wrappedInMethodType() }) -%> |
| 128 | + <%_ for variable in allVariables { propertyRegister(variable, methodRegistrar: methodRegistrar, current: current) } -%> |
| 129 | + <%_ for variable in allStaticVariables { propertyRegister(variable, methodRegistrar: methodRegistrar, current: current) } -%> |
141 | 130 | <%_ for method in wrappedMethods { method.register() } -%>
|
142 | 131 | <%_ for wrapped in wrappedSubscripts { wrapped.register() } -%>
|
143 | 132 | <%_ for method in wrappedStaticMethods { method.register() } -%><%_ -%>
|
|
176 | 165 | <%_ for method in wrappedStaticMethodsForMethodType { -%>
|
177 | 166 | <%= method.methodTypeDeclarationWithParameters() _%>
|
178 | 167 | <%_ } %> <%_ for variable in allStaticVariables { -%>
|
179 |
| - <%= propertyMethodTypes(variable) %> |
| 168 | + <%= propertyMethodTypes(variable, current: current) %> |
180 | 169 | <%_ } %> <%_ %>
|
181 | 170 | <%_ -%>
|
182 | 171 | static func compareParameters(lhs: StaticMethodType, rhs: StaticMethodType, matcher: Matcher) -> Matcher.ComparisonResult {
|
|
194 | 183 | switch self { <%_ for method in wrappedStaticMethodsForMethodType { %>
|
195 | 184 | <%= method.intValueCase -%><% } %>
|
196 | 185 | <%_ for variable in allStaticVariables { -%>
|
197 |
| - <%= propertyMethodTypesIntValue(variable) %> |
| 186 | + <%= propertyMethodTypesIntValue(variable, current: current) %> |
198 | 187 | <%_ } %> <%_ -%>
|
199 | 188 | }
|
200 | 189 | }
|
|
217 | 206 | }
|
218 | 207 |
|
219 | 208 | <%_ for variable in allStaticVariables { -%>
|
220 |
| - <%= wrapProperty(variable).givenConstructorName(prefix: "Static") -%> { |
221 |
| - <%= wrapProperty(variable).givenConstructor(prefix: "Static") _%> |
| 209 | + <%= wrapProperty(variable, current: current).givenConstructorName(prefix: "Static") -%> { |
| 210 | + <%= wrapProperty(variable, current: current).givenConstructor(prefix: "Static") _%> |
222 | 211 | }
|
223 | 212 | <%_ } %> <%_ %>
|
224 | 213 | <%_ for method in wrappedStaticMethodsForMethodType.filter({ !$0.method.returnTypeName.isVoid && !$0.method.isInitializer }) { -%>
|
|
248 | 237 | <%= method.verificationProxyConstructorName(prefix: "Static") -%> { <%= method.verificationProxyConstructor(prefix: "Static") _%> }
|
249 | 238 | <%_ } %> <%_ -%>
|
250 | 239 | <%_ for variable in allStaticVariables { -%>
|
251 |
| - <%= propertyTypes(variable) %> |
| 240 | + <%= propertyTypes(variable, current: current) %> |
252 | 241 | <%_ } %> <%_ -%>
|
253 | 242 | }
|
254 | 243 |
|
|
271 | 260 | <%_ for method in wrappedMethodsForMethodType { -%>
|
272 | 261 | <%= method.methodTypeDeclarationWithParameters() _%>
|
273 | 262 | <%_ } -%> <%_ for variable in allVariables { -%>
|
274 |
| - <%= propertyMethodTypes(variable) %> |
| 263 | + <%= propertyMethodTypes(variable, current: current) %> |
275 | 264 | <%_ } %> <%_ %> <%_ for wrapped in wrappedSubscripts { -%>
|
276 | 265 | <%= wrapped.subscriptCases() _%>
|
277 | 266 | <%_ } %> <%_ %>
|
|
293 | 282 | switch self { <%_ for method in wrappedMethodsForMethodType { %>
|
294 | 283 | <%= method.intValueCase -%><% } %>
|
295 | 284 | <%_ for variable in allVariables { -%>
|
296 |
| - <%= propertyMethodTypesIntValue(variable) %> |
| 285 | + <%= propertyMethodTypesIntValue(variable, current: current) %> |
297 | 286 | <%_ } %> <%_ for wrapped in wrappedSubscripts { -%>
|
298 | 287 | <%= wrapped.intValueCase() %>
|
299 | 288 | <%_ } -%>
|
|
327 | 316 | }
|
328 | 317 |
|
329 | 318 | <%_ for variable in allVariables { -%>
|
330 |
| - <%= wrapProperty(variable).givenConstructorName() -%> { |
331 |
| - <%= wrapProperty(variable).givenConstructor() _%> |
| 319 | + <%= wrapProperty(variable, current: current).givenConstructorName() -%> { |
| 320 | + <%= wrapProperty(variable, current: current).givenConstructor() _%> |
332 | 321 | }
|
333 | 322 | <%_ } %> <%_ %>
|
334 | 323 | <%_ for method in wrappedMethodsForMethodType.filter({ !$0.method.returnTypeName.isVoid && !$0.method.isInitializer }) { -%>
|
|
363 | 352 | <%= method.verificationProxyConstructorName() -%> { <%= method.verificationProxyConstructor() _%> }
|
364 | 353 | <%_ } %> <%_ -%>
|
365 | 354 | <%_ for variable in allVariables { -%>
|
366 |
| - <%= propertyTypes(variable) %> |
| 355 | + <%= propertyTypes(variable, current: current) %> |
367 | 356 | <%_ } %> <%_ -%>
|
368 | 357 | <%_ for wrapped in wrappedSubscripts { -%>
|
369 | 358 | <%= wrapped.verifyConstructorName() -%> { <%= wrapped.verifyConstructor() _%> }
|
|
526 | 515 | }
|
527 | 516 |
|
528 | 517 | <%_ } else { -%>
|
| 518 | +<%_ } -%> |
529 | 519 | // sourcery:end
|
| 520 | +<%_ return bufferStr -%> |
530 | 521 | <%_ } -%>
|
531 |
| -<% } -%> |
532 |
| -<%_ if mockedCount == 0 { -%> |
| 522 | + |
| 523 | +<%# ================================================== SETUP -%><%_ -%> |
| 524 | +<%_ var all = types.all |
| 525 | + all += types.protocols.map { $0 } |
| 526 | + all += types.protocolCompositions.map { $0 } |
| 527 | +-%> |
| 528 | + |
| 529 | +<%_ |
| 530 | + |
| 531 | +let content = try await withThrowingTaskGroup(of: String.self) { group in |
| 532 | + for type in all { |
| 533 | + group.addTask { |
| 534 | + let methodRegistrar = MethodRegistrar() |
| 535 | + let subscriptRegistrar = SubscriptRegistrar() |
| 536 | + return await generate(type: type, methodRegistrar: methodRegistrar, subscriptRegistrar: subscriptRegistrar) |
| 537 | + } |
| 538 | + } |
| 539 | + var fullContent = "" |
| 540 | + for try await content in group { |
| 541 | + fullContent.append(content) |
| 542 | + } |
| 543 | + return fullContent |
| 544 | +} |
| 545 | +if content.isEmpty { |
| 546 | +-%> |
533 | 547 | // SwiftyMocky: no AutoMockable found.
|
534 | 548 | // Please define and inherit from AutoMockable, or annotate protocols to be mocked
|
535 |
| -<%_ } -%> |
| 549 | +<%_ |
| 550 | +} else { |
| 551 | +-%> |
| 552 | +<%= content -%> |
| 553 | +<%_ |
| 554 | +} |
| 555 | +-%> |
0 commit comments