diff --git a/Foundation/CPObject.j b/Foundation/CPObject.j index 311e67998..b0cfd2e26 100644 --- a/Foundation/CPObject.j +++ b/Foundation/CPObject.j @@ -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 diff --git a/Tests/Foundation/CPObjectTest.j b/Tests/Foundation/CPObjectTest.j index f333ab2c5..1d792db49 100644 --- a/Tests/Foundation/CPObjectTest.j +++ b/Tests/Foundation/CPObjectTest.j @@ -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