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

[Bugfix][AutoMockable.stencil]- inout with multiple parameters & optional any parameter in completion handler #1290

Conversation

paul1893
Copy link
Contributor

@paul1893 paul1893 commented Mar 5, 2024

Context

I noticed 2 bugs in mock generation. If a user have these kind of APIs:

public protocol ProtocolWithMethodWithInoutParameter {
    func execute(param: inout String, bar: Int) // 👈 `inout` parameter in a function that have more that one parameter.
}

protocol FunctionWithNullableCompletionThatHasNullableAnyParameterProtocol {
    func add(
        _ request: Int,
        withCompletionHandler completionHandler: (((any Error)?) -> Void)? // 👈 a completion with an optional that is annotated with `any` keyword.
    )
}

then it will produce:

public var executeParamInoutStringBarIntVoidReceivedArguments: (param: inout String, bar: Int)? // ❌ 'inout' may only be used on parameters
public var executeParamInoutStringBarIntVoidReceivedInvocations: [(param: inout String, bar: Int)] = [] // ❌ 'inout' may only be used on parameters

...

var addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidClosure: ((Int, (((any Error)?) -> Void))?) -> Void)? // ❌ Does not compile because parenthesis syntax is wrong 

func add(_ request: Int, withCompletionHandler completionHandler: (((any Error)?) -> Void))?) { // ❌ Does not compile because parenthesis syntax is wrong
     addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidCallsCount += 1
     addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidClosure?(request, completionHandler)
}

In this PR

I've updated the AutoMockable.stencil to support these 2 identified cases.

now it will produce:

public var executeParamInoutStringBarIntVoidReceivedArguments: (param: String, bar: Int)? // ✅
public var executeParamInoutStringBarIntVoidReceivedInvocations: [(param: String, bar: Int)] = [] // ✅

...
var addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidClosure: ((Int, ((((any Error)?) -> Void))?) -> Void)? // ✅

func add(_ request: Int, withCompletionHandler completionHandler: ((((any Error)?) -> Void))?) { // ✅
    addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidCallsCount += 1
    addRequestIntWithCompletionHandlerCompletionHandlerAnyErrorVoidVoidClosure?(request, completionHandler)
}

…d any as optional parameter in completion handler
@paul1893 paul1893 force-pushed the bugfix/optional-any-completion-and-multiple-inout-parameter branch from 239a182 to 0f14392 Compare March 5, 2024 10:49
@art-divin art-divin added this to the 2.1.8 milestone Mar 5, 2024
@art-divin art-divin self-requested a review March 5, 2024 11:18
Copy link
Collaborator

@art-divin art-divin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, great find @paul1893 🤝

@art-divin art-divin merged commit 3a44361 into krzysztofzablocki:master Mar 5, 2024
2 checks passed
art-divin pushed a commit that referenced this pull request Mar 17, 2024
…d any as optional parameter in completion handler (#1290)

Co-authored-by: Paul Bancarel <paul.bancarel.ext@adevinta.com>
art-divin pushed a commit that referenced this pull request Mar 17, 2024
…d any as optional parameter in completion handler (#1290)

Co-authored-by: Paul Bancarel <paul.bancarel.ext@adevinta.com>
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

Successfully merging this pull request may close these issues.

3 participants