Cuckoo icon indicating copy to clipboard operation
Cuckoo copied to clipboard

Mock protocols inherited from NSObjectProtocol

Open paiv opened this issue 5 years ago • 3 comments

I am trying to mock a protocol inheriting from NSObjectProtocol. This seems like a common scenario, but I don't see it mentioned.

  1. The fist issue is, I need to supply source for NSObjectProtocol to Cuckoo generate. I am using this protocol generated by Xcode:
@objc public protocol NSObjectProtocol {
    func isEqual(_ object: Any?) -> Bool
    var hash: Int { get }
    var superclass: AnyClass? { get }
    func `self`() -> Self
    func perform(_ aSelector: Selector!) -> Unmanaged<AnyObject>!
    func perform(_ aSelector: Selector!, with object: Any!) -> Unmanaged<AnyObject>!
    func perform(_ aSelector: Selector!, with object1: Any!, with object2: Any!) -> Unmanaged<AnyObject>!
    func isProxy() -> Bool
    func isKind(of aClass: AnyClass) -> Bool
    func isMember(of aClass: AnyClass) -> Bool
    func conforms(to aProtocol: Protocol) -> Bool
    func responds(to aSelector: Selector!) -> Bool
    var description: String { get }
    @objc optional var debugDescription: String { get }
}
  1. Next, generated mock MockNSObjectProtocol have compilation issues:
public func self() -> Self {

// Keyword 'self' cannot be used as an identifier here

(see #328)

    return cuckoo_manager.call("self() -> Self",
            parameters: (),
            escapingParameters: (),
            superclassCall:
                
                Cuckoo.MockManager.crashOnProtocolSuperclassCall()
                ,
            defaultCall: __defaultImplStub!.`self`())
        
    }

// Cannot convert return expression of type 'NSObjectProtocol' to return type 'Self'

(seems like #304)

    public var debugDescription: String {
        get {
            return cuckoo_manager.getter("debugDescription",
                superclassCall:
                    
                    Cuckoo.MockManager.crashOnProtocolSuperclassCall()
                    ,
                defaultCall: __defaultImplStub!.debugDescription)
        }
        
    }

// Value of optional type 'String?' must be unwrapped to a value of type 'String'

(copied into #330)

I am stuck at this stage. I assume this is the way it supposed to work, bar generator issues.

paiv avatar Jan 24 '20 14:01 paiv

Oh yes, when we were improving attribute parsing, this was one of the areas that were left untouched. We are currently tending to more pressing matters using the time we have, but if you're interested in adding support for @objc protocols, I'll happily introduce you to the project and answer any of your questions.

As far as I remember, SourceKitten actually gives us the @objc and optional (as per #330) for the attributes, we aren't just handling them correctly at the moment.

MatyasKriz avatar Feb 29 '20 17:02 MatyasKriz