Skip to content

Commit

Permalink
fixed the code according to carllindbergs comment - it will now also …
Browse files Browse the repository at this point in the history
…work for typedefs on classes that have ivars. Also adapted the tests, so that the new behavior is also tested.
  • Loading branch information
parallaxe committed Jan 7, 2014
1 parent d9d5a22 commit 761c3c3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Source/OCMock/OCMReturnValueProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ - (void)handleInvocation:(NSInvocation *)anInvocation
{
const char *returnType = [[anInvocation methodSignature] methodReturnTypeWithoutQualifiers];
if(strcmp(returnType, @encode(id)) != 0) {
// if the returnType is a typedef to an object, it has the form ^{OriginalClass=#}
NSString *regexString = @"^\\^\\{(.*)=#@*\\}";
// if the returnType is a typedef to an object, it has the form ^{OriginClass=#}
NSString *regexString = @"^\\^\\{(.*)=#.*\\}";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:0 error:NULL];
NSString *type = [NSString stringWithCString:returnType encoding:NSASCIIStringEncoding];
if([regex numberOfMatchesInString:type options:0 range:NSMakeRange(0, type.length)] == 0)
Expand Down
11 changes: 7 additions & 4 deletions Source/OCMockTests/OCMockObjectProtocolMocksTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ - (int)primitiveValue;
- (id)objectValue;
@end

@interface InterfaceForTypedef : NSObject
@interface InterfaceForTypedef : NSObject {
int prop1;
NSObject *prop2;
}
@end

@implementation InterfaceForTypedef
Expand Down Expand Up @@ -88,19 +91,19 @@ - (void)testDoesNotRespondToInvalidProtocolSelector
- (void)testWithTypedefReturnType {
id mock = [OCMockObject mockForProtocol:@protocol(ProtocolWithTypedefs)];
STAssertNoThrow([[[mock stub] andReturn:[TypedefInterface new]] typedefReturnValue1], @"Should accept a typedefed return-type");
STAssertNoThrow([mock typedefReturnValue1], @"bla");
STAssertNoThrow([mock typedefReturnValue1], nil);
}

- (void)testWithTypedefPointerReturnType {
id mock = [OCMockObject mockForProtocol:@protocol(ProtocolWithTypedefs)];
STAssertNoThrow([[[mock stub] andReturn:[TypedefInterface new]] typedefReturnValue2], @"Should accept a typedefed return-type");
STAssertNoThrow([mock typedefReturnValue2], @"bla");
STAssertNoThrow([mock typedefReturnValue2], nil);
}

- (void)testWithTypedefParameter {
id mock = [OCMockObject mockForProtocol:@protocol(ProtocolWithTypedefs)];
STAssertNoThrow([[mock stub] typedefParameter:nil], @"Should accept a typedefed parameter-type");
STAssertNoThrow([mock typedefParameter:nil], @"bla");
STAssertNoThrow([mock typedefParameter:nil], nil);
}


Expand Down

0 comments on commit 761c3c3

Please sign in to comment.