Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // swiftlint:disable line_length
- // swiftlint:disable variable_name
- // swiftlint:disable large_tuple
- import Foundation
- #if os(iOS) || os(tvOS) || os(watchOS)
- import UIKit
- #elseif os(OSX)
- import AppKit
- #endif
- import RxSwift
- {% 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 %}
- {% 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 %})
- {% 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 %}
- {% elif not method.parameters.count == 0 %}
- var {% call swiftifyMethodName method.selectorName %}ReceivedArguments: ({% for param in method.parameters %}{{ param.name }}: {% if param.typeAttributes.escaping %}{{ param.unwrappedTypeName }}{% else %}{{ param.typeName }}{% endif %}{% if not forloop.last %}, {% endif %}{% endfor %})?
- {% endif %}
- {% if not method.returnTypeName.isVoid and not method.isInitializer %}
- var {% call swiftifyMethodName method.selectorName %}ReturnValue: {% if method.isOptionalReturnType %}{{ method.returnTypeName }}{% else %}{{ method.returnTypeName }}!{% endif %}
- {% 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 %}
Add Comment
Please, Sign In to add comment