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

Make compatible with xctool and Xcode 7 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
16 changes: 15 additions & 1 deletion Bundle/GoogleTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void OnTestPartResult(const TestPartResult& test_part_result) {
* Google Test cases or individual tests via Xcode.
*/
@interface GoogleTestLoader : NSObject
+ (void)registerTestClasses;
@end

/**
Expand All @@ -95,6 +96,11 @@ @interface GoogleTestCase : XCTestCase

@implementation GoogleTestCase

+ (void)initialize
{
[GoogleTestLoader registerTestClasses];
}

/**
* Associates generated Google Test classes with the test bundle.
*
Expand Down Expand Up @@ -172,11 +178,15 @@ @implementation GoogleTestLoader
+ (void)load {
NSBundle *bundle = [NSBundle bundleForClass:self];
[[NSNotificationCenter defaultCenter] addObserverForName:NSBundleDidLoadNotification object:bundle queue:nil usingBlock:^(NSNotification *notification) {
[self registerTestClasses];
[GoogleTestCase initialize];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this was done because registerTestClasses could implicitly invoke [GoogleTestCase initialize] and cause duplicated class registration because check if ([GoogleTestFilterMap count]) will fail.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling +initialize is frowned upon in ObjC; I think what you want is

if (self == [GoogleTestCase class]) {
    [self registerTestClasses];     
}

Does that solve it getting called twice?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

I had the bug where registerTestClasses was called twice, this is how I fixed it (I created the pull request also):

    NSArray* loadedClasses = [notification.userInfo objectForKey:NSLoadedClasses];

    if( loadedClasses != nil ) {
        if( [loadedClasses indexOfObject:@"GoogleTestLoader"] != NSNotFound) {
            [self registerTestClasses];
        }
    }

}];
}

+ (void)registerTestClasses {
if ([GoogleTestFilterMap count]) {
return;
}

// Pass the command-line arguments to Google Test to support the --gtest options
NSArray *arguments = [[NSProcessInfo processInfo] arguments];

Expand Down Expand Up @@ -241,6 +251,10 @@ + (void)registerTestClasses {
methodName = [@"_" stringByAppendingString:methodName];
}

// Google Test set test method name in parameterized tests to <name>/<index>.
// Replace / with a _ to create a valid method name.
methodName = [methodName stringByReplacingOccurrencesOfString:@"/" withString:@"_"];

NSString *testKey = [NSString stringWithFormat:@"%@.%@", className, methodName];
NSString *testFilter = [NSString stringWithFormat:@"%@.%@", testCaseName, testName];
testFilterMap[testKey] = testFilter;
Expand Down