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 Feb 15, 2021
1 parent 6358799 commit 7fa24af
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 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 @@ -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

0 comments on commit 7fa24af

Please sign in to comment.