Guest User

Untitled

a guest
Jan 22nd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. // swiftlint:disable line_length
  2. // swiftlint:disable variable_name
  3. // swiftlint:disable large_tuple
  4.  
  5. import Foundation
  6. #if os(iOS) || os(tvOS) || os(watchOS)
  7. import UIKit
  8. #elseif os(OSX)
  9. import AppKit
  10. #endif
  11.  
  12. import RxSwift
  13.  
  14. {% macro swiftifyMethodName name %}{{ name | replace:"(","_" | replace:")","" | replace:":","_" | replace:"`","" | snakeToCamelCase | lowerFirstWord }}{% endmacro %}
  15.  
  16. {% macro methodThrowableErrorDeclaration method %}
  17. var {% call swiftifyMethodName method.selectorName %}ThrowableError: Error?
  18. {% endmacro %}
  19.  
  20. {% macro methodThrowableErrorUsage method %}
  21. if let error = {% call swiftifyMethodName method.selectorName %}ThrowableError {
  22. throw error
  23. }
  24. {% endmacro %}
  25.  
  26. {% macro methodReceivedParameters method %}
  27. {%if method.parameters.count == 1 %}
  28. {% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name|upperFirstLetter }} = {{ param.name }}{% endfor %}
  29. {% else %}
  30. {% if not method.parameters.count == 0 %}
  31. {% call swiftifyMethodName method.selectorName %}ReceivedArguments = ({% for param in method.parameters %}{{ param.name }}: {{ param.name }}{% if not forloop.last %}, {% endif %}{% endfor %})
  32. {% endif %}
  33. {% endif %}
  34. {% endmacro %}
  35.  
  36. {% macro methodClosureName method %}{% call swiftifyMethodName method.selectorName %}Closure{% endmacro %}
  37.  
  38. {% macro closureReturnTypeName method %}{% if method.isOptionalReturnType %}{{ method.unwrappedReturnTypeName }}?{% else %}{{ method.returnTypeName }}{% endif %}{% endmacro %}
  39.  
  40. {% macro methodClosureDeclaration method %}
  41. 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 %})?
  42. {% endmacro %}
  43.  
  44. {% macro methodClosureCallParameters method %}{% for param in method.parameters %}{{ param.name }}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %}
  45.  
  46. {% macro mockMethod method %}
  47. //MARK: - {{ method.shortName }}
  48.  
  49. {% if method.throws %}
  50. {% call methodThrowableErrorDeclaration method %}
  51. {% endif %}
  52. {% if not method.isInitializer %}
  53. var {% call swiftifyMethodName method.selectorName %}CallsCount = 0
  54. var {% call swiftifyMethodName method.selectorName %}Called: Bool {
  55. return {% call swiftifyMethodName method.selectorName %}CallsCount > 0
  56. }
  57. {% endif %}
  58. {% if method.parameters.count == 1 %}
  59. var {% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name|upperFirstLetter }}: {{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}?{% endfor %}
  60. {% elif not method.parameters.count == 0 %}
  61. 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 %})?
  62. {% endif %}
  63. {% if not method.returnTypeName.isVoid and not method.isInitializer %}
  64. var {% call swiftifyMethodName method.selectorName %}ReturnValue: {% if method.isOptionalReturnType %}{{ method.returnTypeName }}{% else %}{{ method.returnTypeName }}!{% endif %}
  65. {% endif %}
  66. {% call methodClosureDeclaration method %}
  67.  
  68. {% if method.isInitializer %}
  69. required {{ method.name }} {
  70. {% call methodReceivedParameters method %}
  71. {% call methodClosureName method %}?({% call methodClosureCallParameters method %})
  72. }
  73. {% else %}
  74. func {{ method.name }}{{ ' throws' if method.throws }}{% if not method.returnTypeName.isVoid %} -> {{ method.returnTypeName }}{% endif %} {
  75. {% if method.throws %}
  76. {% call methodThrowableErrorUsage method %}
  77. {% endif %}
  78. {% call swiftifyMethodName method.selectorName %}CallsCount += 1
  79. {% call methodReceivedParameters method %}
  80. {% if method.returnTypeName.isVoid %}
  81. {% if method.throws %}try {% endif %}{% call methodClosureName method %}?({% call methodClosureCallParameters method %})
  82. {% else %}
  83. return {{ 'try ' if method.throws }}{% call methodClosureName method %}.map({ {{ 'try ' if method.throws }}$0({% call methodClosureCallParameters method %}) }) ?? {% call swiftifyMethodName method.selectorName %}ReturnValue
  84. {% endif %}
  85. }
  86.  
  87. {% endif %}
  88. {% endmacro %}
  89.  
  90. {% macro mockOptionalVariable variable %}
  91. var {% call mockedVariableName variable %}: {{ variable.typeName }}
  92. {% endmacro %}
  93.  
  94. {% macro mockNonOptionalArrayOrDictionaryVariable variable %}
  95. var {% call mockedVariableName variable %}: {{ variable.typeName }} = {% if variable.isArray %}[]{% elif variable.isDictionary %}[:]{% endif %}
  96. {% endmacro %}
  97.  
  98. {% macro mockNonOptionalVariable variable %}
  99. var {% call mockedVariableName variable %}: {{ variable.typeName }} {
  100. get { return {% call underlyingMockedVariableName variable %} }
  101. set(value) { {% call underlyingMockedVariableName variable %} = value }
  102. }
  103. var {% call underlyingMockedVariableName variable %}: {{ variable.typeName }}!
  104. {% endmacro %}
  105.  
  106. {% macro underlyingMockedVariableName variable %}underlying{{ variable.name|upperFirstLetter }}{% endmacro %}
  107. {% macro mockedVariableName variable %}{{ variable.name }}{% endmacro %}
  108.  
  109. {% for type in types.protocols where type.based.AutoMockable or type|annotated:"AutoMockable" %}{% if type.name != "AutoMockable" %}
  110. class {{ type.name }}Mock: {{ type.name }} {
  111. {% for variable in type.allVariables|!definedInExtension %}
  112. {% if variable.isOptional %}{% call mockOptionalVariable variable %}{% elif variable.isArray or variable.isDictionary %}{% call mockNonOptionalArrayOrDictionaryVariable variable %}{% else %}{% call mockNonOptionalVariable variable %}{% endif %}
  113. {% endfor %}
  114.  
  115. {% for method in type.allMethods|!definedInExtension %}
  116. {% call mockMethod method %}
  117. {% endfor %}
  118. }
  119. {% endif %}{% endfor %}
Add Comment
Please, Sign In to add comment