Skip to content

Commit

Permalink
Changed asserts to if/exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdoe committed Sep 29, 2015
1 parent b43225f commit a0a6755
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
22 changes: 7 additions & 15 deletions Source/OCMock/OCMBlockArgCaller.m
Expand Up @@ -48,14 +48,8 @@ - (NSInvocation *)buildInvocationFromBlock:(id)block
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];

NSUInteger argsLen = sig.numberOfArguments - 1;

/// @note Either allow all args or no args (all default) to avoid users
/// passing mismatching arguments.
NSAssert(
params.count == argsLen || params == nil,
@"All block arguments are required (%lu). Pass [OCMArg defaultValue] for default value.",
(unsigned long)argsLen
);
if((params != nil) && ([params count] != argsLen))
[NSException raise:NSInvalidArgumentException format:@"Specified too few arguments for block; expected (%lu) arguments.", (unsigned long)argsLen];

for(NSUInteger i = 0, j = 1; i < argsLen; ++i, ++j)
{
Expand All @@ -81,7 +75,7 @@ - (NSInvocation *)buildInvocationFromBlock:(id)block
}
else
{
[NSException raise:NSInvalidArgumentException format:@"Could not default type %s", typeEncoding];
[NSException raise:NSInvalidArgumentException format:@"Could not default type %s. Must specify arguments for this block.", typeEncoding];
}
}
else if (typeEncoding[0] == '@')
Expand All @@ -90,7 +84,8 @@ - (NSInvocation *)buildInvocationFromBlock:(id)block
}
else
{
NSAssert([param isKindOfClass:[NSValue class]], @"Argument at %lu should be boxed in NSValue", (long unsigned)i);
if(![param isKindOfClass:[NSValue class]])
[NSException raise:NSInvalidArgumentException format:@"Argument at index %lu should be boxed in NSValue.", (long unsigned)i];

char const *valEncoding = [param objCType];

Expand All @@ -99,11 +94,8 @@ - (NSInvocation *)buildInvocationFromBlock:(id)block
BOOL takesVoidPtr = !strcmp(typeEncoding, "^v") && valEncoding[0] == '^';
BOOL takesNumber = OCMNumberTypeForObjCType(typeEncoding) && OCMNumberTypeForObjCType(valEncoding);

NSAssert(
takesVoidPtr || takesNumber || OCMEqualTypesAllowingOpaqueStructs(typeEncoding, valEncoding),
@"Param type mismatch! You gave %s, block requires %s",
valEncoding, typeEncoding
);
if(!takesVoidPtr && !takesNumber && !OCMEqualTypesAllowingOpaqueStructs(typeEncoding, valEncoding))
[NSException raise:NSInvalidArgumentException format:@"Argument type mismatch; Block requires %s but argument provided is %s", typeEncoding, valEncoding];

NSUInteger argSize;
NSGetSizeAndAlignment(typeEncoding, &argSize, NULL);
Expand Down
6 changes: 3 additions & 3 deletions Source/OCMockTests/OCMockObjectTests.m
Expand Up @@ -642,7 +642,7 @@ - (void)testThrowsIfBoxedValueNotFound

[[mock stub] enumerateLinesUsingBlock:[OCMArg invokeBlockWithArgs:@"123", @"Not an NSValue", nil]];

XCTAssertThrowsSpecificNamed([mock enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {}], NSException, NSInternalInconsistencyException, @"No exception occurred");
XCTAssertThrowsSpecificNamed([mock enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {}], NSException, NSInvalidArgumentException, @"Should have raised an exception.");

}

Expand All @@ -651,7 +651,7 @@ - (void)testThrowsIfArgTypesMismatch

[[mock stub] enumerateLinesUsingBlock:[OCMArg invokeBlockWithArgs:@"123", OCMOCK_VALUE(YES), nil]];

XCTAssertThrowsSpecificNamed([mock enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {}], NSException, NSInternalInconsistencyException, @"No exception occurred");
XCTAssertThrowsSpecificNamed([mock enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {}], NSException, NSInvalidArgumentException, @"Should have raised an exception.");

}

Expand All @@ -660,7 +660,7 @@ - (void)testThrowsIfArgsLengthMismatch

[[mock stub] enumerateLinesUsingBlock:[OCMArg invokeBlockWithArgs:@"First but no second", nil]];

XCTAssertThrowsSpecificNamed([mock enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {}], NSException, NSInternalInconsistencyException, @"No exception occurred");
XCTAssertThrowsSpecificNamed([mock enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {}], NSException, NSInvalidArgumentException, @"Should have raised an exception.");

}

Expand Down

0 comments on commit a0a6755

Please sign in to comment.