Permalink
Browse files

Adding ability to use callbacks instead of returns

  • Loading branch information...
1 parent 29cde75 commit d17b9cf8c5903430554bc75b95d559ba773509cd @hammerdr committed Jan 26, 2010
Showing with 83 additions and 1 deletion.
  1. +33 −1 Framework/OJMoq/OJMoq.j
  2. +2 −0 Framework/OJMoq/OJMoqSelector.j
  3. +48 −0 tests/OJMoqTest.j
View
@@ -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]);
+ }
}
@@ -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
@@ -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

0 comments on commit d17b9cf

Please sign in to comment.