123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- #import <Foundation/Foundation.h>
- #import <XCTest/XCTest.h>
- #import <gtest/gtest.h>
- #import <objc/runtime.h>
- using testing::TestCase;
- using testing::TestInfo;
- using testing::TestPartResult;
- using testing::UnitTest;
- static NSString * const GoogleTestDisabledPrefix = @"DISABLED_";
- static NSString * const GeneratedClassPrefix = @"";
- static NSDictionary *GoogleTestFilterMap;
- class XCTestListener : public testing::EmptyTestEventListener {
- public:
- XCTestListener(XCTestCase *testCase) :
- _testCase(testCase) {}
-
- void OnTestPartResult(const TestPartResult& test_part_result) {
- if (test_part_result.passed())
- return;
-
- int lineNumber = test_part_result.line_number();
- const char *fileName = test_part_result.file_name();
- NSString *path = fileName ? [@(fileName) stringByStandardizingPath] : nil;
- NSString *description = @(test_part_result.message());
- [_testCase recordFailureWithDescription:description
- inFile:path
- atLine:(lineNumber >= 0 ? (NSUInteger)lineNumber : 0)
- expected:YES];
- }
-
- private:
- XCTestCase *_testCase;
- };
- @interface GoogleTestLoader : NSObject
- @end
- @interface GoogleTestCase : XCTestCase
- @end
- @implementation GoogleTestCase
- + (NSBundle *)bundleForClass {
- return [NSBundle bundleForClass:[GoogleTestLoader class]];
- }
- + (NSArray *)testInvocations {
- NSMutableArray *invocations = [NSMutableArray array];
-
- unsigned int methodCount = 0;
- Method *methods = class_copyMethodList([self class], &methodCount);
-
- for (unsigned int i = 0; i < methodCount; i++) {
- SEL sel = method_getName(methods[i]);
- NSMethodSignature *sig = [self instanceMethodSignatureForSelector:sel];
- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
- [invocation setSelector:sel];
- [invocations addObject:invocation];
- }
-
- free(methods);
-
- return invocations;
- }
- @end
- static void RunTest(id self, SEL _cmd) {
- XCTestListener *listener = new XCTestListener(self);
- UnitTest *googleTest = UnitTest::GetInstance();
- googleTest->listeners().Append(listener);
-
- NSString *testKey = [NSString stringWithFormat:@"%@.%@", [self class], NSStringFromSelector(_cmd)];
- NSString *testFilter = GoogleTestFilterMap[testKey];
- XCTAssertNotNil(testFilter, @"No test filter found for test %@", testKey);
-
- testing::GTEST_FLAG(filter) = [testFilter UTF8String];
-
- (void)RUN_ALL_TESTS();
-
- delete googleTest->listeners().Release(listener);
-
- int totalTestsRun = googleTest->successful_test_count() + googleTest->failed_test_count();
- XCTAssertEqual(totalTestsRun, 1, @"Expected to run a single test for filter \"%@\"", testFilter);
- }
- @implementation GoogleTestLoader
- + (void)load {
- NSBundle *bundle = [NSBundle bundleForClass:self];
- [[NSNotificationCenter defaultCenter] addObserverForName:NSBundleDidLoadNotification object:bundle queue:nil usingBlock:^(NSNotification *notification) {
- [self registerTestClasses];
- }];
- }
- + (void)registerTestClasses {
-
- NSArray *arguments = [[NSProcessInfo processInfo] arguments];
-
- int i = 0;
- int argc = (int)[arguments count];
- const char **argv = (const char **)calloc((unsigned int)argc + 1, sizeof(const char *));
- for (NSString *arg in arguments) {
- argv[i++] = [arg UTF8String];
- }
-
- testing::InitGoogleTest(&argc, (char **)argv);
- UnitTest *googleTest = UnitTest::GetInstance();
- testing::TestEventListeners& listeners = googleTest->listeners();
- delete listeners.Release(listeners.default_result_printer());
- free(argv);
-
- BOOL runDisabledTests = testing::GTEST_FLAG(also_run_disabled_tests);
- NSMutableDictionary *testFilterMap = [NSMutableDictionary dictionary];
- NSCharacterSet *decimalDigitCharacterSet = [NSCharacterSet decimalDigitCharacterSet];
-
- for (int testCaseIndex = 0; testCaseIndex < googleTest->total_test_case_count(); testCaseIndex++) {
- const TestCase *testCase = googleTest->GetTestCase(testCaseIndex);
- NSString *testCaseName = @(testCase->name());
-
-
- NSArray *testCaseNameComponents = [testCaseName componentsSeparatedByString:@"/"];
-
- if (runDisabledTests == NO) {
- BOOL testCaseDisabled = NO;
-
- for (NSString *component in testCaseNameComponents) {
- if ([component hasPrefix:GoogleTestDisabledPrefix]) {
- testCaseDisabled = YES;
- break;
- }
- }
-
- if (testCaseDisabled) {
- continue;
- }
- }
-
-
-
- NSString *className = [GeneratedClassPrefix stringByAppendingString:[testCaseNameComponents componentsJoinedByString:@"_"]];
-
- Class testClass = objc_allocateClassPair([GoogleTestCase class], [className UTF8String], 0);
- NSAssert1(testClass, @"Failed to register Google Test class \"%@\", this class may already exist. The value of GeneratedClassPrefix can be changed to avoid this.", className);
- BOOL hasMethods = NO;
-
- for (int testIndex = 0; testIndex < testCase->total_test_count(); testIndex++) {
- const TestInfo *testInfo = testCase->GetTestInfo(testIndex);
- NSString *testName = @(testInfo->name());
- if (runDisabledTests == NO && [testName hasPrefix:GoogleTestDisabledPrefix]) {
- continue;
- }
-
-
-
- NSString *methodName = testName;
- if ([methodName length] > 0 && [decimalDigitCharacterSet characterIsMember:[methodName characterAtIndex:0]]) {
- methodName = [@"_" stringByAppendingString:methodName];
- }
-
- NSString *testKey = [NSString stringWithFormat:@"%@.%@", className, methodName];
- NSString *testFilter = [NSString stringWithFormat:@"%@.%@", testCaseName, testName];
- testFilterMap[testKey] = testFilter;
-
- SEL selector = sel_registerName([methodName UTF8String]);
- BOOL added = class_addMethod(testClass, selector, (IMP)RunTest, "v@:");
- NSAssert1(added, @"Failed to add Goole Test method \"%@\", this method may already exist in the class.", methodName);
- hasMethods = YES;
- }
-
- if (hasMethods) {
- objc_registerClassPair(testClass);
- } else {
- objc_disposeClassPair(testClass);
- }
- }
-
- GoogleTestFilterMap = testFilterMap;
- }
- @end
|