uber / mockolo

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

[Bugfix] Fix functions with same signature and different generic constraints not getting generated #241

Closed ryanaveo closed 1 year ago

ryanaveo commented 1 year ago

A mock file will fail to compile if the protocol being mocked contains two functions with the same signature and name but has different generic constraints. Consider the following example:

@CreateMock
protocol Foo {
   func connect<T>(adapter: T) where T: Adapter
   func connect<T>(adapter: T) where T: KeyedAdapter
}

The above code will generate the following mock:

public var connectCallCount = 0    
public var connectHandler: ((Any) -> ())?    
public func connect<T>(adapter: T)  where T: Adapter {
   mockFunc(&connectCallCount)("connect", connectHandler?(adapter), .void)    
}

This fails to compile because it's missing the implementation with KeyedAdapter. With this PR, the following mock will now be produced

public var connectCallCount = 0
public var connectHandler: ((Any) -> ())?
public func connect<T>(adapter: T) where T: EntityAdapter {
    mockFunc(&connectCallCount)("connect", connectHandler?(adapter), .void)
}

public var connectAdapterCallCount =0
public var connectAdapterHandler: ((Any) -> ())?
public func connect<T>(adapter: T) where T: KeyedEntityAdapter {
    mockFunc(&connectAdapterCallCount)("connect", connectAdapterHandler?(adapter), .void)
}