Skip to content

Commit

Permalink
Error when ignoreNonObjectArgs is unnecessary.
Browse files Browse the repository at this point in the history
This encourages proper use of the APIS and keeps the testing code cleaner and easier to
read.
  • Loading branch information
dmaclach committed Jan 2, 2021
1 parent c81c481 commit e7a8574
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
25 changes: 25 additions & 0 deletions Source/OCMock/OCMInvocationMatcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ - (void)setInvocation:(NSInvocation *)anInvocation
// anInvocation contains self as an argument, -retainArguments would create a retain cycle.
[anInvocation retainObjectArgumentsExcludingObject:self];
recordedInvocation = [anInvocation retain];
[self verifyInvocationCompatibleWithIgnoreNonObjectArgs];
}

- (void)setRecordedAsClassMethod:(BOOL)flag
Expand All @@ -60,6 +61,7 @@ - (BOOL)recordedAsClassMethod
- (void)setIgnoreNonObjectArgs:(BOOL)flag
{
ignoreNonObjectArgs = flag;
[self verifyInvocationCompatibleWithIgnoreNonObjectArgs];
}

- (NSString *)description
Expand Down Expand Up @@ -142,4 +144,27 @@ - (BOOL)matchesInvocation:(NSInvocation *)anInvocation
return YES;
}

- (void)verifyInvocationCompatibleWithIgnoreNonObjectArgs
{
if (!recordedInvocation || !ignoreNonObjectArgs)
{
return;
}
NSMethodSignature *signature = [recordedInvocation methodSignature];
NSUInteger n = [signature numberOfArguments];
BOOL foundNonObjectArg = NO;
for(NSUInteger i = 2; i < n; i++)
{
if(!OCMIsObjectType([signature getArgumentTypeAtIndex:i]))
{
foundNonObjectArg = YES;
break;
}
}
if (!foundNonObjectArg)
{
[NSException raise:NSInvalidArgumentException format:@"Method `%@` with %@ marked as ignoreNonObjectArgs.", NSStringFromSelector([recordedInvocation selector]), n == 2 ? @"0 args" : @"only object args"];
}
}

@end
28 changes: 26 additions & 2 deletions Source/OCMockTests/OCMockObjectTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,10 @@ - (void)testRaisesExceptionWhenMethodWithMixedArgumentsIsCalledWithWrongObjectAr

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

- (void)testRaisesExceptionWhenIgnoringNonObjectArgumentsOnMethodThatHasZeroArgs
{
@try
{
[[[mock stub] ignoringNonObjectArgs] uppercaseString];
}
@catch (NSException *e)
{
XCTAssertTrue([[e reason] containsString:@"0 args"]);
}
}

- (void)testRaisesExceptionWhenIgnoringNonObjectArgumentsOnMethodThatOnlyTakesObjects
{
@try
{
[[[mock stub] ignoringNonObjectArgs] stringByAppendingString:[OCMArg any]];
}
@catch (NSException *e)
{
XCTAssertTrue([[e reason] containsString:@"only object args"]);
}
}

#pragma mark returning values from stubbed methods

Expand Down

0 comments on commit e7a8574

Please sign in to comment.