OCMStubRecorder.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2004-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 "OCMStubRecorder.h"
  17. #import "OCClassMockObject.h"
  18. #import "OCMReturnValueProvider.h"
  19. #import "OCMBoxedReturnValueProvider.h"
  20. #import "OCMExceptionReturnValueProvider.h"
  21. #import "OCMIndirectReturnValueProvider.h"
  22. #import "OCMNotificationPoster.h"
  23. #import "OCMBlockCaller.h"
  24. #import "OCMRealObjectForwarder.h"
  25. #import "OCMFunctions.h"
  26. #import "OCMInvocationStub.h"
  27. @implementation OCMStubRecorder
  28. #pragma mark Initialisers, description, accessors, etc.
  29. - (id)init
  30. {
  31. self = [super init];
  32. invocationMatcher = [[OCMInvocationStub alloc] init];
  33. return self;
  34. }
  35. - (OCMInvocationStub *)stub
  36. {
  37. return (OCMInvocationStub *)invocationMatcher;
  38. }
  39. #pragma mark Recording invocation actions
  40. - (id)andReturn:(id)anObject
  41. {
  42. [[self stub] addInvocationAction:[[[OCMReturnValueProvider alloc] initWithValue:anObject] autorelease]];
  43. return self;
  44. }
  45. - (id)andReturnValue:(NSValue *)aValue
  46. {
  47. [[self stub] addInvocationAction:[[[OCMBoxedReturnValueProvider alloc] initWithValue:aValue] autorelease]];
  48. return self;
  49. }
  50. - (id)andThrow:(NSException *)anException
  51. {
  52. [[self stub] addInvocationAction:[[[OCMExceptionReturnValueProvider alloc] initWithValue:anException] autorelease]];
  53. return self;
  54. }
  55. - (id)andPost:(NSNotification *)aNotification
  56. {
  57. [[self stub] addInvocationAction:[[[OCMNotificationPoster alloc] initWithNotification:aNotification] autorelease]];
  58. return self;
  59. }
  60. - (id)andCall:(SEL)selector onObject:(id)anObject
  61. {
  62. [[self stub] addInvocationAction:[[[OCMIndirectReturnValueProvider alloc] initWithProvider:anObject andSelector:selector] autorelease]];
  63. return self;
  64. }
  65. - (id)andDo:(void (^)(NSInvocation *))aBlock
  66. {
  67. [[self stub] addInvocationAction:[[[OCMBlockCaller alloc] initWithCallBlock:aBlock] autorelease]];
  68. return self;
  69. }
  70. - (id)andForwardToRealObject
  71. {
  72. [[self stub] addInvocationAction:[[[OCMRealObjectForwarder alloc] init] autorelease]];
  73. return self;
  74. }
  75. #pragma mark Finishing recording
  76. - (void)forwardInvocation:(NSInvocation *)anInvocation
  77. {
  78. [super forwardInvocation:anInvocation];
  79. [mockObject addStub:[self stub]];
  80. }
  81. @end
  82. @implementation OCMStubRecorder (Properties)
  83. @dynamic _andReturn;
  84. - (OCMStubRecorder *(^)(NSValue *))_andReturn
  85. {
  86. id (^theBlock)(id) = ^ (NSValue *aValue)
  87. {
  88. if(OCMIsObjectType([aValue objCType]))
  89. {
  90. NSValue *objValue = nil;
  91. [aValue getValue:&objValue];
  92. return [self andReturn:objValue];
  93. }
  94. else
  95. {
  96. return [self andReturnValue:aValue];
  97. }
  98. };
  99. return [[theBlock copy] autorelease];
  100. }
  101. @dynamic _andThrow;
  102. - (OCMStubRecorder *(^)(NSException *))_andThrow
  103. {
  104. id (^theBlock)(id) = ^ (NSException * anException)
  105. {
  106. return [self andThrow:anException];
  107. };
  108. return [[theBlock copy] autorelease];
  109. }
  110. @dynamic _andPost;
  111. - (OCMStubRecorder *(^)(NSNotification *))_andPost
  112. {
  113. id (^theBlock)(id) = ^ (NSNotification * aNotification)
  114. {
  115. return [self andPost:aNotification];
  116. };
  117. return [[theBlock copy] autorelease];
  118. }
  119. @dynamic _andCall;
  120. - (OCMStubRecorder *(^)(id, SEL))_andCall
  121. {
  122. id (^theBlock)(id, SEL) = ^ (id anObject, SEL aSelector)
  123. {
  124. return [self andCall:aSelector onObject:anObject];
  125. };
  126. return [[theBlock copy] autorelease];
  127. }
  128. @dynamic _andDo;
  129. - (OCMStubRecorder *(^)(void (^)(NSInvocation *)))_andDo
  130. {
  131. id (^theBlock)(void (^)(NSInvocation *)) = ^ (void (^ blockToCall)(NSInvocation *))
  132. {
  133. return [self andDo:blockToCall];
  134. };
  135. return [[theBlock copy] autorelease];
  136. }
  137. @dynamic _andForwardToRealObject;
  138. - (OCMStubRecorder *(^)(void))_andForwardToRealObject
  139. {
  140. id (^theBlock)(void) = ^ (void)
  141. {
  142. return [self andForwardToRealObject];
  143. };
  144. return [[theBlock copy] autorelease];
  145. }
  146. @end