Skip to content

Commit

Permalink
: fix stencil to consider nullable closures as escaping (#1358)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdmotoc authored Sep 10, 2024
1 parent 3da93a7 commit 8acc3bf
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Templates/Templates/AutoMockable.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down Expand Up @@ -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 %}
Expand Down
8 changes: 8 additions & 0 deletions Templates/Tests/Context/AutoMockable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
52 changes: 52 additions & 0 deletions Templates/Tests/Expected/AutoMockable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {

Expand All @@ -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 {

Expand Down
55 changes: 54 additions & 1 deletion Templates/Tests/Generated/AutoMockable.generated.swift
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 @@ -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)
}

Expand Down Expand Up @@ -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 {

Expand All @@ -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 {

Expand Down

0 comments on commit 8acc3bf

Please sign in to comment.