Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding ability to use callbacks instead of returns

  • Loading branch information...
commit d17b9cf8c5903430554bc75b95d559ba773509cd 1 parent 29cde75
@hammerdr authored
View
34 Framework/OJMoq/OJMoq.j
@@ -97,7 +97,28 @@ function moq(baseObject)
return self;
}
-- (id)verifyThatAllExpectationsHaveBeenMet
+- (OJMoq)selector:(SEL)aSelector callback:(Function)aCallback
+{
+ [self selector:aSelector callback:aCallback arguments:[CPArray array]];
+}
+
+- (OJMoq)selector:(SEL)aSelector callback:(Function)aCallback arguments:(CPArray)arguments
+{
+ var theSelector = [OJMoqSelector find:[[OJMoqSelector alloc] initWithName:sel_getName(aSelector) withArguments:arguments] in:selectors];
+
+ if(theSelector)
+ {
+ [theSelector setCallback:aCallback];
+ }
+ else
+ {
+ var aNewSelector = [[OJMoqSelector alloc] initWithName:sel_getName(aSelector) withArguments:arguments];
+ [aNewSelector setCallback:aCallback];
+ [selectors addObject:aNewSelector];
+ }
+}
+
+- (OJMoq)verifyThatAllExpectationsHaveBeenMet
{
for(var i = 0; i < [expectations count]; i++)
{
@@ -127,6 +148,7 @@ function moq(baseObject)
else
{
__ojmoq_setReturnValue(anInvocation, selectors);
+ __ojmoq_startCallback(anInvocation, selectors);
}
}
@@ -167,4 +189,14 @@ function __ojmoq_setReturnValue(anInvocation, selectors)
{
[anInvocation setReturnValue:[[CPObject alloc] init]];
}
+}
+
+function __ojmoq_startCallback(anInvocation, selectors)
+{
+ var theSelector = [OJMoqSelector find:[[OJMoqSelector alloc] initWithName:sel_getName([anInvocation selector])
+ withArguments:[anInvocation userArguments]] in:selectors];
+ if(theSelector)
+ {
+ [theSelector callback]([anInvocation userArguments]);
+ }
}
View
2  Framework/OJMoq/OJMoqSelector.j
@@ -7,6 +7,7 @@
CPNumber timesCalled @accessors(readonly);
id returnValue @accessors;
CPArray arguments @accessors;
+ Function callback @accessors;
}
+ (OJMoqSelector)find:(OJMoqSelector)aSelector in:(CPArray)selectors
@@ -32,6 +33,7 @@
arguments = someArguments;
timesCalled = 0;
returnValue = [[CPObject alloc] init];
+ callback = function(){};
}
return self;
}
View
48 tests/OJMoqTest.j
@@ -176,6 +176,54 @@
[self assertThrows:function(){[aMock someSelectorThatWillNeverEverExist];}];
}
+- (void)testThatOJMoqDoesCallCallbackWhenPassedSelector
+{
+ var aMock = moq();
+ var called = NO;
+
+ [aMock selector:@selector(a) callback:function(args){called = YES;}];
+
+ [aMock a];
+
+ [self assertTrue:called];
+}
+
+- (void)testThatOJMoqDoesNotCallCallbackWhenPassedAnotherSelector
+{
+ var aMock = moq();
+ var called = NO;
+
+ [aMock selector:@selector(a) callback:function(args){called = YES;}];
+
+ [aMock b];
+
+ [self assertFalse:called];
+}
+
+- (void)testThatOJMoqDoesCallCallbackWhenPassedSelectorWithArguments
+{
+ var aMock = moq();
+ var called = NO;
+
+ [aMock selector:@selector(a:) callback:function(args){called = YES;} arguments:["BOB"]];
+
+ [aMock a:"BOB"];
+
+ [self assertTrue:called];
+}
+
+- (void)testThatOJMoqDoesPassArgumentsToCallback
+{
+ var aMock = moq();
+ var result = nil;
+
+ [aMock selector:@selector(a:) callback:function(args){result = args[0];}];
+
+ [aMock a:"BOB"];
+
+ [self assert:"BOB" equals:result];
+}
+
// Adding these because ojtest does not have them. Should eventually
// do a pull request for these.
- (void)assert:(id)expected notEqual:(id)actual
Please sign in to comment.
Something went wrong with that request. Please try again.