Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to mock a protocol with methods that differ in parameter type - Error: "Invalid redeclaration" #1238

Closed
r3econ opened this issue Dec 14, 2023 · 2 comments · Fixed by #1240
Labels
Milestone

Comments

@r3econ
Copy link

r3econ commented Dec 14, 2023

I am unable to mock a protocol with methods that differ in parameter type. For example:

// sourcery: AutoMockable
public protocol ProtocolA {
    func doSomething(_ data: Int) -> [String]
    func doSomething(_ data: String) -> [String]
}

The generated mock is invalid and can't be compiled. The compiler shows errors like Invalid redeclaration of 'doSomethingCallsCount'. The mock:

// Generated using Sourcery 2.1.2 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
import Foundation
import Core

// swiftlint:disable all
open class AMock: ProtocolA {

    public init() {}

    // MARK: - doSomething

    public var doSomethingCallsCount = 0
    public var doSomethingCalled: Bool {
        return doSomethingCallsCount > 0
    }
    public var doSomethingReceivedData: Int?
    public var doSomethingReceivedInvocations: [Int] = []
    public var doSomethingReturnValue: [String]!
    public var doSomethingClosure: ((Int) -> [String])?

    public func doSomething(_ data: Int) -> [String] {
        doSomethingCallsCount += 1
        doSomethingReceivedData = data
        doSomethingReceivedInvocations.append(data)
        if let doSomethingClosure {
            return doSomethingClosure(data)
        } else {
            return doSomethingReturnValue
        }
    }

    // MARK: - doSomething

    public var doSomethingCallsCount = 0
    public var doSomethingCalled: Bool {
        return doSomethingCallsCount > 0
    }
    public var doSomethingReceivedData: String?
    public var doSomethingReceivedInvocations: [String] = []
    public var doSomethingReturnValue: [String]!
    public var doSomethingClosure: ((String) -> [String])?

    public func doSomething(_ data: String) -> [String] {
        doSomethingCallsCount += 1
        doSomethingReceivedData = data
        doSomethingReceivedInvocations.append(data)
        if let doSomethingClosure {
            return doSomethingClosure(data)
        } else {
            return doSomethingReturnValue
        }
    }

}
// swiftlint:enable all

Is it possible to configure the mock generation to make it work?
I'm using Sourcery 2.1.2

@art-divin
Copy link
Collaborator

👋🏻 Hey @r3econ ,

thank you for your bug report. Indeed, current implementation does not support method overloading it seems.

Possible solution would be to start including type information into the generated tracking variable names to make them uniquely-named, i.e.

-public var doSomethingReceivedData: Int?
+public var doSomethingIntReceivedData: Int?
...
-public var doSomethingCallsCount = 0
+public var doSomethingStringCallsCount = 0

@r3econ
Copy link
Author

r3econ commented Dec 20, 2023

Thanks a lot! 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants