Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug][AutoMockable] Compilation errors for methods having closure parameters with more than a single parameter. #1356

Open
rokridi opened this issue Aug 8, 2024 · 0 comments

Comments

@rokridi
Copy link
Contributor

rokridi commented Aug 8, 2024

Hello 👋
The current AutoMockable.stencil does not properly handle methods methods having closure parameters with more than a single parameter.

Example:

// sourcery: AutoMockable
protocol MyProtocol {
    func foo(closureA: @escaping (String, Int) -> Void)
}

The generated mock looks like this:

class MyProtocolMock: MyProtocol {




    //MARK: - foo

    var fooClosureAEscapingStringIntVoidVoidCallsCount = 0
    var fooClosureAEscapingStringIntVoidVoidCalled: Bool {
        return fooClosureAEscapingStringIntVoidVoidCallsCount > 0
    }
    var fooClosureAEscapingStringIntVoidVoidReceivedClosureA: (((String, Int) -> Void))?
    var fooClosureAEscapingStringIntVoidVoidReceivedInvocations: [(((String, Int) -> Void))] = []
    var fooClosureAEscapingStringIntVoidVoidClosure: ((@escaping (String, Int) -> Void) -> Void)?

    func foo(closureA: (@escaping (String, Int) -> Void) // <-- there is a missing closing parenthese {
        fooClosureAEscapingStringIntVoidVoidCallsCount += 1
        fooClosureAEscapingStringIntVoidVoidReceivedClosureA = closureA
        fooClosureAEscapingStringIntVoidVoidReceivedInvocations.append(closureA)
        fooClosureAEscapingStringIntVoidVoidClosure?(closureA)
    }


}

The solution would be to replace this line with this:

{% 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 %}){% endif %}{% if not forloop.last %}, {% endif %}{% endfor -%}){% endmacro %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant