From 8acc3bf2ba1828e5f1126c1acc23a9e79561652d Mon Sep 17 00:00:00 2001 From: Alex Motoc Date: Tue, 10 Sep 2024 15:12:06 +0300 Subject: [PATCH] : fix stencil to consider nullable closures as escaping (#1358) --- Templates/Templates/AutoMockable.stencil | 4 +- Templates/Tests/Context/AutoMockable.swift | 8 +++ .../Tests/Expected/AutoMockable.expected | 52 ++++++++++++++++++ .../Generated/AutoMockable.generated.swift | 55 ++++++++++++++++++- 4 files changed, 116 insertions(+), 3 deletions(-) diff --git a/Templates/Templates/AutoMockable.stencil b/Templates/Templates/AutoMockable.stencil index e4d0dbaf0..df9c94641 100755 --- a/Templates/Templates/AutoMockable.stencil +++ b/Templates/Templates/AutoMockable.stencil @@ -39,7 +39,7 @@ import {{ import }} {% macro methodReceivedParameters method %} {% set hasNonEscapingClosures %} - {%- for param in method.parameters where param.isClosure and not param.typeAttributes.escaping %} + {%- for param in method.parameters where param.isClosure and not param.typeAttributes.escaping and not param.isOptional %} {{ true }} {% endfor -%} {% endset %} @@ -77,7 +77,7 @@ import {{ import }} } {% endif %} {% set hasNonEscapingClosures %} - {%- for param in method.parameters where param.isClosure and not param.typeAttributes.escaping %} + {%- for param in method.parameters where param.isClosure and not param.typeAttributes.escaping and not param.isOptional %} {{ true }} {% endfor -%} {% endset %} diff --git a/Templates/Tests/Context/AutoMockable.swift b/Templates/Tests/Context/AutoMockable.swift index 7feea6ecb..5dafa93e4 100644 --- a/Templates/Tests/Context/AutoMockable.swift +++ b/Templates/Tests/Context/AutoMockable.swift @@ -75,10 +75,18 @@ protocol ClosureProtocol: AutoMockable { func setClosure(_ closure: @escaping () -> Void) } +protocol NullableClosureProtocol: AutoMockable { + func setClosure(_ closure: (() -> Void)?) +} + protocol MultiClosureProtocol: AutoMockable { func setClosure(name: String, _ closure: @escaping () -> Void) } +protocol MultiNullableClosureProtocol: AutoMockable { + func setClosure(name: String, _ closure: (() -> Void)?) +} + protocol NonEscapingClosureProtocol: AutoMockable { func executeClosure(_ closure: () -> Void) } diff --git a/Templates/Tests/Expected/AutoMockable.expected b/Templates/Tests/Expected/AutoMockable.expected index 8af6cab2a..081746ed2 100644 --- a/Templates/Tests/Expected/AutoMockable.expected +++ b/Templates/Tests/Expected/AutoMockable.expected @@ -964,10 +964,14 @@ class FunctionWithNullableCompletionThatHasNullableAnyParameterProtocolMock: Fun var addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidCalled: Bool { return addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidCallsCount > 0 } + var addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidReceivedArguments: (request: Int, completionHandler: ((((any Error)?) -> Void))?)? + var addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidReceivedInvocations: [(request: Int, completionHandler: ((((any Error)?) -> Void))?)] = [] var addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidClosure: ((Int, ((((any Error)?) -> Void))?) -> Void)? func add(_ request: Int, withCompletionHandler completionHandler: ((((any Error)?) -> Void))?) { addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidCallsCount += 1 + addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidReceivedArguments = (request: request, completionHandler: completionHandler) + addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidReceivedInvocations.append((request: request, completionHandler: completionHandler)) addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidClosure?(request, completionHandler) } @@ -1124,6 +1128,30 @@ class MultiNonEscapingClosureProtocolMock: MultiNonEscapingClosureProtocol { } +} +class MultiNullableClosureProtocolMock: MultiNullableClosureProtocol { + + + + + //MARK: - setClosure + + var setClosureNameStringClosureVoidVoidCallsCount = 0 + var setClosureNameStringClosureVoidVoidCalled: Bool { + return setClosureNameStringClosureVoidVoidCallsCount > 0 + } + var setClosureNameStringClosureVoidVoidReceivedArguments: (name: String, closure: (() -> Void)?)? + var setClosureNameStringClosureVoidVoidReceivedInvocations: [(name: String, closure: (() -> Void)?)] = [] + var setClosureNameStringClosureVoidVoidClosure: ((String, (() -> Void)?) -> Void)? + + func setClosure(name: String, _ closure: (() -> Void)?) { + setClosureNameStringClosureVoidVoidCallsCount += 1 + setClosureNameStringClosureVoidVoidReceivedArguments = (name: name, closure: closure) + setClosureNameStringClosureVoidVoidReceivedInvocations.append((name: name, closure: closure)) + setClosureNameStringClosureVoidVoidClosure?(name, closure) + } + + } class NonEscapingClosureProtocolMock: NonEscapingClosureProtocol { @@ -1144,6 +1172,30 @@ class NonEscapingClosureProtocolMock: NonEscapingClosureProtocol { } +} +class NullableClosureProtocolMock: NullableClosureProtocol { + + + + + //MARK: - setClosure + + var setClosureClosureVoidVoidCallsCount = 0 + var setClosureClosureVoidVoidCalled: Bool { + return setClosureClosureVoidVoidCallsCount > 0 + } + var setClosureClosureVoidVoidReceivedClosure: (((() -> Void)))? + var setClosureClosureVoidVoidReceivedInvocations: [(((() -> Void)))?] = [] + var setClosureClosureVoidVoidClosure: (((() -> Void)?) -> Void)? + + func setClosure(_ closure: (() -> Void)?) { + setClosureClosureVoidVoidCallsCount += 1 + setClosureClosureVoidVoidReceivedClosure = closure + setClosureClosureVoidVoidReceivedInvocations.append(closure) + setClosureClosureVoidVoidClosure?(closure) + } + + } public class ProtocolWithMethodWithGenericParametersMock: ProtocolWithMethodWithGenericParameters { diff --git a/Templates/Tests/Generated/AutoMockable.generated.swift b/Templates/Tests/Generated/AutoMockable.generated.swift index 8af6cab2a..31d4511d0 100644 --- a/Templates/Tests/Generated/AutoMockable.generated.swift +++ b/Templates/Tests/Generated/AutoMockable.generated.swift @@ -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 { @@ -964,10 +965,14 @@ class FunctionWithNullableCompletionThatHasNullableAnyParameterProtocolMock: Fun var addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidCalled: Bool { return addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidCallsCount > 0 } + var addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidReceivedArguments: (request: Int, completionHandler: ((((any Error)?) -> Void))?)? + var addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidReceivedInvocations: [(request: Int, completionHandler: ((((any Error)?) -> Void))?)] = [] var addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidClosure: ((Int, ((((any Error)?) -> Void))?) -> Void)? func add(_ request: Int, withCompletionHandler completionHandler: ((((any Error)?) -> Void))?) { addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidCallsCount += 1 + addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidReceivedArguments = (request: request, completionHandler: completionHandler) + addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidReceivedInvocations.append((request: request, completionHandler: completionHandler)) addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidClosure?(request, completionHandler) } @@ -1124,6 +1129,30 @@ class MultiNonEscapingClosureProtocolMock: MultiNonEscapingClosureProtocol { } +} +class MultiNullableClosureProtocolMock: MultiNullableClosureProtocol { + + + + + //MARK: - setClosure + + var setClosureNameStringClosureVoidVoidCallsCount = 0 + var setClosureNameStringClosureVoidVoidCalled: Bool { + return setClosureNameStringClosureVoidVoidCallsCount > 0 + } + var setClosureNameStringClosureVoidVoidReceivedArguments: (name: String, closure: (() -> Void)?)? + var setClosureNameStringClosureVoidVoidReceivedInvocations: [(name: String, closure: (() -> Void)?)] = [] + var setClosureNameStringClosureVoidVoidClosure: ((String, (() -> Void)?) -> Void)? + + func setClosure(name: String, _ closure: (() -> Void)?) { + setClosureNameStringClosureVoidVoidCallsCount += 1 + setClosureNameStringClosureVoidVoidReceivedArguments = (name: name, closure: closure) + setClosureNameStringClosureVoidVoidReceivedInvocations.append((name: name, closure: closure)) + setClosureNameStringClosureVoidVoidClosure?(name, closure) + } + + } class NonEscapingClosureProtocolMock: NonEscapingClosureProtocol { @@ -1144,6 +1173,30 @@ class NonEscapingClosureProtocolMock: NonEscapingClosureProtocol { } +} +class NullableClosureProtocolMock: NullableClosureProtocol { + + + + + //MARK: - setClosure + + var setClosureClosureVoidVoidCallsCount = 0 + var setClosureClosureVoidVoidCalled: Bool { + return setClosureClosureVoidVoidCallsCount > 0 + } + var setClosureClosureVoidVoidReceivedClosure: (((() -> Void)))? + var setClosureClosureVoidVoidReceivedInvocations: [(((() -> Void)))?] = [] + var setClosureClosureVoidVoidClosure: (((() -> Void)?) -> Void)? + + func setClosure(_ closure: (() -> Void)?) { + setClosureClosureVoidVoidCallsCount += 1 + setClosureClosureVoidVoidReceivedClosure = closure + setClosureClosureVoidVoidReceivedInvocations.append(closure) + setClosureClosureVoidVoidClosure?(closure) + } + + } public class ProtocolWithMethodWithGenericParametersMock: ProtocolWithMethodWithGenericParameters {