From 5da8728d6ce1907af47a95dd5eccde248ae02b8d Mon Sep 17 00:00:00 2001 From: cacaodev Date: Sat, 12 Nov 2016 18:26:53 +0100 Subject: [PATCH] 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. --- Foundation/CPObject.j | 20 +++++++++++++++----- Tests/Foundation/CPObjectTest.j | 9 +++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) 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