Mock protocols inherited from NSObjectProtocol
I am trying to mock a protocol inheriting from NSObjectProtocol. This seems like a common scenario, but I don't see it mentioned.
- The fist issue is, I need to supply source for
NSObjectProtocoltoCuckoo 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 }
}
- Next, generated mock
MockNSObjectProtocolhave 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.
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.