Brightify / Cuckoo

Boilerplate-free mocking framework for Swift!
MIT License
1.67k stars 175 forks source link

Strip conflicting imports #462

Closed ppamorim closed 1 year ago

ppamorim commented 1 year ago

Cuckoo is generating for the given protocol:

import RealmSwift // Note this!

class User: Realm.Object {
  //...
}

protocol UserRepository {
  func findUser() -> User
}

class UserRepositoryImpl: UserRepository {
  func findUser() -> User {
    //...
  }
}

This mock:

import Cuckoo
@testable import MyProject

import Foundation
import RealmSwift // <--- Issue

class MockUserRepository: UserRepository, Cuckoo.ProtocolMock {
  //...
}

This is causing Swift not be able to know from where User is coming. Returning the error 'User' is ambiguous for type lookup in this context. This is happening because RealmSwift.User is a thing (typealias User = RLMUser).

To temporarily measure to sort this I am manually removing import RealmSwift as this is not required for the mock, but it's not healthy to have Cuckoo generator disabled.

Offtopic, when the latest version on master with the fix for the _ parameters will be released? I had to manually compile the source code and get cuckoo_generator from the master branch source code to make it work.

MatyasKriz commented 1 year ago

Hey @ppamorim, is it possible to create a typealias in your project so you can omit the imports?

Something like

import RealmSwift

typealias RealmObject = Realm.Object

in its own file, then it shouldn't be necessary to import RealmSwift and Cuckoo will not add it to the generated file.

It could of course be worked around by adding a CLI option, but I'd like to keep that to a minimum before we switch to TOML configuration.

ppamorim commented 1 year ago

@MatyasKriz I will test that, thanks.

Edit: This works, luckly I had the database isolated in my application and it was easy to fix.

Should I reopen the issue to keep track of the feature request?