KWReceiveMatcher.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // Licensed under the terms in License.txt
  3. //
  4. // Copyright 2010 Allen Ding. All rights reserved.
  5. //
  6. #import "KWReceiveMatcher.h"
  7. #import "KWFormatter.h"
  8. #import "KWInvocationCapturer.h"
  9. #import "KWMessagePattern.h"
  10. #import "KWMessageTracker.h"
  11. #import "KWObjCUtilities.h"
  12. #import "KWStringUtilities.h"
  13. #import "KWWorkarounds.h"
  14. #import "NSObject+KiwiStubAdditions.h"
  15. static NSString * const MatchVerifierKey = @"MatchVerifierKey";
  16. static NSString * const CountTypeKey = @"CountTypeKey";
  17. static NSString * const CountKey = @"CountKey";
  18. static NSString * const StubValueKey = @"StubValueKey";
  19. @interface KWReceiveMatcher()
  20. #pragma mark - Properties
  21. @property (nonatomic, readwrite, strong) KWMessageTracker *messageTracker;
  22. @end
  23. @implementation KWReceiveMatcher
  24. #pragma mark - Initializing
  25. - (id)initWithSubject:(id)anObject {
  26. self = [super initWithSubject:anObject];
  27. if (self) {
  28. _willEvaluateMultipleTimes = NO;
  29. }
  30. return self;
  31. }
  32. #pragma mark - Getting Matcher Strings
  33. + (NSArray *)matcherStrings {
  34. return @[@"receive:",
  35. @"receive:withCount:",
  36. @"receive:withCountAtLeast:",
  37. @"receive:withCountAtMost:",
  38. @"receive:andReturn:",
  39. @"receive:andReturn:withCount:",
  40. @"receive:andReturn:withCountAtLeast:",
  41. @"receive:andReturn:withCountAtMost:",
  42. @"receiveMessagePattern:countType:count:",
  43. @"receiveMessagePattern:andReturn:countType:count:",
  44. @"receiveUnspecifiedCountOfMessagePattern:",
  45. @"receiveUnspecifiedCountOfMessagePattern:andReturn:"];
  46. }
  47. #pragma mark - Matching
  48. - (BOOL)shouldBeEvaluatedAtEndOfExample {
  49. return YES;
  50. }
  51. - (BOOL)evaluate {
  52. BOOL succeeded = [self.messageTracker succeeded];
  53. if (!self.willEvaluateMultipleTimes) {
  54. [self.messageTracker stopTracking];
  55. }
  56. return succeeded;
  57. }
  58. #pragma mark - Getting Failure Messages
  59. - (NSString *)failureMessageForShould {
  60. return [NSString stringWithFormat:@"expected subject to receive -%@ %@, but received it %@",
  61. [self.messageTracker.messagePattern stringValue],
  62. [self.messageTracker expectedCountPhrase],
  63. [self.messageTracker receivedCountPhrase]];
  64. }
  65. - (NSString *)failureMessageForShouldNot {
  66. return [NSString stringWithFormat:@"expected subject not to receive -%@, but received it %@",
  67. [self.messageTracker.messagePattern stringValue],
  68. [self.messageTracker receivedCountPhrase]];
  69. }
  70. #pragma mark - Configuring Matchers
  71. - (void)receive:(SEL)aSelector {
  72. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
  73. [self receiveUnspecifiedCountOfMessagePattern:messagePattern];
  74. }
  75. - (void)receive:(SEL)aSelector withCount:(NSUInteger)aCount {
  76. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
  77. return [self receiveMessagePattern:messagePattern countType:KWCountTypeExact count:aCount];
  78. }
  79. - (void)receive:(SEL)aSelector withCountAtLeast:(NSUInteger)aCount {
  80. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
  81. return [self receiveMessagePattern:messagePattern countType:KWCountTypeAtLeast count:aCount];
  82. }
  83. - (void)receive:(SEL)aSelector withCountAtMost:(NSUInteger)aCount {
  84. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
  85. return [self receiveMessagePattern:messagePattern countType:KWCountTypeAtMost count:aCount];
  86. }
  87. - (void)receive:(SEL)aSelector andReturn:(id)aValue {
  88. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
  89. [self receiveUnspecifiedCountOfMessagePattern:messagePattern andReturn:aValue];
  90. }
  91. - (void)receive:(SEL)aSelector andReturn:(id)aValue withCount:(NSUInteger)aCount {
  92. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
  93. [self receiveMessagePattern:messagePattern andReturn:aValue countType:KWCountTypeExact count:aCount];
  94. }
  95. - (void)receive:(SEL)aSelector andReturn:(id)aValue withCountAtLeast:(NSUInteger)aCount {
  96. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
  97. [self receiveMessagePattern:messagePattern andReturn:aValue countType:KWCountTypeAtLeast count:aCount];
  98. }
  99. - (void)receive:(SEL)aSelector andReturn:(id)aValue withCountAtMost:(NSUInteger)aCount {
  100. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
  101. [self receiveMessagePattern:messagePattern andReturn:aValue countType:KWCountTypeAtMost count:aCount];
  102. }
  103. - (void)receiveUnspecifiedCountOfMessagePattern:(KWMessagePattern *)messagePattern {
  104. if (self.willEvaluateAgainstNegativeExpectation) {
  105. [self receiveMessagePattern:messagePattern countType:KWCountTypeAtLeast count:1];
  106. } else {
  107. [self receiveMessagePattern:messagePattern countType:KWCountTypeExact count:1];
  108. }
  109. }
  110. - (void)receiveUnspecifiedCountOfMessagePattern:(KWMessagePattern *)messagePattern andReturn:(id)aValue {
  111. if (self.willEvaluateAgainstNegativeExpectation) {
  112. [self receiveMessagePattern:messagePattern andReturn:aValue countType:KWCountTypeAtLeast count:1];
  113. } else {
  114. [self receiveMessagePattern:messagePattern andReturn:aValue countType:KWCountTypeExact count:1];
  115. }
  116. }
  117. - (void)receiveMessagePattern:(KWMessagePattern *)aMessagePattern countType:(KWCountType)aCountType count:(NSUInteger)aCount {
  118. #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
  119. @try {
  120. #endif // #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
  121. [self.subject stubMessagePattern:aMessagePattern andReturn:nil overrideExisting:NO];
  122. self.messageTracker = [KWMessageTracker messageTrackerWithSubject:self.subject messagePattern:aMessagePattern countType:aCountType count:aCount];
  123. #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
  124. } @catch(NSException *exception) {
  125. KWSetExceptionFromAcrossInvocationBoundary(exception);
  126. }
  127. #endif // #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
  128. }
  129. - (void)receiveMessagePattern:(KWMessagePattern *)aMessagePattern andReturn:(id)aValue countType:(KWCountType)aCountType count:(NSUInteger)aCount {
  130. #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
  131. @try {
  132. #endif // #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
  133. [self.subject stubMessagePattern:aMessagePattern andReturn:aValue];
  134. self.messageTracker = [KWMessageTracker messageTrackerWithSubject:self.subject messagePattern:aMessagePattern countType:aCountType count:aCount];
  135. #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
  136. } @catch(NSException *exception) {
  137. KWSetExceptionFromAcrossInvocationBoundary(exception);
  138. }
  139. #endif // #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
  140. }
  141. #pragma mark - Capturing Invocations
  142. + (NSMethodSignature *)invocationCapturer:(KWInvocationCapturer *)anInvocationCapturer methodSignatureForSelector:(SEL)aSelector {
  143. KWMatchVerifier *verifier = (anInvocationCapturer.userInfo)[MatchVerifierKey];
  144. if ([verifier.subject respondsToSelector:aSelector])
  145. return [verifier.subject methodSignatureForSelector:aSelector];
  146. NSString *encoding = KWEncodingForDefaultMethod();
  147. return [NSMethodSignature signatureWithObjCTypes:[encoding UTF8String]];
  148. }
  149. + (void)invocationCapturer:(KWInvocationCapturer *)anInvocationCapturer didCaptureInvocation:(NSInvocation *)anInvocation {
  150. NSDictionary *userInfo = anInvocationCapturer.userInfo;
  151. id verifier = userInfo[MatchVerifierKey];
  152. KWCountType countType = [userInfo[CountTypeKey] unsignedIntegerValue];
  153. NSUInteger count = [userInfo[CountKey] unsignedIntegerValue];
  154. id stubValue = userInfo[StubValueKey];
  155. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternFromInvocation:anInvocation];
  156. if (stubValue != nil)
  157. [verifier receiveMessagePattern:messagePattern andReturn:stubValue countType:countType count:count];
  158. else
  159. [verifier receiveMessagePattern:messagePattern countType:countType count:count];
  160. }
  161. @end
  162. @implementation KWMatchVerifier(KWReceiveMatcherAdditions)
  163. #pragma mark - Verifying
  164. - (void)receive:(SEL)aSelector withArguments:(id)firstArgument, ... {
  165. va_list argumentList;
  166. va_start(argumentList, firstArgument);
  167. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector firstArgumentFilter:firstArgument argumentList:argumentList];
  168. [(id)self receiveUnspecifiedCountOfMessagePattern:messagePattern];
  169. }
  170. - (void)receive:(SEL)aSelector withCount:(NSUInteger)aCount arguments:(id)firstArgument, ... {
  171. va_list argumentList;
  172. va_start(argumentList, firstArgument);
  173. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector firstArgumentFilter:firstArgument argumentList:argumentList];
  174. [(id)self receiveMessagePattern:messagePattern countType:KWCountTypeExact count:aCount];
  175. }
  176. - (void)receive:(SEL)aSelector withCountAtLeast:(NSUInteger)aCount arguments:(id)firstArgument, ... {
  177. va_list argumentList;
  178. va_start(argumentList, firstArgument);
  179. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector firstArgumentFilter:firstArgument argumentList:argumentList];
  180. [(id)self receiveMessagePattern:messagePattern countType:KWCountTypeAtLeast count:aCount];
  181. }
  182. - (void)receive:(SEL)aSelector withCountAtMost:(NSUInteger)aCount arguments:(id)firstArgument, ... {
  183. va_list argumentList;
  184. va_start(argumentList, firstArgument);
  185. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector firstArgumentFilter:firstArgument argumentList:argumentList];
  186. [(id)self receiveMessagePattern:messagePattern countType:KWCountTypeAtMost count:aCount];
  187. }
  188. - (void)receive:(SEL)aSelector andReturn:(id)aValue withArguments:(id)firstArgument, ... {
  189. va_list argumentList;
  190. va_start(argumentList, firstArgument);
  191. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector firstArgumentFilter:firstArgument argumentList:argumentList];
  192. [(id)self receiveUnspecifiedCountOfMessagePattern:messagePattern andReturn:aValue];
  193. }
  194. - (void)receive:(SEL)aSelector andReturn:(id)aValue withCount:(NSUInteger)aCount arguments:(id)firstArgument, ... {
  195. va_list argumentList;
  196. va_start(argumentList, firstArgument);
  197. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector firstArgumentFilter:firstArgument argumentList:argumentList];
  198. [(id)self receiveMessagePattern:messagePattern andReturn:aValue countType:KWCountTypeExact count:aCount];
  199. }
  200. - (void)receive:(SEL)aSelector andReturn:(id)aValue withCountAtLeast:(NSUInteger)aCount arguments:(id)firstArgument, ... {
  201. va_list argumentList;
  202. va_start(argumentList, firstArgument);
  203. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector firstArgumentFilter:firstArgument argumentList:argumentList];
  204. [(id)self receiveMessagePattern:messagePattern andReturn:aValue countType:KWCountTypeAtLeast count:aCount];
  205. }
  206. - (void)receive:(SEL)aSelector andReturn:(id)aValue withCountAtMost:(NSUInteger)aCount arguments:(id)firstArgument, ... {
  207. va_list argumentList;
  208. va_start(argumentList, firstArgument);
  209. KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector firstArgumentFilter:firstArgument argumentList:argumentList];
  210. [(id)self receiveMessagePattern:messagePattern andReturn:aValue countType:KWCountTypeAtMost count:aCount];
  211. }
  212. #pragma mark Invocation Capturing Methods
  213. - (NSDictionary *)userInfoForReceiveMatcherWithCountType:(KWCountType)aCountType count:(NSUInteger)aCount {
  214. return @{
  215. MatchVerifierKey: self,
  216. CountTypeKey: @(aCountType),
  217. CountKey: @(aCount)
  218. };
  219. }
  220. - (NSDictionary *)userInfoForReceiveMatcherWithCountType:(KWCountType)aCountType count:(NSUInteger)aCount value:(id)aValue {
  221. return @{
  222. MatchVerifierKey: self,
  223. CountTypeKey: @(aCountType),
  224. CountKey: @(aCount),
  225. StubValueKey: aValue
  226. };
  227. }
  228. - (id)receive {
  229. NSDictionary *userInfo = [self userInfoForReceiveMatcherWithCountType:KWCountTypeExact count:1];
  230. return [KWInvocationCapturer invocationCapturerWithDelegate:[KWReceiveMatcher class] userInfo:userInfo];
  231. }
  232. - (id)receiveWithCount:(NSUInteger)aCount {
  233. NSDictionary *userInfo = [self userInfoForReceiveMatcherWithCountType:KWCountTypeExact count:aCount];
  234. return [KWInvocationCapturer invocationCapturerWithDelegate:[KWReceiveMatcher class] userInfo:userInfo];
  235. }
  236. - (id)receiveWithCountAtLeast:(NSUInteger)aCount {
  237. NSDictionary *userInfo = [self userInfoForReceiveMatcherWithCountType:KWCountTypeAtLeast count:aCount];
  238. return [KWInvocationCapturer invocationCapturerWithDelegate:[KWReceiveMatcher class] userInfo:userInfo];
  239. }
  240. - (id)receiveWithCountAtMost:(NSUInteger)aCount {
  241. NSDictionary *userInfo = [self userInfoForReceiveMatcherWithCountType:KWCountTypeAtMost count:aCount];
  242. return [KWInvocationCapturer invocationCapturerWithDelegate:[KWReceiveMatcher class] userInfo:userInfo];
  243. }
  244. - (id)receiveAndReturn:(id)aValue {
  245. NSDictionary *userInfo = [self userInfoForReceiveMatcherWithCountType:KWCountTypeExact count:1 value:aValue];
  246. return [KWInvocationCapturer invocationCapturerWithDelegate:[KWReceiveMatcher class] userInfo:userInfo];
  247. }
  248. - (id)receiveAndReturn:(id)aValue withCount:(NSUInteger)aCount {
  249. NSDictionary *userInfo = [self userInfoForReceiveMatcherWithCountType:KWCountTypeExact count:aCount value:aValue];
  250. return [KWInvocationCapturer invocationCapturerWithDelegate:[KWReceiveMatcher class] userInfo:userInfo];
  251. }
  252. - (id)receiveAndReturn:(id)aValue withCountAtLeast:(NSUInteger)aCount {
  253. NSDictionary *userInfo = [self userInfoForReceiveMatcherWithCountType:KWCountTypeAtLeast count:aCount value:aValue];
  254. return [KWInvocationCapturer invocationCapturerWithDelegate:[KWReceiveMatcher class] userInfo:userInfo];
  255. }
  256. - (id)receiveAndReturn:(id)aValue withCountAtMost:(NSUInteger)aCount {
  257. NSDictionary *userInfo = [self userInfoForReceiveMatcherWithCountType:KWCountTypeAtMost count:aCount value:aValue];
  258. return [KWInvocationCapturer invocationCapturerWithDelegate:[KWReceiveMatcher class] userInfo:userInfo];
  259. }
  260. @end