uber / mockolo

Efficient Mock Generator for Swift
Apache License 2.0
813 stars 86 forks source link

[Bug] Protocol that inherits from protocols in two different modules with same function name does not produce unique handlers #244

Open ryanaveo opened 1 year ago

ryanaveo commented 1 year ago

Environment

macOS 13.4.1 Swift version 5.8.1 Xcode 14.3.1 v2.0.1

Issue

The following code produces a mock that does not compile successfully

Protocols to Mock

// ModuleA

/// @CreateMock
public protocol Foo {
    func foo(num: Int)
}
// ModuleB

/// @CreateMock
public protocol Bar {
    func foo(text: String)
}
// Module C
import ModuleA
import ModuleB

/// @CreateMock
public protocol FooBar: Foo, Bar {}

Generated Mock

public class FooBarMock: FooBar {
    public init() { }

    public var fooCallCount = 0
    public var fooHandler: ((Int) -> ())?
    public func foo(num: Int)  {
        mockFunc(&fooCallCount)("foo", fooHandler?(num), .void)
    }
    public func foo(text: String)  {
        mockFunc(&fooCallCount)("foo", fooHandler?(text), .void) // error found here because fooHandler expects an Int, not a string
    }
}

Error

error: cannot convert value of type 'String' to expected argument type 'Int'
        mockFunc(&fooCallCount)("foo", fooHandler?(text), .void)
toddlee commented 7 months ago

any update on this? simplest solution would be to add some increasing index to ones after the first one with the same name.

Ricardo1980 commented 6 months ago

I have exactly the same issue. A potential solution could be adding a prefix or suffix to the generated mock using something like: /// @mockable(method: postfix = Foo)