OCProtocolMockObject.m 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2005-2015 Erik Doernenburg and contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. * not use these files except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. */
  16. #import <objc/runtime.h>
  17. #import "NSMethodSignature+OCMAdditions.h"
  18. #import "OCProtocolMockObject.h"
  19. @implementation OCProtocolMockObject
  20. #pragma mark Initialisers, description, accessors, etc.
  21. - (id)initWithProtocol:(Protocol *)aProtocol
  22. {
  23. NSParameterAssert(aProtocol != nil);
  24. [super init];
  25. mockedProtocol = aProtocol;
  26. return self;
  27. }
  28. - (NSString *)description
  29. {
  30. const char* name = protocol_getName(mockedProtocol);
  31. return [NSString stringWithFormat:@"OCMockObject(%s)", name];
  32. }
  33. #pragma mark Proxy API
  34. - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
  35. {
  36. struct { BOOL isRequired; BOOL isInstance; } opts[4] = { {YES, YES}, {NO, YES}, {YES, NO}, {NO, NO} };
  37. for(int i = 0; i < 4; i++)
  38. {
  39. struct objc_method_description methodDescription = protocol_getMethodDescription(mockedProtocol, aSelector, opts[i].isRequired, opts[i].isInstance);
  40. if(methodDescription.name != NULL)
  41. return [NSMethodSignature signatureWithObjCTypes:methodDescription.types];
  42. }
  43. return nil;
  44. }
  45. - (BOOL)conformsToProtocol:(Protocol *)aProtocol
  46. {
  47. return protocol_conformsToProtocol(mockedProtocol, aProtocol);
  48. }
  49. - (BOOL)respondsToSelector:(SEL)selector
  50. {
  51. return ([self methodSignatureForSelector:selector] != nil);
  52. }
  53. @end