-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomXCTestRunner.m
224 lines (170 loc) · 6.14 KB
/
CustomXCTestRunner.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// CustomXCTestRunner.h
// CustomXCTestRunner (Objective-C)
//
// Created by Stanislaw Pankevich on 30/03/18.
// Copyright © 2016 Lowlevelbits.org. All rights reserved.
//
#import "CustomXCTestRunner.h"
#import <XCTest/XCTest.h>
#import <XCTest/XCTestObservationCenter.h>
#import <objc/runtime.h>
static NSString *getTestName(NSString *fullTestName) {
NSString *searchedString = fullTestName;
NSRange searchedRange = NSMakeRange(0, [searchedString length]);
NSString *pattern = @"^-\\[[A-Za-z0-9_]+ ([A-Za-z0-9_]+)\\]$";
NSError *error = nil;
NSRegularExpression* regex =
[NSRegularExpression regularExpressionWithPattern:pattern
options:0
error:&error];
NSArray *matches = [regex matchesInString:searchedString
options:0
range:searchedRange];
if (matches.count == 0) {
return nil;
}
for (NSTextCheckingResult* match in matches) {
// NSString *matchText = [searchedString substringWithRange:[match range]];
NSRange group1 = [match rangeAtIndex:1];
return [searchedString substringWithRange:group1];
}
return nil;
}
@interface CustomXCTestObserver : NSObject <XCTestObservation>
@property (assign, nonatomic) NSUInteger testsFailed;
@end
@implementation CustomXCTestObserver
- (instancetype)init {
self = [super init];
self.testsFailed = 0;
return self;
}
//- (void)testBundleWillStart:(NSBundle *)testBundle {
// NSLog(@"testBundleWillStart: %@", testBundle);
//}
//
//- (void)testBundleDidFinish:(NSBundle *)testBundle {
// NSLog(@"testBundleDidFinish: %@", testBundle);
//}
//
//- (void)testSuiteWillStart:(XCTestSuite *)testSuite {
// NSLog(@"testSuiteWillStart: %@", testSuite);
//}
//
//- (void)testCaseWillStart:(XCTestCase *)testCase {
// NSLog(@"testCaseWillStart: %@", testCase);
//}
//
//- (void)testSuiteDidFinish:(XCTestSuite *)testSuite {
// NSLog(@"testSuiteDidFinish: %@", testSuite);
//}
//
//- (void)testSuite:(XCTestSuite *)testSuite didFailWithDescription:(NSString *)description inFile:(NSString *)filePath atLine:(NSUInteger)lineNumber {
// NSLog(@"testSuite:didFailWithDescription:inFile:atLine: %@ %@ %@ %tu",
// testSuite, description, filePath, lineNumber);
//}
//
- (void)testCase:(XCTestCase *)testCase didFailWithDescription:(NSString *)description inFile:(NSString *)filePath atLine:(NSUInteger)lineNumber {
//NSLog(@"testCase:didFailWithDescription:inFile:atLine: %@ %@ %@ %tu",
// testCase, description, filePath, lineNumber);
self.testsFailed++;
}
//- (void)testCaseDidFinish:(XCTestCase *)testCase {
// NSLog(@"testCaseWillFinish: %@", testCase);
//}
@end
static void ObjCEnumerateRuntimeClasses(void(^callback)(Class)) {
int numClasses;
numClasses = objc_getClassList(NULL, 0);
if (numClasses == 0) {
return;
}
Class * classes = (Class *)malloc(sizeof(Class) * numClasses);
numClasses = objc_getClassList(classes, numClasses);
for (int i = 0; i < numClasses; i++) {
Class runtimeClass = classes[i];
callback(runtimeClass);
}
free(classes);
}
int CustomXCTestRunnerRunAll(void) {
int exitResult = 0;
@autoreleasepool {
CustomXCTestObserver *testObserver = [CustomXCTestObserver new];
Class xcTestCaseClass = NSClassFromString(@"XCTestCase");
NSCAssert(xcTestCaseClass, nil);
XCTestSuite *customXCTestRunnerSuite =
[[XCTestSuite alloc] initWithName:@"CustomXCTestRunner Suite"];
ObjCEnumerateRuntimeClasses(^(__unsafe_unretained Class runtimeClass) {
if (class_getSuperclass(runtimeClass) != xcTestCaseClass) {
return;
}
XCTestSuite *suite = [XCTestSuite testSuiteForTestCaseClass:runtimeClass];
[customXCTestRunnerSuite addTest:suite];
});
XCTestObservationCenter *center = [XCTestObservationCenter sharedTestObservationCenter];
[center addTestObserver:testObserver];
[customXCTestRunnerSuite runTest];
if (testObserver.testsFailed > 0) {
exitResult = 1;
}
}
return exitResult;
}
int CustomXCTestRunnerRunOne(const char *const testName) {
int exitResult = 0;
@autoreleasepool {
CustomXCTestObserver *testObserver = [CustomXCTestObserver new];
NSString *testNameString = [NSString stringWithUTF8String:testName];
NSArray *testPair = [testNameString componentsSeparatedByString:@"."];
if (testPair.count != 2) {
return -1;
}
NSString *classString = testPair[0];
NSString *testString = testPair[1];
XCTestSuite *customXCTestRunnerSuite =
[[XCTestSuite alloc] initWithName:@"CustomXCTestRunner Suite"];
XCTestSuite *suite = [XCTestSuite testSuiteForTestCaseWithName:classString];
XCTest *foundTest = nil;
for (XCTest *test in suite.tests) {
NSString *testName = getTestName(test.name);
if ([testName isEqualToString:testString]) {
foundTest = test;
}
}
if (foundTest == nil) {
return -1;
}
[customXCTestRunnerSuite addTest:foundTest];
XCTestObservationCenter *center = [XCTestObservationCenter sharedTestObservationCenter];
[center addTestObserver:testObserver];
[customXCTestRunnerSuite runTest];
if (testObserver.testsFailed > 0) {
exitResult = 1;
}
return exitResult;
}
}
void CustomXCTestRunnerPrintAllTests(char *output) {
// printf("Running CustomXCTestRunnerPrintAllTests\n");
Class xcTestCaseClass = NSClassFromString(@"XCTestCase");
NSCAssert(xcTestCaseClass, nil);
NSMutableArray <NSString *> *tests = [NSMutableArray new];
ObjCEnumerateRuntimeClasses(^(__unsafe_unretained Class runtimeClass) {
if (class_getSuperclass(runtimeClass) != xcTestCaseClass) {
return;
}
XCTestSuite *suite = [XCTestSuite testSuiteForTestCaseClass:runtimeClass];
for (XCTest *xcTest in suite.tests) {
NSString *testName = getTestName(xcTest.name);
if (testName == nil) {
continue;
}
NSString *test = [NSString stringWithFormat:@"%@.%@", xcTest.className, testName];
[tests addObject:test];
}
});
NSString *allTestsString = [tests componentsJoinedByString:@","];
sprintf(output, "%s", allTestsString.UTF8String);
}