Objective-J Test API 0.1.0
Frameworks/OJMoq/OJMoqMock.j
Go to the documentation of this file.
00001 @import <Foundation/CPObject.j>
00002 @import "OJMoqSelector.j"
00003 @import "OJMoqAssert.j"
00004 @import "CPInvocation+Arguments.j"
00005 
00006 function mock(obj) {
00007         return [[OJMoqMock alloc] initWithBaseObject:obj];
00008 }
00009 
00010 @implementation OJMoqMock : CPObject
00011 {
00012         id                      baseObject                      @accessors;
00013         CPArray         expectations            @accessors;
00014         CPArray         selectors                       @accessors;
00015 }
00016 
00017 - (id)initWithBaseObject:(id)aBaseObject
00018 {
00019         self = [super init];
00020         if(self)
00021         {
00022                 baseObject = aBaseObject;
00023                 expectations = [CPArray array];
00024                 selectors = [CPArray array];
00025         }
00026         return self;
00027 }
00028 
00029 - (void)selector:(SEL)aSelector times:(CPNumber)times
00030 {
00031         [self selector:aSelector times:times arguments:[CPArray array]];
00032 }
00033 
00034 - (void)selector:(SEL)aSelector times:(CPNumber)times arguments:(CPArray)arguments
00035 {
00036         var selector = [self findOrCreateSelector:aSelector withArguments:arguments];
00037         [expectations addObject:function(){[OJMoqAssert selector:selector hasBeenCalled:times];}];
00038 }
00039 
00040 - (void)selector:(SEL)aSelector returns:(id)returnValue
00041 {
00042         [self selector:aSelector returns:returnValue arguments:[CPArray array]];
00043 }
00044 
00045 - (void)selector:(SEL)aSelector returns:(id)returnValue arguments:(CPArray)arguments
00046 {
00047         [[self findOrCreateSelector:aSelector withArguments:arguments] setReturnValue:returnValue];
00048 }
00049 
00050 - (void)selector:(SEL)aSelector callback:(Function)callback
00051 {
00052         [self selector:aSelector callback:callback arguments:[CPArray array]];
00053 }
00054 
00055 - (void)selector:(SEL)aSelector callback:(Function)callback arguments:(CPArray)arguments
00056 {
00057         [[self findOrCreateSelector:aSelector withArguments:arguments] setCallback:callback];
00058 }
00059 
00060 - (void)verifyThatAllExpectationsHaveBeenMet
00061 {
00062         expectations.forEach(function(expectation){expectation();});
00063 }
00064 
00065 - (OJMoqSelector)findOrCreateSelector:(SEL)aSelector withArguments:(CPArray)arguments
00066 {
00067         var foundSelectors = [OJMoqSelector find:[[OJMoqSelector alloc] initWithName:sel_getName(aSelector) 
00068                                                                                                                                         withArguments:arguments] 
00069                                             in:selectors ignoreWildcards:NO],
00070             selector = nil;
00071         
00072     if ([foundSelectors count] > 1)
00073         [CPException raise:CPInternalInconsistencyException reason:"Multiple selectors found with the exact same name and arguments"];
00074     else if ([foundSelectors count] === 1)
00075         selector = foundSelectors[0];
00076     else {
00077         selector = [[OJMoqSelector alloc] initWithName:aSelector withArguments:arguments];
00078                 [selectors addObject:selector];
00079         }
00080 
00081         return selector;
00082 }
00083 
00084 /* @ignore */
00085 - (CPMethodSignature)methodSignatureForSelector:(SEL)aSelector
00086 {
00087     return YES;
00088 }
00089 
00090 /* @ignore */
00091 - (void)forwardInvocation:(CPInvocation)anInvocation
00092 {
00093         if (![self respondsToSelector:[anInvocation selector]])
00094                 [CPException raise:"InvalidArgumentException" reason:"The selector " + [anInvocation selector] + " could not be found."];
00095         
00096         var foundSelectors = [OJMoqSelector find:[[OJMoqSelector alloc] initWithName:sel_getName([anInvocation selector]) 
00097                                                                                                                                         withArguments:[anInvocation userArguments]] 
00098                                             in:selectors ignoreWildcards:NO],
00099         count = [foundSelectors count];
00100 
00101         while (count--) {
00102                 var selector = foundSelectors[count],
00103                         returnValue = [selector returnValue],
00104                         callback = [selector callback];
00105                         
00106         [selector call];
00107                 [anInvocation setReturnValue:returnValue];
00108                 if(callback) callback([anInvocation userArguments]);
00109         }
00110 }
00111 
00115 - (BOOL)respondsToSelector:(SEL)aSelector
00116 {
00117     return [baseObject respondsToSelector:aSelector];
00118 }
00119 
00120 @end
 All Classes Files Functions Variables