mockolo icon indicating copy to clipboard operation
mockolo copied to clipboard

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

Open omarzl opened this issue 2 months ago • 3 comments

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())
        }
    }
}

omarzl avatar Apr 26 '24 20:04 omarzl