Skip to content

Commit

Permalink
Fix AutoMockable for closure with multiple parameters (#1373)
Browse files Browse the repository at this point in the history
Resolves #1356
  • Loading branch information
MontakOleg authored Oct 24, 2024
1 parent 8acc3bf commit dca8305
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
4 changes: 1 addition & 3 deletions Templates/Templates/AutoMockable.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,11 @@ import {{ import }}
{{ typeName | replace:"some","(some" | replace:"?",")?" }}
{%- elif isVariadic -%}
{{ typeName }}...
{%- elif typeName.isClosure and typeName.closure.parameters.count > 0 and typeName.closure.parameters.last.isVariadic -%}
{{ typeName }})
{%- else -%}
{{ typeName }}
{%- endif -%}
{%- endmacro %}
{% macro methodName method %}func {{ method.shortName}}({%- for param in method.parameters %}{% if param.argumentLabel == nil %}_ {% if not param.name == "" %}{{ param.name }}{% else %}arg{{ param.index }}{% endif %}{%elif param.argumentLabel == param.name%}{{ param.name }}{%else%}{{ param.argumentLabel }} {{ param.name }}{% endif %}: {% if param.typeName.isClosure and param.typeName.closure.parameters.count > 1 %}({% endif %}{% call existentialParameterTypeName param.typeName param.isVariadic %}{% if not forloop.last %}, {% endif %}{% endfor -%}){% endmacro %}
{% macro methodName method %}func {{ method.shortName}}({%- for param in method.parameters %}{% if param.argumentLabel == nil %}_ {% if not param.name == "" %}{{ param.name }}{% else %}arg{{ param.index }}{% endif %}{%elif param.argumentLabel == param.name%}{{ param.name }}{%else%}{{ param.argumentLabel }} {{ param.name }}{% endif %}: {% if param.typeName.isClosure and param.typeName.closure.parameters.count > 1 %}({% endif %}{% call existentialParameterTypeName param.typeName param.isVariadic %}{% if param.typeName.isClosure and param.typeName.closure.parameters.count > 1 and not (param.typeName|contains:"any " and param.typeName.closure.returnTypeName.name|contains:"any " and param.typeName|contains:"?") %}){% endif %}{% if not forloop.last %}, {% endif %}{% endfor -%}){% endmacro %}

{% macro extractProtocolCompositionFromAssociatedTypes type -%}
{%- if type.associatedTypes|sortedValuesByKeys|count > 0 -%}
Expand Down
4 changes: 4 additions & 0 deletions Templates/Tests/Context/AutoMockable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ protocol MultiExistentialArgumentsClosureProtocol: AutoMockable {
func execute(completion: ((any StubWithSomeNameProtocol)?, any StubWithSomeNameProtocol) -> (any StubWithSomeNameProtocol)?)
}

protocol ClosureWithTwoParametersProtocol: AutoMockable {
func setClosure(closure: @escaping (String, Int) -> Void)
}

/// sourcery: AutoMockable
protocol AnnotatedProtocol {
func sayHelloWith(name: String)
Expand Down
4 changes: 4 additions & 0 deletions Templates/Tests/Context_Linux/AutoMockable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ protocol MultiExistentialArgumentsClosureProtocol: AutoMockable {
func execute(completion: ((any StubWithSomeNameProtocol)?, any StubWithSomeNameProtocol) -> (any StubWithSomeNameProtocol)?)
}

protocol ClosureWithTwoParametersProtocol: AutoMockable {
func setClosure(closure: @escaping (String, Int) -> Void)
}

/// sourcery: AutoMockable
protocol AnnotatedProtocol {
func sayHelloWith(name: String)
Expand Down
27 changes: 26 additions & 1 deletion Templates/Tests/Expected/AutoMockable.expected
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated using Sourcery 2.2.4 — https://github.com/krzysztofzablocki/Sourcery
// Generated using Sourcery 2.2.5 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
// swiftlint:disable line_length
// swiftlint:disable variable_name
Expand Down Expand Up @@ -32,6 +32,7 @@ import AppKit






public class AccessLevelProtocolMock: AccessLevelProtocol {
Expand Down Expand Up @@ -672,6 +673,30 @@ class ClosureProtocolMock: ClosureProtocol {
}


}
class ClosureWithTwoParametersProtocolMock: ClosureWithTwoParametersProtocol {




//MARK: - setClosure

var setClosureClosureEscapingStringIntVoidVoidCallsCount = 0
var setClosureClosureEscapingStringIntVoidVoidCalled: Bool {
return setClosureClosureEscapingStringIntVoidVoidCallsCount > 0
}
var setClosureClosureEscapingStringIntVoidVoidReceivedClosure: (((String, Int) -> Void))?
var setClosureClosureEscapingStringIntVoidVoidReceivedInvocations: [(((String, Int) -> Void))] = []
var setClosureClosureEscapingStringIntVoidVoidClosure: ((@escaping (String, Int) -> Void) -> Void)?

func setClosure(closure: (@escaping (String, Int) -> Void)) {
setClosureClosureEscapingStringIntVoidVoidCallsCount += 1
setClosureClosureEscapingStringIntVoidVoidReceivedClosure = closure
setClosureClosureEscapingStringIntVoidVoidReceivedInvocations.append(closure)
setClosureClosureEscapingStringIntVoidVoidClosure?(closure)
}


}
class CurrencyPresenterMock: CurrencyPresenter {

Expand Down
24 changes: 24 additions & 0 deletions Templates/Tests/Generated/AutoMockable.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,30 @@ class ClosureProtocolMock: ClosureProtocol {
}


}
class ClosureWithTwoParametersProtocolMock: ClosureWithTwoParametersProtocol {




//MARK: - setClosure

var setClosureClosureEscapingStringIntVoidVoidCallsCount = 0
var setClosureClosureEscapingStringIntVoidVoidCalled: Bool {
return setClosureClosureEscapingStringIntVoidVoidCallsCount > 0
}
var setClosureClosureEscapingStringIntVoidVoidReceivedClosure: (((String, Int) -> Void))?
var setClosureClosureEscapingStringIntVoidVoidReceivedInvocations: [(((String, Int) -> Void))] = []
var setClosureClosureEscapingStringIntVoidVoidClosure: ((@escaping (String, Int) -> Void) -> Void)?

func setClosure(closure: (@escaping (String, Int) -> Void)) {
setClosureClosureEscapingStringIntVoidVoidCallsCount += 1
setClosureClosureEscapingStringIntVoidVoidReceivedClosure = closure
setClosureClosureEscapingStringIntVoidVoidReceivedInvocations.append(closure)
setClosureClosureEscapingStringIntVoidVoidClosure?(closure)
}


}
class CurrencyPresenterMock: CurrencyPresenter {

Expand Down

0 comments on commit dca8305

Please sign in to comment.