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

Protocol with primary associated type cannot be mocked #491

Closed
aaronbarsky opened this issue May 10, 2024 · 4 comments
Closed

Protocol with primary associated type cannot be mocked #491

aaronbarsky opened this issue May 10, 2024 · 4 comments

Comments

@aaronbarsky
Copy link

Using 2.0.1 I am trying to mock the following file.

import Combine
import Foundation
protocol SubscriptionServiceType<Output> {
    associatedtype Output: Equatable
    func connect() -> AnyPublisher<Output, Error>
}

1.10 produces the valid mock

class MockSubscriptionServiceType<Output: Equatable>: SubscriptionServiceType, Cuckoo.ProtocolMock {
     typealias MocksType = DefaultImplCaller<Output>
     typealias Stubbing = __StubbingProxy_SubscriptionServiceType
     typealias Verification = __VerificationProxy_SubscriptionServiceType

     let cuckoo_manager = Cuckoo.MockManager.preconfiguredManager ?? Cuckoo.MockManager(hasParent: false)
    
     class DefaultImplCaller<Output: Equatable>: SubscriptionServiceType {
        private let reference: Any

2.0.1 produces invalid code

class MockSubscriptionServiceType<Output, Output: Equatable>: SubscriptionServiceType, Cuckoo.ProtocolMock {
    typealias MocksType = DefaultImplCaller<Output, Output>
    typealias Stubbing = __StubbingProxy_SubscriptionServiceType
    typealias Verification = __VerificationProxy_SubscriptionServiceType

    let cuckoo_manager = Cuckoo.MockManager.preconfiguredManager ?? Cuckoo.MockManager(hasParent: false)

    class DefaultImplCaller<Output, Output: Equatable>: SubscriptionServiceType {
        private let reference: Any

Note the 1.10.0 code has a Swift 6 warning

Generic parameter 'Output' shadows generic parameter from outer scope with the same name; this is an error in Swift 6

but for now it is functional.

The 2.0.1 code with Output, Output:Equatable does not compile

@MatyasKriz
Copy link
Collaborator

Thanks for reporting this and providing the source as well as generated code for both versions. The fix itself shouldn't be difficult, but I'll see what I can do about the Swift 6 warning as well.

@MatyasKriz
Copy link
Collaborator

I've added your test sample to the unit tests, so this error shouldn't occur again. 🙂 Please try out 2.0.2 to see if it's fixed your issue completely.

Furthermore this issue made me prod the DefaultImplCaller altogether and find out it actually (probably, according to the unit tests) doesn't require the generic parameters as it's already got them available through the class it resides in.

@aaronbarsky
Copy link
Author

Brilliant. Compiles without warnings and my unit tests pass. Thanks for the quick turnaround.

@MatyasKriz
Copy link
Collaborator

Glad to hear that. 🙂 Closing issue then.

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

No branches or pull requests

2 participants