Objective-J Test API 0.1.0
Frameworks/OJMoq/OJMoqSpy.j
Go to the documentation of this file.
00001 @import <Foundation/CPObject.j>
00002 @import "OJMoqSelector.j"
00003 @import "OJMoqAssert.j"
00004 
00005 function spy(obj) 
00006 {
00007         return [OJMoqSpy spyOnBaseObject:obj];
00008 }
00009 
00010 @implementation OJMoqSpy : CPObject
00011 {
00012         CPObject                _baseObject             @accessors(property=baseObject);
00013         CPArray                 expectations;
00014         CPArray                 selectors;
00015 }
00016 
00017 + (OJMoqSpy)spyOnBaseObject:(id)baseObject
00018 {
00019         return [[OJMoqSpy alloc] initWithBaseObject:baseObject];
00020 }
00021 
00022 - (id)init
00023 {
00024         return [[OJMoqSpy alloc] initWithBaseObject:nil];
00025 }
00026 
00027 - (id)initWithBaseObject:(CPObject)baseObject
00028 {
00029         if(self = [super init]) {
00030                 _baseObject = baseObject;
00031                 
00032                 expectations = [];
00033                 selectors = [];
00034         }
00035         return self;
00036 }
00037 
00038 - (void)selector:(SEL)selector times:(CPNumber)times
00039 {
00040         [self selector:selector times:times arguments:[]];
00041 }
00042 
00043 - (void)selector:(SEL)selector times:(CPNumber)times arguments:(CPArray)arguments
00044 {
00045         [self replaceMethod:selector];
00046 
00047         var aSelector = [[OJMoqSelector alloc] initWithName:sel_getName(selector) withArguments:arguments];
00048         var expectationFunction = function(){[OJMoqAssert selector:aSelector hasBeenCalled:times];};
00049     [expectations addObject:expectationFunction];
00050         [selectors addObject:aSelector];
00051 }
00052 
00053 - (void)verifyThatAllExpectationsHaveBeenMet
00054 {
00055         expectations.forEach(function(expectation){
00056                 expectation();
00057         });
00058 }
00059 
00060 - (void)incrementNumberOfCallsForMethod:(SEL)selector arguments:(CPArray)userArguments
00061 {
00062         var foundSelectors = [OJMoqSelector find:[[OJMoqSelector alloc] initWithName:sel_getName(selector)
00063                                         withArguments:userArguments] in:selectors ignoreWildcards:NO],
00064         count = [foundSelectors count];
00065 
00066         while (count--)
00067         [foundSelectors[count] call];
00068 }
00069 
00070 - (void)replaceMethod:(SEL)selector
00071 {
00072         var aFunction = class_getMethodImplementation([_baseObject class], selector);
00073         class_replaceMethod([_baseObject class],
00074                 selector,
00075                 function(object, _cmd) {
00076                         if(object === _baseObject) {
00077                                 var userArguments = Array.prototype.slice.call(arguments).splice(2, arguments.length);
00078                                 [self incrementNumberOfCallsForMethod:selector arguments:userArguments];
00079                         }
00080                         return aFunction.apply(this, arguments);
00081                 });
00082 }
00083 
00084 @end
 All Classes Files Functions Variables