Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when ignoreNonObjectArgs is unnecessary. #414

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -139,4 +141,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
3 changes: 1 addition & 2 deletions Source/OCMockTests/OCMockObjectMacroTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,6 @@ - (void)testReturnsCorrectObjectFromInitMethodCalledOnRecorderInsideMacro
// used and we're now making sure that a return value is specified for init methods.
id mock = OCMClassMock([NSString class]);
OCMStub([[mock andReturn:nil] initWithString:OCMOCK_ANY]);
OCMStub([[mock ignoringNonObjectArgs] initWithString:OCMOCK_ANY]);
OCMStub([[mock andReturnValue:nil] initWithString:OCMOCK_ANY]);
OCMStub([[mock andThrow:nil] initWithString:OCMOCK_ANY]);
OCMStub([[mock andPost:nil] initWithString:OCMOCK_ANY]);
Expand All @@ -565,7 +564,7 @@ - (void)testReturnsCorrectObjectFromInitMethodCalledOnRecorderInsideMacro
_OCMVerify([(id)[mock withQuantifier:nil] initWithString:OCMOCK_ANY]);

// Test multiple levels of recorder methods.
OCMStub([[[[mock ignoringNonObjectArgs] andReturn:nil] andThrow:nil] initWithString:OCMOCK_ANY]);
OCMStub([[[mock andReturn:nil] andThrow:nil] initWithString:OCMOCK_ANY]);
}

- (void)testStubMacroPassesExceptionThrough
Expand Down
28 changes: 26 additions & 2 deletions Source/OCMockTests/OCMockObjectTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,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 @@ -503,6 +504,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