1 //--------------------------------------------------------------------------------------- 2 // $Id$ 3 // Copyright (c) 2005-2008 by Mulle Kybernetik. See License file for details. 4 //--------------------------------------------------------------------------------------- 5 6 #import <objc/runtime.h> 7 #import "NSMethodSignature+OCMAdditions.h" 8 #import "OCProtocolMockObject.h" 9 10 @implementation OCProtocolMockObject 11 12 #pragma mark Initialisers, description, accessors, etc. 13 14 - (id)initWithProtocol:(Protocol *)aProtocol 15 { 16 [super init]; 17 mockedProtocol = aProtocol; 18 return self; 19 } 20 21 - (NSString *)description 22 { 23 const char* name = protocol_getName(mockedProtocol); 24 return [NSString stringWithFormat:@"OCMockObject[%s]", name]; 25 } 26 27 #pragma mark Proxy API 28 29 - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector 30 { 31 struct objc_method_description methodDescription = protocol_getMethodDescription(mockedProtocol, aSelector, YES, YES); 32 if(methodDescription.name == NULL) 33 { 34 methodDescription = protocol_getMethodDescription(mockedProtocol, aSelector, NO, YES); 35 } 36 if(methodDescription.name == NULL) 37 { 38 return nil; 39 } 40 return [NSMethodSignature signatureWithObjCTypes:methodDescription.types]; 41 } 42 43 - (BOOL)conformsToProtocol:(Protocol *)aProtocol 44 { 45 return protocol_conformsToProtocol(mockedProtocol, aProtocol); 46 } 47 48 - (BOOL)respondsToSelector:(SEL)selector 49 { 50 return ([self methodSignatureForSelector:selector] != nil); 51 } 52 53 @end 54