MakeAWishFoundation / SwiftyMocky

Framework for automatic mock generation. Adds a set of handy methods, simplifying testing. One of the best and most complete solutions, including generics support and much more.
MIT License
1.03k stars 104 forks source link

Incorrect mock generation with @MainActor in closure of async method #316

Open muratdi opened 2 years ago

muratdi commented 2 years ago

Hi!

We use async functions for network operations and then pass the result with closure blocks up to our ViewControllers. It’s very convenient to declare @MainActor directly in closure to switch to the main queue:

func signUp(login: String, completion: @MainActor @escaping (Result<Void, Error>) -> Void) { 

}

Anyway, in our Mock.generated we got this:

fileprivate enum MethodType {
        case m_signUp__login_logincompletion_completion(Parameter<String>, Parameter<(Result<Void, Error>) -> Void>)
}

open func signUp(login: String, completion: @MainActor @escaping (Result<Void, Error>) -> Void) {

        addInvocation(.m_signUp__login_logincompletion_completion(Parameter<String>.value(`login`), 
                      Parameter<(Result<Void, Error>) -> Void>.value(`completion`)))

    let perform = methodPerformValue(.m_signUp__login_logincompletion_completion(Parameter<String>.value(`login`), 
                                     Parameter<(Result<Void, Error>) -> Void>.value(`completion`))) as? (String, @MainActor @escaping (Result<Void, Error>) -> Void) -> Void

    perform?(`login`, `completion`)
}

With 2 compiler errors when adding .value(completion):

`Converting function value of type '@MainActor (Result<Void, Error>) -> Void' to '(Result<Void, Error>) -> Void' loses global actor 'MainActor'`

I’ve been looking through issues (i.e. #290, #304) but mine looks new.

It seems to be an easy fix in some way. We either need to add @MainActor to MethodType or just ignore it while parsing (looks like it’s not that necessary to mock it at all).

mltbnz commented 2 years ago

seems like that was fixed in the latest release 4.2.0

spaluchiewicz commented 2 years ago

@muratdi could you please check this according to the comment from @mltbnz?