Skip to content

Commit 7fa24af

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 6358799 commit 7fa24af

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
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
@@ -139,4 +141,27 @@ - (BOOL)matchesInvocation:(NSInvocation *)anInvocation
139141
return YES;
140142
}
141143

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

Source/OCMockTests/OCMockObjectMacroTests.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,6 @@ - (void)testReturnsCorrectObjectFromInitMethodCalledOnRecorderInsideMacro
552552
// used and we're now making sure that a return value is specified for init methods.
553553
id mock = OCMClassMock([NSString class]);
554554
OCMStub([[mock andReturn:nil] initWithString:OCMOCK_ANY]);
555-
OCMStub([[mock ignoringNonObjectArgs] initWithString:OCMOCK_ANY]);
556555
OCMStub([[mock andReturnValue:nil] initWithString:OCMOCK_ANY]);
557556
OCMStub([[mock andThrow:nil] initWithString:OCMOCK_ANY]);
558557
OCMStub([[mock andPost:nil] initWithString:OCMOCK_ANY]);
@@ -565,7 +564,7 @@ - (void)testReturnsCorrectObjectFromInitMethodCalledOnRecorderInsideMacro
565564
_OCMVerify([(id)[mock withQuantifier:nil] initWithString:OCMOCK_ANY]);
566565

567566
// Test multiple levels of recorder methods.
568-
OCMStub([[[[mock ignoringNonObjectArgs] andReturn:nil] andThrow:nil] initWithString:OCMOCK_ANY]);
567+
OCMStub([[[mock andReturn:nil] andThrow:nil] initWithString:OCMOCK_ANY]);
569568
}
570569

571570
- (void)testStubMacroPassesExceptionThrough

Source/OCMockTests/OCMockObjectTests.m

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

490490
- (void)testBlocksAreNotConsideredNonObjectArguments
491491
{
492-
[[[mock stub] ignoringNonObjectArgs] enumerateLinesUsingBlock:[OCMArg invokeBlock]];
492+
mock = [OCMockObject mockForClass:[NSArray class]];
493+
[[[mock stub] ignoringNonObjectArgs] enumerateObjectsWithOptions:0 usingBlock:[OCMArg invokeBlock]];
493494
__block BOOL blockWasInvoked = NO;
494-
[mock enumerateLinesUsingBlock:^(NSString *_Nonnull line, BOOL *_Nonnull stop) {
495+
[mock enumerateObjectsWithOptions:5 usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
495496
blockWasInvoked = YES;
496497
}];
497498
XCTAssertTrue(blockWasInvoked, @"Should not have ignored the block argument.");
@@ -503,6 +504,29 @@ - (void)testThrowsWhenAttemptingToStubMethodOnStoppedMock
503504
XCTAssertThrowsSpecificNamed([[mock stub] rangeOfString:@"foo" options:0], NSException, NSInternalInconsistencyException);
504505
}
505506

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

507531
#pragma mark returning values from stubbed methods
508532

0 commit comments

Comments
 (0)