uber / mockolo

Efficient Mock Generator for Swift
Apache License 2.0
804 stars 85 forks source link

Compilation fix for a mock where the return type of a closure is an opaque type #259

Closed omarzl closed 2 months ago

omarzl commented 2 months ago

The following code:

protocol ProtocolReturningOpaqueTypeInClosureProtocol {
    func register(router: @autoclosure @escaping () -> (some Error))
}

produces this mock:

class ProtocolReturningOpaqueTypeInClosureProtocolMock: ProtocolReturningOpaqueTypeInClosureProtocol {
    init() { }
    private(set) var registerCallCount = 0
    var registerHandler: ((@autoclosure @escaping () -> (some Error)) -> ())?
    func register(router: @autoclosure @escaping () -> (some Error))  {
        registerCallCount += 1
        if let registerHandler = registerHandler {
            registerHandler(router())
        }
    }
}

causing the compilation error: 'some' cannot appear in parameter position in result type '((@autoclosure @escaping () -> (some Error)) -> ())?'

This pr fixes the variable registerHandler by generating the following mock:

class ProtocolReturningOpaqueTypeInClosureProtocolMock: ProtocolReturningOpaqueTypeInClosureProtocol {
    init() { }
    private(set) var registerCallCount = 0
    var registerHandler: ((@autoclosure @escaping () -> (any Error)) -> ())?
    func register(router: @autoclosure @escaping () -> (some Error))  {
        registerCallCount += 1
        if let registerHandler = registerHandler {
            registerHandler(router())
        }
    }
}
CLAassistant commented 2 months ago

CLA assistant check
All committers have signed the CLA.

omarzl commented 2 months ago

I wonder if this implementation would work well with the following code?🤔

Hello, I am only replacing the word some to any when the return type is some MyType

These other examples work well:

protocol Foo {
    func register(router: () -> (Bar))
    func register<T>(router: () -> (T))
}

protocol Bar {}

The generated mocks are:

var registerRouterHandler: ((() -> (Bar)) -> ())?
var registerRouterTHandler: ((() -> (Any)) -> ())?
omarzl commented 2 months ago

oh right, sorry I misunderstood you I updated the fix and the tests to consider this scenario, thanks!

sidepelican commented 2 months ago

Sorry I forgot launch workflow.