uber / mockolo

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

[bug] Duplicated underlying var declarations generated for protocol mocks with init requirements #224

Closed pablocornejo closed 1 year ago

pablocornejo commented 1 year ago

Hi, I've found a bug where when mocking a protocol that has multiple init requirements that contain a parameter with the same name, the generated underlying var declaration gets duplicated.

For example, generating a mock for:

protocol MyProtocol {
    init(param: Any)
    init(param: Any, anotherParam: String)
}

Results in:

class MyProtocolMock: MyProtocol {
        private var _param: Any!
    private var _anotherParam: String!
    private var _param: Any!
    public init() { }
    required init(param: Any) {
        self._param = param
    }
    required init(param: Any, anotherParam: String = "") {
        self._param = param
        self._anotherParam = anotherParam
    }

}

...which fails to compile: error: invalid redeclaration of '_param'

A similar thing happens if using different types for param:

protocol MyProtocol {
    init(param: Int)
    init(param: Any)
}

Results in:

class MyProtocolMock: MyProtocol {
        private var _param: Any!
    private var _param: Int!
    public init() { }
    required init(param: Any) {
        self._param = param
    }
    required init(param: Int = 0) {
        self._param = param
    }

}

...error: invalid redeclaration of '_param'

fummicc1 commented 1 year ago

@pablocornejo Thank you for sharing! If you have a solution, feel free to send a PR.😄 I will start to investigate this issue.

fummicc1 commented 1 year ago

Hello, I have fixed this issue in #231 , so let me close this issue. please tell me if there is something wrong. :)