Created
December 8, 2021 23:09
-
-
Save Nikoloutsos/e2c6a9271da7b2f4604687a96feced1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// swiftlint:disable line_length | |
// swiftlint:disable variable_name | |
// ❤️ https://github.com/krzysztofzablocki/Sourcery/blob/master/Templates/Templates/AutoMockable.stencil | |
{% for import in argument.autoMockableImports %} | |
import {{ import }} | |
{% endfor %} | |
{% for import in argument.autoMockableTestableImports %} | |
@testable import {{ import }} | |
{% endfor %} | |
{% macro swiftifyMethodName name %}{{ name | replace:"(","_" | replace:")","" | replace:":","_" | replace:"`","" | snakeToCamelCase | lowerFirstWord }}{% endmacro %} | |
{% macro methodThrowableErrorDeclaration method %} | |
var {% call swiftifyMethodName method.selectorName %}ThrowableError: Error? | |
{% endmacro %} | |
{% macro methodThrowableErrorUsage method %} | |
if let error = {% call swiftifyMethodName method.selectorName %}ThrowableError { | |
throw error | |
} | |
{% endmacro %} | |
{% macro methodReceivedParameters method %} | |
{%if method.parameters.count == 1 %} | |
{% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name|upperFirstLetter }} = {{ param.name }}{% endfor %} | |
{% call swiftifyMethodName method.selectorName %}ReceivedInvocations.append({% for param in method.parameters %}{{ param.name }}){% endfor %} | |
{% else %} | |
{% if not method.parameters.count == 0 %} | |
{% call swiftifyMethodName method.selectorName %}ReceivedArguments = ({% for param in method.parameters %}{{ param.name }}: {{ param.name }}{% if not forloop.last%}, {% endif %}{% endfor %}) | |
{% call swiftifyMethodName method.selectorName %}ReceivedInvocations.append(({% for param in method.parameters %}{{ param.name }}: {{ param.name }}{% if not forloop.last%}, {% endif %}{% endfor %})) | |
{% endif %} | |
{% endif %} | |
{% endmacro %} | |
{% macro methodClosureName method %}{% call swiftifyMethodName method.selectorName %}Closure{% endmacro %} | |
{% macro closureReturnTypeName method %}{% if method.isOptionalReturnType %}{{ method.unwrappedReturnTypeName }}?{% else %}{{ method.returnTypeName }}{% endif %}{% endmacro %} | |
{% macro methodClosureDeclaration method %} | |
var {% call methodClosureName method %}: (({% for param in method.parameters %}{{ param.typeName }}{% if not forloop.last %}, {% endif %}{% endfor %}) {% if method.throws %}throws {% endif %}-> {% if method.isInitializer %}Void{% else %}{% call closureReturnTypeName method %}{% endif %})? | |
{% endmacro %} | |
{% macro methodClosureCallParameters method %}{% for param in method.parameters %}{{ param.name }}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %} | |
{% macro mockMethod method %} | |
//MARK: - {{ method.shortName }} | |
{% if method.throws %} | |
{% call methodThrowableErrorDeclaration method %} | |
{% endif %} | |
{% if not method.isInitializer %} | |
var {% call swiftifyMethodName method.selectorName %}CallsCount = 0 | |
var {% call swiftifyMethodName method.selectorName %}Called: Bool { | |
return {% call swiftifyMethodName method.selectorName %}CallsCount > 0 | |
} | |
{% endif %} | |
{% if method.parameters.count == 1 %} | |
var {% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name|upperFirstLetter }}: {{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}?{% endfor %} | |
var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations{% for param in method.parameters %}: [{{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}{%if param.typeName.isOptional%}?{%endif%}]{% endfor %} = [] | |
{% elif not method.parameters.count == 0 %} | |
var {% call swiftifyMethodName method.selectorName %}ReceivedArguments: ({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})? | |
var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations: [({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})] = [] | |
{% endif %} | |
{% if not method.returnTypeName.isVoid and not method.isInitializer %} | |
var {% call swiftifyMethodName method.selectorName %}ReturnValue: {{ '(' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ method.returnTypeName }}{{ ')' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ '!' if not method.isOptionalReturnType }} | |
{% endif %} | |
{% call methodClosureDeclaration method %} | |
{% if method.isInitializer %} | |
required {{ method.name }} { | |
{% call methodReceivedParameters method %} | |
{% call methodClosureName method %}?({% call methodClosureCallParameters method %}) | |
} | |
{% else %} | |
func {{ method.name }}{{ ' throws' if method.throws }}{% if not method.returnTypeName.isVoid %} -> {{ method.returnTypeName }}{% endif %} { | |
{% if method.throws %} | |
{% call methodThrowableErrorUsage method %} | |
{% endif %} | |
{% call swiftifyMethodName method.selectorName %}CallsCount += 1 | |
{% call methodReceivedParameters method %} | |
{% if method.returnTypeName.isVoid %} | |
{% if method.throws %}try {% endif %}{% call methodClosureName method %}?({% call methodClosureCallParameters method %}) | |
{% else %} | |
return {{ 'try ' if method.throws }}{% call methodClosureName method %}.map({ {{ 'try ' if method.throws }}$0({% call methodClosureCallParameters method %}) }) ?? {% call swiftifyMethodName method.selectorName %}ReturnValue | |
{% endif %} | |
} | |
{% endif %} | |
{% endmacro %} | |
{% macro mockOptionalVariable variable %} | |
var {% call mockedVariableName variable %}: {{ variable.typeName }} | |
{% endmacro %} | |
{% macro mockNonOptionalArrayOrDictionaryVariable variable %} | |
var {% call mockedVariableName variable %}: {{ variable.typeName }} = {% if variable.isArray %}[]{% elif variable.isDictionary %}[:]{% endif %} | |
{% endmacro %} | |
{% macro mockNonOptionalVariable variable %} | |
var {% call mockedVariableName variable %}: {{ variable.typeName }} { | |
get { return {% call underlyingMockedVariableName variable %} } | |
set(value) { {% call underlyingMockedVariableName variable %} = value } | |
} | |
var {% call underlyingMockedVariableName variable %}: {{ variable.typeName }}! | |
{% endmacro %} | |
{% macro underlyingMockedVariableName variable %}underlying{{ variable.name|upperFirstLetter }}{% endmacro %} | |
{% macro mockedVariableName variable %}{{ variable.name }}{% endmacro %} | |
{% for type in types.protocols where type.based.AutoMockable or type|annotated:"AutoMockable" %}{% if type.name != "AutoMockable" %} | |
class {{ type.name }}Mock: {{ type.name }} { | |
{% for variable in type.allVariables|!definedInExtension %} | |
{% if variable.isOptional %}{% call mockOptionalVariable variable %}{% elif variable.isArray or variable.isDictionary %}{% call mockNonOptionalArrayOrDictionaryVariable variable %}{% else %}{% call mockNonOptionalVariable variable %}{% endif %} | |
{% endfor %} | |
{% for method in type.allMethods|!definedInExtension %} | |
{% call mockMethod method %} | |
{% endfor %} | |
} | |
{% endif %}{% endfor %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment