New: CPObject instancesImplementSelector:(SEL)aSelector

Tests if instances of given class implement a selector.

With test.

Note: The naming is from cocoa where this method is semi-public.
This commit is contained in:
cacaodev
2016-11-12 18:30:02 +01:00
parent 947d552509
commit 5da8728d6c
2 changed files with 24 additions and 5 deletions
+15 -5
View File
@@ -275,13 +275,13 @@ CPLog(@"Got some class: %@", inst);
}
/*!
Tests if the receiver implements the provided selector regardless of inheritance.
@param aSelector the selector for which to test the receiver
@return \c YES if the receiver implements the selector
Tests if instances of a given class implement the provided selector regardless of inheritance.
@param aSelector the selector for which to test the class
@return \c YES if instances of the class implement the selector
*/
- (BOOL)implementsSelector:(SEL)aSelector
+ (BOOL)instancesImplementSelector:(SEL)aSelector
{
var methods = class_copyMethodList([self class]),
var methods = class_copyMethodList(self),
count = methods.length;
while (count--)
@@ -291,6 +291,16 @@ CPLog(@"Got some class: %@", inst);
return NO;
}
/*!
Tests if the receiver implements the provided selector regardless of inheritance.
@param aSelector the selector for which to test the receiver
@return \c YES if the receiver implements the selector
*/
- (BOOL)implementsSelector:(SEL)aSelector
{
return [[self class] instancesImplementSelector:aSelector];
}
/*!
Test whether instances of this class conforms to the provided protocol.
@param aProtocol the protocol for which to test the class
+9
View File
@@ -25,6 +25,15 @@
[self assertFalse:[receiver implementsSelector:@selector(notImplementedInSuperNorReceiver)]];
}
- (void)testInstancesImplementSelector
{
[self assertTrue:[Receiver instancesImplementSelector:@selector(implementedInReceiverAndSuper)]];
[self assertTrue:[Receiver instancesImplementSelector:@selector(implementedInReceiverOnly)]];
[self assertFalse:[Receiver instancesImplementSelector:@selector(implementedInSuperOnly)]];
[self assertFalse:[Receiver instancesImplementSelector:@selector(notImplementedInSuperNorReceiver)]];
}
- (void)testVersion
{
// zero by default