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

Use new recordIssue: API #315

Merged
merged 4 commits into from
Apr 25, 2022
Merged
Changes from 2 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
58 changes: 35 additions & 23 deletions UnitTesting/GTMGoogleTestRunner.mm
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ @interface GTMGoogleTestRunner : XCTestCase {
#define IS_XCODE_11_4_OR_BETTER (__IPHONE_OS_VERSION_MAX_ALLOWED >= 130400 \
|| __MAC_OS_X_VERSION_MAX_ALLOWED >= 101504)

// We need Xcode 11.4 or better to support XCTIssue.
#define IS_XCODE_12_0_OR_BETTER (__IPHONE_OS_VERSION_MAX_ALLOWED >= 130700 \
|| __MAC_OS_X_VERSION_MAX_ALLOWED >= 101504)

#if IS_XCODE_11_4_OR_BETTER

// If a test has been skipped this will be set with an exception describing
Expand All @@ -118,8 +122,11 @@ - (id)initWithName:(NSString *)testName;
virtual ~GoogleTestPrinter() {}

virtual void OnTestPartResult(const TestPartResult &test_part_result) {
if (!test_part_result.passed()) {
const char *file_name = test_part_result.file_name();
if (test_part_result.passed()) {
return;
}

const char *file_name = test_part_result.file_name();
NSString *file = @(file_name ? file_name : "<file name unavailable>");
eytanbiala marked this conversation as resolved.
Show resolved Hide resolved
int line = test_part_result.line_number();
NSString *summary = @(test_part_result.summary());
Expand All @@ -130,37 +137,42 @@ virtual void OnTestPartResult(const TestPartResult &test_part_result) {
[summary stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
BOOL expected = test_part_result.nonfatally_failed();
#if IS_XCODE_11_4_OR_BETTER
eytanbiala marked this conversation as resolved.
Show resolved Hide resolved
XCTSourceCodeLocation *location = [[XCTSourceCodeLocation alloc] initWithFilePath:file lineNumber:line];
eytanbiala marked this conversation as resolved.
Show resolved Hide resolved
XCTSourceCodeContext *codeContext = [[XCTSourceCodeContext alloc] initWithLocation:location];

if (test_part_result.skipped()) {
// Test skipping works differently than other test failures in XCTest in
// that it is done via exceptions. We can't throw the exception from
// here though because it will be caught by the GUnit test
// infrastructure and reported as an error. So we record all of the data
// we need, and then throw the exception in -runGoogleTest once we are
// above the GUnit exception handling code.
XCTSourceCodeLocation *location =
[[XCTSourceCodeLocation alloc] initWithFilePath:file
lineNumber:line];
XCTSourceCodeContext *codeContext =
[[XCTSourceCodeContext alloc] initWithLocation:location];
XCTSkippedTestContext *skippedContext =
[[XCTSkippedTestContext alloc] initWithExplanation:oneLineSummary
evaluatedExpression:nil
message:nil
sourceCodeContext:codeContext];
NSDictionary *userInfo =
@{@"XCTestErrorUserInfoKeySkippedTestContext" : skippedContext};
test_case_.skipException =
[[_XCTSkipFailureException alloc] initWithName:@"_XCTSkipFailureException"
reason:@"Test skipped"
userInfo:userInfo];
XCTSkippedTestContext *skippedContext = [[XCTSkippedTestContext alloc] initWithExplanation:oneLineSummary
evaluatedExpression:nil
message:nil
sourceCodeContext:codeContext];
NSDictionary *userInfo = @{@"XCTestErrorUserInfoKeySkippedTestContext" : skippedContext};
test_case_.skipException = [[_XCTSkipFailureException alloc] initWithName:@"_XCTSkipFailureException"
reason:@"Test skipped"
userInfo:userInfo];
return;
} else {
#if IS_XCODE_12_0_OR_BETTER
XCTIssue *issue =
[[XCTIssue alloc] initWithType:expected ? XCTIssueTypeAssertionFailure : XCTIssueTypeUncaughtException
compactDescription:oneLineSummary
detailedDescription:summary
sourceCodeContext:codeContext
associatedError:nil
attachments:@[]];
[test_case_ recordIssue:issue];
return;
#endif // IS_XCODE_12_0_OR_BETTER
}

#else // IS_XCODE_11_4_OR_BETTER
[test_case_ recordFailureWithDescription:oneLineSummary inFile:file atLine:line expected:expected];
#endif
[test_case_ recordFailureWithDescription:oneLineSummary
inFile:file
atLine:line
expected:expected];
}
}

private:
Expand Down