From dca83054ac36724f6639b16f0e340ec4dc32772a Mon Sep 17 00:00:00 2001 From: MontakOleg Date: Thu, 24 Oct 2024 12:52:05 +0300 Subject: [PATCH] Fix AutoMockable for closure with multiple parameters (#1373) Resolves https://github.com/krzysztofzablocki/Sourcery/issues/1356 --- Templates/Templates/AutoMockable.stencil | 4 +-- Templates/Tests/Context/AutoMockable.swift | 4 +++ .../Tests/Context_Linux/AutoMockable.swift | 4 +++ .../Tests/Expected/AutoMockable.expected | 27 ++++++++++++++++++- .../Generated/AutoMockable.generated.swift | 24 +++++++++++++++++ 5 files changed, 59 insertions(+), 4 deletions(-) diff --git a/Templates/Templates/AutoMockable.stencil b/Templates/Templates/AutoMockable.stencil index df9c94641..b0d112d5a 100755 --- a/Templates/Templates/AutoMockable.stencil +++ b/Templates/Templates/AutoMockable.stencil @@ -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 -%} diff --git a/Templates/Tests/Context/AutoMockable.swift b/Templates/Tests/Context/AutoMockable.swift index 5dafa93e4..1852e8843 100644 --- a/Templates/Tests/Context/AutoMockable.swift +++ b/Templates/Tests/Context/AutoMockable.swift @@ -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) diff --git a/Templates/Tests/Context_Linux/AutoMockable.swift b/Templates/Tests/Context_Linux/AutoMockable.swift index 7feea6ecb..a70876d02 100644 --- a/Templates/Tests/Context_Linux/AutoMockable.swift +++ b/Templates/Tests/Context_Linux/AutoMockable.swift @@ -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) diff --git a/Templates/Tests/Expected/AutoMockable.expected b/Templates/Tests/Expected/AutoMockable.expected index 081746ed2..0303f891a 100644 --- a/Templates/Tests/Expected/AutoMockable.expected +++ b/Templates/Tests/Expected/AutoMockable.expected @@ -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 @@ -32,6 +32,7 @@ import AppKit + public class AccessLevelProtocolMock: AccessLevelProtocol { @@ -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 { diff --git a/Templates/Tests/Generated/AutoMockable.generated.swift b/Templates/Tests/Generated/AutoMockable.generated.swift index 31d4511d0..0303f891a 100644 --- a/Templates/Tests/Generated/AutoMockable.generated.swift +++ b/Templates/Tests/Generated/AutoMockable.generated.swift @@ -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 {