Skip to content

Commit

Permalink
[#1251] Swifty generated variable names + fixed generated mocks compi…
Browse files Browse the repository at this point in the history
…lation issues due to method generic parameters (#1252)
  • Loading branch information
rokridi authored Jan 9, 2024
1 parent b62a8fc commit 71c2d46
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Templates/Templates/AutoMockable.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ import {{ import }}
@testable import {{ import }}
{% endfor %}

{% macro cleanString string %}{{ string | replace:"(","_" | replace:")","" | replace:":","_" | replace:"`","" | replace:" ","_" | replace:"?","_" | replace:"!","_" | replace:",","_" | replace:"->","_" | replace:"@","_" | replace:".","_" | replace:"[","" | replace:"]","" | snakeToCamelCase }}{% endmacro %}
{% macro swiftifyMethodName method %}{% call cleanString method.name | lowerFirstWord %}{% call cleanString method.actualReturnTypeName.name | upperFirstLetter %}{% endmacro %}
{% macro cleanString string %}{{ string | replace:"(","_" | replace:")","" | replace:":","_" | replace:"`","" | replace:" ","_" | replace:"?","_" | replace:"!","_" | replace:",","_" | replace:"->","_" | replace:"@","_" | replace:".","_" | replace:"[","" | replace:"]","" | replace:"<","" | replace:">","" | snakeToCamelCase }}{% endmacro %}
{%- macro swiftifyMethodName method -%}
{%- set cleanMethodName %}{% call cleanString method.name %}{% endset %}
{%- set cleanMethodReturnTypeName %}{% call cleanString method.actualReturnTypeName.name %}{% endset -%}
{{ cleanMethodName | lowerFirstLetter }}{{ cleanMethodReturnTypeName | upperFirstLetter }}
{%- endmacro -%}

{% macro accessLevel level %}{% if level != 'internal' %}{{ level }} {% endif %}{% endmacro %}

Expand Down
5 changes: 5 additions & 0 deletions Templates/Tests/Context/AutoMockable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,8 @@ protocol SubscriptProtocol {
subscript<T: Hashable>(arg: T) -> T? { get set }
subscript<T>(arg: String) -> T? where T: Cancellable { get throws }
}

// sourcery: AutoMockable
public protocol ProtocolWithMethodWithGenericParameters {
func execute(param: Result<Int, Error>) -> Result<String, Error>
}
29 changes: 29 additions & 0 deletions Templates/Tests/Expected/AutoMockable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -1526,4 +1526,33 @@ class VariablesProtocolMock: VariablesProtocol {
var universityMarks: [String: Int] = [:]


}
class ProtocolWithMethodWithGenericParameters: SomeProtocol {




//MARK: - execute

var executeParamResultIntErrorResultStringErrorCallsCount = 0
var executeParamResultIntErrorResultStringErrorCalled: Bool {
return executeParamResultIntErrorResultStringErrorCallsCount > 0
}
var executeParamResultIntErrorResultStringErrorReceivedParam: (Result<Int, Error>)?
var executeParamResultIntErrorResultStringErrorReceivedInvocations: [(Result<Int, Error>)] = []
var executeParamResultIntErrorResultStringErrorReturnValue: Result<String, Error>!
var executeParamResultIntErrorResultStringErrorClosure: ((Result<Int, Error>) -> Result<String, Error>)?

func execute(param: Result<Int, Error>) -> Result<String, Error> {
executeParamResultIntErrorResultStringErrorCallsCount += 1
executeParamResultIntErrorResultStringErrorReceivedParam = param
executeParamResultIntErrorResultStringErrorReceivedInvocations.append(param)
if let executeParamResultIntErrorResultStringErrorClosure = executeParamResultIntErrorResultStringErrorClosure {
return executeParamResultIntErrorResultStringErrorClosure(param)
} else {
return executeParamResultIntErrorResultStringErrorReturnValue
}
}


}

0 comments on commit 71c2d46

Please sign in to comment.