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

Support func foo(nonescapingClosure: () -> Void) #35

Closed
dafurman opened this issue Sep 10, 2023 · 0 comments · Fixed by #69
Closed

Support func foo(nonescapingClosure: () -> Void) #35

dafurman opened this issue Sep 10, 2023 · 0 comments · Fixed by #69
Assignees
Labels
enhancement New feature or request

Comments

@dafurman
Copy link
Contributor

Thanks for the work you've done on this library! As someone who loves Sourcery mocks, it's no surprise someone would take advantage of Swift Macros to reimplement that functionality, so thanks for being that person!

Anywhoo, this is from the main branch as of today.

functions with nonescaping closures will cause Spyable to fail to compile, whereas escaping closures compile just fine:
Screenshot 2023-09-09 at 5 27 27 PM

As a sidenote, it also seems like indentation is off at lines 9 & 22.

For reference: The code generated in Sourcery's Automockable for the same protocol is this, which compiles:

class MyProtocolMock: MyProtocol {

    //MARK: - doThing

    var doThingCompletionCallsCount = 0
    var doThingCompletionCalled: Bool {
        return doThingCompletionCallsCount > 0
    }
    var doThingCompletionReceivedCompletion: ((() -> Void))?
    var doThingCompletionReceivedInvocations: [((() -> Void))] = []
    var doThingCompletionClosure: ((@escaping () -> Void) -> Void)?

    func doThing(completion: @escaping () -> Void) {
        doThingCompletionCallsCount += 1
        doThingCompletionReceivedCompletion = completion
        doThingCompletionReceivedInvocations.append(completion)
        doThingCompletionClosure?(completion)
    }

    //MARK: - doThing

    var doThingNonescapingCompletionCallsCount = 0
    var doThingNonescapingCompletionCalled: Bool {
        return doThingNonescapingCompletionCallsCount > 0
    }
    var doThingNonescapingCompletionClosure: ((() -> Void) -> Void)?

    func doThing(nonescapingCompletion: () -> Void) {
        doThingNonescapingCompletionCallsCount += 1
        doThingNonescapingCompletionClosure?(nonescapingCompletion)
    }
}

It looks like this could be resolved by making spyable match automockable's behavior by having these two lines be removed for nonescaping closures:

doThingNonEscapingCompletionReceivedNonEscapingCompletion = (nonEscapingCompletion)
doThingNonEscapingCompletionReceivedInvocations.append((nonEscapingCompletion))

Automockable.stencil has lots of checks that go something like if x and not hasNonEscapingClosures, so maybe something similar could be replicated here?

@Matejkob Matejkob added the enhancement New feature or request label Nov 9, 2023
@Matejkob Matejkob linked a pull request Nov 30, 2023 that will close this issue
Matejkob added a commit that referenced this issue Jan 1, 2024
Fix #35 - Support nonescaping closure parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants