Skip to content

Commit 584a44a

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 90693d3 commit 584a44a

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,14 +465,38 @@ - (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.");
474475
}
475476

477+
- (void)testRaisesExceptionWhenIgnoringNonObjectArgumentsOnMethodThatHasZeroArgs
478+
{
479+
@try
480+
{
481+
[[[mock stub] ignoringNonObjectArgs] uppercaseString];
482+
}
483+
@catch (NSException *e)
484+
{
485+
XCTAssertTrue([[e reason] containsString:@"0 args"]);
486+
}
487+
}
488+
489+
- (void)testRaisesExceptionWhenIgnoringNonObjectArgumentsOnMethodThatOnlyTakesObjects
490+
{
491+
@try
492+
{
493+
[[[mock stub] ignoringNonObjectArgs] stringByAppendingString:[OCMArg any]];
494+
}
495+
@catch (NSException *e)
496+
{
497+
XCTAssertTrue([[e reason] containsString:@"only object args"]);
498+
}
499+
}
476500

477501
#pragma mark returning values from stubbed methods
478502

0 commit comments

Comments
 (0)