PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/NetworkAndOthers/KiwiTime/Pods/Kiwi/Classes/NSObject+KiwiStubAdditions.m

https://bitbucket.org/kumaranvram/ios-bootcamp
Objective C | 245 lines | 193 code | 47 blank | 5 comment | 20 complexity | f73c9fc0994c8484ce2d8d980627e6f9 MD5 | raw file
  1. //
  2. // Licensed under the terms in License.txt
  3. //
  4. // Copyright 2010 Allen Ding. All rights reserved.
  5. //
  6. #import "NSObject+KiwiStubAdditions.h"
  7. #import "KWIntercept.h"
  8. #import "KWInvocationCapturer.h"
  9. #import "KWMessagePattern.h"
  10. #import "KWObjCUtilities.h"
  11. #import "KWStringUtilities.h"
  12. #import "KWStub.h"
  13. static NSString * const StubValueKey = @"StubValueKey";
  14. static NSString * const StubSecondValueKey = @"StubSecondValueKey";
  15. static NSString * const ChangeStubValueAfterTimesKey = @"ChangeStubValueAfterTimesKey";
  16. @implementation NSObject(KiwiStubAdditions)
  17. #pragma mark - Capturing Invocations
  18. - (NSMethodSignature *)invocationCapturer:(KWInvocationCapturer *)anInvocationCapturer methodSignatureForSelector:(SEL)aSelector {
  19. NSMethodSignature *signature = [self methodSignatureForSelector:aSelector];
  20. if (signature != nil)
  21. return signature;
  22. NSString *encoding = KWEncodingForVoidMethod();
  23. return [NSMethodSignature signatureWithObjCTypes:[encoding UTF8String]];
  24. }
  25. - (void)invocationCapturer:(KWInvocationCapturer *)anInvocationCapturer didCaptureInvocation:(NSInvocation *)anInvocation {
  26. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternFromInvocation:anInvocation];
  27. id value = (anInvocationCapturer.userInfo)[StubValueKey];
  28. if (!(anInvocationCapturer.userInfo)[StubSecondValueKey]) {
  29. [self stubMessagePattern:messagePattern andReturn:value];
  30. } else {
  31. id times = (anInvocationCapturer.userInfo)[ChangeStubValueAfterTimesKey];
  32. id secondValue = (anInvocationCapturer.userInfo)[StubSecondValueKey];
  33. [self stubMessagePattern:messagePattern andReturn:value times:times afterThatReturn:secondValue];
  34. }
  35. }
  36. #pragma mark - Stubbing Methods
  37. - (void)stub:(SEL)aSelector {
  38. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
  39. [self stubMessagePattern:messagePattern andReturn:nil];
  40. }
  41. - (void)stub:(SEL)aSelector withBlock:(id (^)(NSArray *))block {
  42. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
  43. [self stubMessagePattern:messagePattern withBlock:block];
  44. }
  45. - (void)stub:(SEL)aSelector withArguments:(id)firstArgument, ... {
  46. va_list argumentList;
  47. va_start(argumentList, firstArgument);
  48. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector firstArgumentFilter:firstArgument argumentList:argumentList];
  49. [self stubMessagePattern:messagePattern andReturn:nil];
  50. }
  51. - (void)stub:(SEL)aSelector andReturn:(id)aValue {
  52. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
  53. [self stubMessagePattern:messagePattern andReturn:aValue];
  54. }
  55. - (void)stub:(SEL)aSelector andReturn:(id)aValue withArguments:(id)firstArgument, ... {
  56. va_list argumentList;
  57. va_start(argumentList, firstArgument);
  58. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector firstArgumentFilter:firstArgument argumentList:argumentList];
  59. [self stubMessagePattern:messagePattern andReturn:aValue];
  60. }
  61. + (void)stub:(SEL)aSelector {
  62. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
  63. [self stubMessagePattern:messagePattern andReturn:nil];
  64. }
  65. + (void)stub:(SEL)aSelector withBlock:(id (^)(NSArray *))block {
  66. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
  67. [self stubMessagePattern:messagePattern withBlock:block];
  68. }
  69. + (void)stub:(SEL)aSelector withArguments:(id)firstArgument, ... {
  70. va_list argumentList;
  71. va_start(argumentList, firstArgument);
  72. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector firstArgumentFilter:firstArgument argumentList:argumentList];
  73. [self stubMessagePattern:messagePattern andReturn:nil];
  74. }
  75. + (void)stub:(SEL)aSelector andReturn:(id)aValue {
  76. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
  77. [self stubMessagePattern:messagePattern andReturn:aValue];
  78. }
  79. + (void)stub:(SEL)aSelector andReturn:(id)aValue withArguments:(id)firstArgument, ... {
  80. va_list argumentList;
  81. va_start(argumentList, firstArgument);
  82. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector firstArgumentFilter:firstArgument argumentList:argumentList];
  83. [self stubMessagePattern:messagePattern andReturn:aValue];
  84. }
  85. - (id)stub {
  86. return [KWInvocationCapturer invocationCapturerWithDelegate:self];
  87. }
  88. - (id)stubAndReturn:(id)aValue {
  89. NSDictionary *userInfo = @{StubValueKey: aValue};
  90. return [KWInvocationCapturer invocationCapturerWithDelegate:self userInfo:userInfo];
  91. }
  92. - (id)stubAndReturn:(id)aValue times:(id)times afterThatReturn:(id)aSecondValue {
  93. NSDictionary *userInfo = @{StubValueKey: aValue, ChangeStubValueAfterTimesKey: times, StubSecondValueKey: aSecondValue};
  94. return [KWInvocationCapturer invocationCapturerWithDelegate:self userInfo:userInfo];
  95. }
  96. - (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern andReturn:(id)aValue {
  97. [self stubMessagePattern:aMessagePattern andReturn:aValue overrideExisting:YES];
  98. }
  99. - (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern andReturn:(id)aValue overrideExisting:(BOOL)overrideExisting {
  100. if ([self methodSignatureForSelector:aMessagePattern.selector] == nil) {
  101. [NSException raise:@"KWStubException" format:@"cannot stub -%@ because no such method exists",
  102. NSStringFromSelector(aMessagePattern.selector)];
  103. }
  104. Class interceptClass = KWSetupObjectInterceptSupport(self);
  105. KWSetupMethodInterceptSupport(interceptClass, aMessagePattern.selector);
  106. KWStub *stub = [KWStub stubWithMessagePattern:aMessagePattern value:aValue];
  107. KWAssociateObjectStub(self, stub, overrideExisting);
  108. }
  109. - (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern andReturn:(id)aValue times:(id)times afterThatReturn:(id)aSecondValue {
  110. if ([self methodSignatureForSelector:aMessagePattern.selector] == nil) {
  111. [NSException raise:@"KWStubException" format:@"cannot stub -%@ because no such method exists",
  112. NSStringFromSelector(aMessagePattern.selector)];
  113. }
  114. Class interceptClass = KWSetupObjectInterceptSupport(self);
  115. KWSetupMethodInterceptSupport(interceptClass, aMessagePattern.selector);
  116. KWStub *stub = [KWStub stubWithMessagePattern:aMessagePattern value:aValue times:times afterThatReturn:aSecondValue];
  117. KWAssociateObjectStub(self, stub, YES);
  118. }
  119. - (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern withBlock:(id (^)(NSArray *params))block {
  120. if ([self methodSignatureForSelector:aMessagePattern.selector] == nil) {
  121. [NSException raise:@"KWStubException" format:@"cannot stub -%@ because no such method exists",
  122. NSStringFromSelector(aMessagePattern.selector)];
  123. }
  124. Class interceptClass = KWSetupObjectInterceptSupport(self);
  125. KWSetupMethodInterceptSupport(interceptClass, aMessagePattern.selector);
  126. KWStub *stub = [KWStub stubWithMessagePattern:aMessagePattern block:block];
  127. KWAssociateObjectStub(self, stub, YES);
  128. }
  129. + (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern andReturn:(id)aValue
  130. {
  131. [self stubMessagePattern:aMessagePattern andReturn:aValue overrideExisting:YES];
  132. }
  133. + (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern andReturn:(id)aValue overrideExisting:(BOOL)override
  134. {
  135. if ([self methodSignatureForSelector:aMessagePattern.selector] == nil) {
  136. [NSException raise:@"KWStubException" format:@"cannot stub -%@ because no such method exists",
  137. NSStringFromSelector(aMessagePattern.selector)];
  138. }
  139. Class interceptClass = KWSetupObjectInterceptSupport(self);
  140. KWSetupMethodInterceptSupport(interceptClass, aMessagePattern.selector);
  141. KWStub *stub = [KWStub stubWithMessagePattern:aMessagePattern value:aValue];
  142. KWAssociateObjectStub(self, stub, override);
  143. }
  144. + (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern andReturn:(id)aValue times:(id)times afterThatReturn:(id)aSecondValue {
  145. [self stubMessagePattern:aMessagePattern andReturn:aValue times:times afterThatReturn:aSecondValue overrideExisting:YES];
  146. }
  147. + (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern andReturn:(id)aValue times:(id)times afterThatReturn:(id)aSecondValue overrideExisting:(BOOL)override {
  148. if ([self methodSignatureForSelector:aMessagePattern.selector] == nil) {
  149. [NSException raise:@"KWStubException" format:@"cannot stub -%@ because no such method exists",
  150. NSStringFromSelector(aMessagePattern.selector)];
  151. }
  152. Class interceptClass = KWSetupObjectInterceptSupport(self);
  153. KWSetupMethodInterceptSupport(interceptClass, aMessagePattern.selector);
  154. KWStub *stub = [KWStub stubWithMessagePattern:aMessagePattern value:aValue times:times afterThatReturn:aSecondValue];
  155. KWAssociateObjectStub(self, stub, override);
  156. }
  157. + (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern withBlock:(id (^)(NSArray *params))block {
  158. [self stubMessagePattern:aMessagePattern withBlock:block overrideExisting:YES];
  159. }
  160. + (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern withBlock:(id (^)(NSArray *params))block overrideExisting:(BOOL)override {
  161. if ([self methodSignatureForSelector:aMessagePattern.selector] == nil) {
  162. [NSException raise:@"KWStubException" format:@"cannot stub -%@ because no such method exists",
  163. NSStringFromSelector(aMessagePattern.selector)];
  164. }
  165. Class interceptClass = KWSetupObjectInterceptSupport(self);
  166. KWSetupMethodInterceptSupport(interceptClass, aMessagePattern.selector);
  167. KWStub *stub = [KWStub stubWithMessagePattern:aMessagePattern block:block];
  168. KWAssociateObjectStub(self, stub, override);
  169. }
  170. - (void)clearStubs {
  171. KWClearObjectStubs(self);
  172. }
  173. #pragma mark - Spying on Messages
  174. - (void)addMessageSpy:(id<KWMessageSpying>)aSpy forMessagePattern:(KWMessagePattern *)aMessagePattern {
  175. if ([self methodSignatureForSelector:aMessagePattern.selector] == nil) {
  176. [NSException raise:@"KWSpyException" format:@"cannot add spy for -%@ because no such method exists",
  177. NSStringFromSelector(aMessagePattern.selector)];
  178. }
  179. Class interceptClass = KWSetupObjectInterceptSupport(self);
  180. KWSetupMethodInterceptSupport(interceptClass, aMessagePattern.selector);
  181. KWAssociateMessageSpy(self, aSpy, aMessagePattern);
  182. }
  183. - (void)removeMessageSpy:(id<KWMessageSpying>)aSpy forMessagePattern:(KWMessagePattern *)aMessagePattern {
  184. KWClearObjectSpy(self, aSpy, aMessagePattern);
  185. }
  186. + (void)addMessageSpy:(id<KWMessageSpying>)aSpy forMessagePattern:(KWMessagePattern *)aMessagePattern {
  187. if ([self methodSignatureForSelector:aMessagePattern.selector] == nil) {
  188. [NSException raise:@"KWSpyException" format:@"cannot add spy for -%@ because no such method exists",
  189. NSStringFromSelector(aMessagePattern.selector)];
  190. }
  191. Class interceptClass = KWSetupObjectInterceptSupport(self);
  192. KWSetupMethodInterceptSupport(interceptClass, aMessagePattern.selector);
  193. KWAssociateMessageSpy(self, aSpy, aMessagePattern);
  194. }
  195. + (void)removeMessageSpy:(id<KWMessageSpying>)aSpy forMessagePattern:(KWMessagePattern *)aMessagePattern {
  196. KWClearObjectSpy(self, aSpy, aMessagePattern);
  197. }
  198. @end