Skip to content

Commit 3da072d

Browse files
committed
Error when ignoreNonObjectArgs is unnecessary.
This encourages proper use of the APIS and keeps the testing code cleaner and easier to read.
1 parent ffeeed2 commit 3da072d

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

Source/OCMock/OCMInvocationMatcher.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ - (void)setInvocation:(NSInvocation *)anInvocation
4545
// anInvocation contains self as an argument, -retainArguments would create a retain cycle.
4646
[anInvocation retainObjectArgumentsExcludingObject:self];
4747
recordedInvocation = [anInvocation retain];
48+
[self verifyInvocationCompatibleWithIgnoreNonObjectArgs];
4849
}
4950

5051
- (void)setRecordedAsClassMethod:(BOOL)flag
@@ -60,6 +61,7 @@ - (BOOL)recordedAsClassMethod
6061
- (void)setIgnoreNonObjectArgs:(BOOL)flag
6162
{
6263
ignoreNonObjectArgs = flag;
64+
[self verifyInvocationCompatibleWithIgnoreNonObjectArgs];
6365
}
6466

6567
- (NSString *)description
@@ -142,4 +144,27 @@ - (BOOL)matchesInvocation:(NSInvocation *)anInvocation
142144
return YES;
143145
}
144146

147+
- (void)verifyInvocationCompatibleWithIgnoreNonObjectArgs
148+
{
149+
if (!recordedInvocation || !ignoreNonObjectArgs)
150+
{
151+
return;
152+
}
153+
NSMethodSignature *signature = [recordedInvocation methodSignature];
154+
NSUInteger n = [signature numberOfArguments];
155+
BOOL foundNonObjectArg = NO;
156+
for(NSUInteger i = 2; i < n; i++)
157+
{
158+
if(!OCMIsObjectType([signature getArgumentTypeAtIndex:i]))
159+
{
160+
foundNonObjectArg = YES;
161+
break;
162+
}
163+
}
164+
if (!foundNonObjectArg)
165+
{
166+
[NSException raise:NSInvalidArgumentException format:@"Method `%@` with %@ marked as ignoreNonObjectArgs.", NSStringFromSelector([recordedInvocation selector]), n == 2 ? @"0 args" : @"only object args"];
167+
}
168+
}
169+
145170
@end

Source/OCMockTests/OCMockObjectTests.m

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,10 @@ - (void)testRaisesExceptionWhenMethodWithMixedArgumentsIsCalledWithWrongObjectAr
465465

466466
- (void)testBlocksAreNotConsideredNonObjectArguments
467467
{
468-
[[[mock stub] ignoringNonObjectArgs] enumerateLinesUsingBlock:[OCMArg invokeBlock]];
468+
mock = [OCMockObject mockForClass:[NSArray class]];
469+
[[[mock stub] ignoringNonObjectArgs] enumerateObjectsWithOptions:0 usingBlock:[OCMArg invokeBlock]];
469470
__block BOOL blockWasInvoked = NO;
470-
[mock enumerateLinesUsingBlock:^(NSString * _Nonnull line, BOOL * _Nonnull stop) {
471+
[mock enumerateObjectsWithOptions:5 usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
471472
blockWasInvoked = YES;
472473
}];
473474
XCTAssertTrue(blockWasInvoked, @"Should not have ignored the block argument.");
@@ -479,6 +480,29 @@ - (void)testThrowsWhenAttemptingToStubMethodOnStoppedMock
479480
XCTAssertThrowsSpecificNamed([[mock stub] rangeOfString:@"foo" options:0], NSException, NSInternalInconsistencyException);
480481
}
481482

483+
- (void)testRaisesExceptionWhenIgnoringNonObjectArgumentsOnMethodThatHasZeroArgs
484+
{
485+
@try
486+
{
487+
[[[mock stub] ignoringNonObjectArgs] uppercaseString];
488+
}
489+
@catch (NSException *e)
490+
{
491+
XCTAssertTrue([[e reason] containsString:@"0 args"]);
492+
}
493+
}
494+
495+
- (void)testRaisesExceptionWhenIgnoringNonObjectArgumentsOnMethodThatOnlyTakesObjects
496+
{
497+
@try
498+
{
499+
[[[mock stub] ignoringNonObjectArgs] stringByAppendingString:[OCMArg any]];
500+
}
501+
@catch (NSException *e)
502+
{
503+
XCTAssertTrue([[e reason] containsString:@"only object args"]);
504+
}
505+
}
482506

483507
#pragma mark returning values from stubbed methods
484508

0 commit comments

Comments
 (0)