Skip to content

Commit e7a8574

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 c81c481 commit e7a8574

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
@@ -485,9 +485,10 @@ - (void)testRaisesExceptionWhenMethodWithMixedArgumentsIsCalledWithWrongObjectAr
485485

486486
- (void)testBlocksAreNotConsideredNonObjectArguments
487487
{
488-
[[[mock stub] ignoringNonObjectArgs] enumerateLinesUsingBlock:[OCMArg invokeBlock]];
488+
mock = [OCMockObject mockForClass:[NSArray class]];
489+
[[[mock stub] ignoringNonObjectArgs] enumerateObjectsWithOptions:0 usingBlock:[OCMArg invokeBlock]];
489490
__block BOOL blockWasInvoked = NO;
490-
[mock enumerateLinesUsingBlock:^(NSString * _Nonnull line, BOOL * _Nonnull stop) {
491+
[mock enumerateObjectsWithOptions:5 usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
491492
blockWasInvoked = YES;
492493
}];
493494
XCTAssertTrue(blockWasInvoked, @"Should not have ignored the block argument.");
@@ -499,6 +500,29 @@ - (void)testThrowsWhenAttemptingToStubMethodOnStoppedMock
499500
XCTAssertThrowsSpecificNamed([[mock stub] rangeOfString:@"foo" options:0], NSException, NSInternalInconsistencyException);
500501
}
501502

503+
- (void)testRaisesExceptionWhenIgnoringNonObjectArgumentsOnMethodThatHasZeroArgs
504+
{
505+
@try
506+
{
507+
[[[mock stub] ignoringNonObjectArgs] uppercaseString];
508+
}
509+
@catch (NSException *e)
510+
{
511+
XCTAssertTrue([[e reason] containsString:@"0 args"]);
512+
}
513+
}
514+
515+
- (void)testRaisesExceptionWhenIgnoringNonObjectArgumentsOnMethodThatOnlyTakesObjects
516+
{
517+
@try
518+
{
519+
[[[mock stub] ignoringNonObjectArgs] stringByAppendingString:[OCMArg any]];
520+
}
521+
@catch (NSException *e)
522+
{
523+
XCTAssertTrue([[e reason] containsString:@"only object args"]);
524+
}
525+
}
502526

503527
#pragma mark returning values from stubbed methods
504528

0 commit comments

Comments
 (0)