Skip to content

Commit

Permalink
Moved off deprecated API for reporting test failures.
Browse files Browse the repository at this point in the history
Apologies for the white space change noise.
  • Loading branch information
erikdoe committed Dec 4, 2020
1 parent 65ca893 commit ee79b16
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 77 deletions.
112 changes: 63 additions & 49 deletions Source/OCMock/OCMFunctions.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/

#import <objc/runtime.h>
#if !TARGET_OS_WATCH
#import <XCTest/XCTest.h>
#endif
#import "OCMFunctionsPrivate.h"
#import "OCClassMockObject.h"
#import "OCPartialMockObject.h"
Expand Down Expand Up @@ -121,51 +124,51 @@ CFNumberType OCMNumberTypeForObjCType(const char *objcType)

static BOOL ParseStructType(const char *type, const char **typeEnd, const char **typeNameEnd, const char **typeEqualSign)
{
if (type[0] != '{' && type[0] != '(')
return NO;

*typeNameEnd = NULL;
*typeEqualSign = NULL;

const char endChar = type[0] == '{' ? '}' : ')';
for (const char* ptr = type + 1; *ptr; ++ptr) {
switch (*ptr) {
case '(':
case '{':
{
const char *subTypeEnd;
const char *subTypeNameEnd;
const char *subTypeEqualSign;
if (!ParseStructType(ptr, &subTypeEnd, &subTypeNameEnd, &subTypeEqualSign))
return NO;
ptr = subTypeEnd;
break;
}
case '=':
{
if (!*typeEqualSign) {
*typeNameEnd = ptr;
*typeEqualSign = ptr;
}
break;
}
case ')':
case '}':
{
if (*ptr == endChar) {
*typeEnd = ptr;
if (!*typeNameEnd)
*typeNameEnd = ptr;
return YES;
}
break;
}
default:
break;
}
}

return NO;
if (type[0] != '{' && type[0] != '(')
return NO;

*typeNameEnd = NULL;
*typeEqualSign = NULL;

const char endChar = type[0] == '{' ? '}' : ')';
for (const char* ptr = type + 1; *ptr; ++ptr) {
switch (*ptr) {
case '(':
case '{':
{
const char *subTypeEnd;
const char *subTypeNameEnd;
const char *subTypeEqualSign;
if (!ParseStructType(ptr, &subTypeEnd, &subTypeNameEnd, &subTypeEqualSign))
return NO;
ptr = subTypeEnd;
break;
}
case '=':
{
if (!*typeEqualSign) {
*typeNameEnd = ptr;
*typeEqualSign = ptr;
}
break;
}
case ')':
case '}':
{
if (*ptr == endChar) {
*typeEnd = ptr;
if (!*typeNameEnd)
*typeNameEnd = ptr;
return YES;
}
break;
}
default:
break;
}
}

return NO;
}


Expand Down Expand Up @@ -220,8 +223,8 @@ static BOOL OCMEqualTypesAllowingOpaqueStructsInternal(const char *type1, const

/* If the names are not equal and neither of the names is a question mark, return NO */
if ((type1NameLen != type2NameLen || strncmp(type1, type2, type1NameLen)) &&
!((type1NameLen == 2) && (type1[1] == '?')) && !((type2NameLen == 2) && (type2[1] == '?')) &&
!(type1NameLen == 1 || type2NameLen == 1))
!((type1NameLen == 2) && (type1[1] == '?')) && !((type2NameLen == 2) && (type2[1] == '?')) &&
!(type1NameLen == 1 || type2NameLen == 1))
return NO;

/* If the same name, and at least one is opaque, that is close enough. */
Expand Down Expand Up @@ -302,7 +305,7 @@ BOOL OCMIsNilValue(const char *objectCType, const void *value, size_t valueSize)
for(size_t i = 0; i < valueSize; i++)
if(((const char *)value)[i] != 0)
return NO;

// Depending on the compilation settings of the file where the return value gets recorded,
// nil and Nil get potentially different encodings. Check all known encodings.
if((strcmp(objectCType, @encode(void *)) == 0) || // Standard Objective-C
Expand Down Expand Up @@ -450,7 +453,18 @@ void OCMSetAssociatedMockForObject(OCClassMockObject *mock, id anObject)
void OCMReportFailure(OCMLocation *loc, NSString *description)
{
id testCase = [loc testCase];
if((testCase != nil) && [testCase respondsToSelector:@selector(recordFailureWithDescription:inFile:atLine:expected:)])
#if !TARGET_OS_WATCH
if((testCase != nil) && [testCase respondsToSelector:@selector(recordIssue:)])
{
XCTSourceCodeLocation *xctloc = [[[XCTSourceCodeLocation alloc] initWithFilePath:[loc file] lineNumber:[loc line]] autorelease];
XCTSourceCodeContext *xctctx = [[[XCTSourceCodeContext alloc] initWithLocation:xctloc] autorelease];
XCTIssue *issue = [[[XCTIssue alloc] initWithType:XCTIssueTypeAssertionFailure compactDescription:description
detailedDescription:nil sourceCodeContext:xctctx associatedError:nil attachments:[NSArray array]] autorelease];
[testCase recordIssue:issue];
}
else
#endif
if((testCase != nil) && [testCase respondsToSelector:@selector(recordFailureWithDescription:inFile:atLine:expected:)])
{
[testCase recordFailureWithDescription:description inFile:[loc file] atLine:[loc line] expected:NO];
}
Expand Down
4 changes: 2 additions & 2 deletions Source/OCMockTests/OCMQuantifierTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ - (void)setUp
expectFailure = NO;
}

- (void)recordFailureWithDescription:(NSString *)description inFile:(NSString *)file atLine:(NSUInteger)line expected:(BOOL)expected
- (void)recordIssue:(XCTIssue *)issue
{
if(expectFailure)
{
didRecordFailure = YES;
}
else
{
[super recordFailureWithDescription:description inFile:file atLine:line expected:expected];
[super recordIssue:issue];
}
}

Expand Down
52 changes: 26 additions & 26 deletions Source/OCMockTests/OCMockObjectMacroTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,39 +82,39 @@ @interface OCMockObjectMacroTests : XCTestCase
BOOL shouldCaptureFailure;
NSString *reportedDescription;
NSString *reportedFile;
NSUInteger reportedLine;
NSInteger reportedLine;
}

@end


@implementation OCMockObjectMacroTests

- (void)recordFailureWithDescription:(NSString *)description inFile:(NSString *)file atLine:(NSUInteger)line expected:(BOOL)expected
- (void)recordIssue:(XCTIssue *)issue
{
if(shouldCaptureFailure)
{
reportedDescription = description;
reportedFile = file;
reportedLine = line;
reportedDescription = issue.compactDescription;
reportedFile = issue.sourceCodeContext.location.fileURL.path;
reportedLine = issue.sourceCodeContext.location.lineNumber;
}
else
{
[super recordFailureWithDescription:description inFile:file atLine:line expected:expected];
[super recordIssue:issue];
}
}


- (void)testReportsVerifyFailureWithCorrectLocation
{
id mock = OCMClassMock([NSString class]);

[[mock expect] lowercaseString];

shouldCaptureFailure = YES;
OCMVerifyAll(mock); const char *expectedFile = __FILE__; int expectedLine = __LINE__;
shouldCaptureFailure = NO;

XCTAssertNotNil(reportedDescription, @"Should have recorded a failure with description.");
XCTAssertEqualObjects([NSString stringWithUTF8String:expectedFile], reportedFile, @"Should have reported correct file.");
XCTAssertEqual(expectedLine, (int)reportedLine, @"Should have reported correct line");
Expand All @@ -123,7 +123,7 @@ - (void)testReportsVerifyFailureWithCorrectLocation
- (void)testReportsIgnoredExceptionsAtVerifyLocation
{
id mock = OCMClassMock([NSString class]);

[[mock reject] lowercaseString];

@try
Expand All @@ -138,7 +138,7 @@ - (void)testReportsIgnoredExceptionsAtVerifyLocation
shouldCaptureFailure = YES;
OCMVerifyAll(mock); const char *expectedFile = __FILE__; int expectedLine = __LINE__;
shouldCaptureFailure = NO;

XCTAssertTrue([reportedDescription rangeOfString:@"ignored"].location != NSNotFound, @"Should have reported ignored exceptions.");
XCTAssertEqualObjects([NSString stringWithUTF8String:expectedFile], reportedFile, @"Should have reported correct file.");
XCTAssertEqual(expectedLine, (int)reportedLine, @"Should have reported correct line");
Expand Down Expand Up @@ -385,14 +385,14 @@ - (void)testShouldThrowExceptionWhenNotUsingMockInMacroThatRequiresMock
- (void)testShouldHintAtPossibleReasonWhenNotUsingMockInMacroThatRequiresMock
{
@try
{
{
id realObject = [NSMutableArray array];
OCMStub([realObject addObject:@"foo"]);
}
@catch (NSException *e)
{
XCTAssertTrue([[e reason] containsString:@"The receiver is not a mock object."]);
}
OCMStub([realObject addObject:@"foo"]);
}
@catch (NSException *e)
{
XCTAssertTrue([[e reason] containsString:@"The receiver is not a mock object."]);
}
}

- (void)testShouldThrowExceptionWhenMockingMethodThatCannotBeMocked
Expand All @@ -407,15 +407,15 @@ - (void)testShouldThrowExceptionWhenMockingMethodThatCannotBeMocked

- (void)testShouldHintAtPossibleReasonWhenMockingMethodThatCannotBeMocked
{
@try
{
@try
{
id mock = OCMClassMock([NSString class]);
OCMStub([mock description]);
}
@catch (NSException *e)
{
XCTAssertTrue([[e reason] containsString:@"The selector conflicts with a selector implemented by OCMStubRecorder/OCMExpectationRecorder."]);
}
OCMStub([mock description]);
}
@catch (NSException *e)
{
XCTAssertTrue([[e reason] containsString:@"The selector conflicts with a selector implemented by OCMStubRecorder/OCMExpectationRecorder."]);
}
}

- (void)testShouldHintAtPossibleReasonWhenVerifyingMethodThatCannotBeMocked
Expand Down

0 comments on commit ee79b16

Please sign in to comment.