This repository has been archived by the owner on Jun 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 309
/
GHTestSuite.m
167 lines (144 loc) · 5.93 KB
/
GHTestSuite.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
//
// GHTestSuite.m
// GHUnit
//
// Created by Gabriel Handford on 1/25/09.
// Copyright 2009. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
//! @cond DEV
#import "GHTestSuite.h"
#import "GHTesting.h"
#import "GHTestGroup+JUnitXML.h"
NSString *GHUnitTest = NULL;
@interface GHTestSuite (CLIDisabled)
- (BOOL)isCLIDisabled;
@end
@implementation GHTestSuite
- (id)initWithName:(NSString *)name testCases:(NSArray *)testCases delegate:(id<GHTestDelegate>)delegate {
if ((self = [super initWithName:name delegate:delegate])) {
for(id testCase in testCases) {
[self addTestCase:testCase];
}
}
return self;
}
+ (GHTestSuite *)allTests {
NSArray *testCases = [[GHTesting sharedInstance] loadAllTestCases];
GHTestSuite *allTests = [[self alloc] initWithName:@"Tests" testCases:nil delegate:nil];
for(id testCase in testCases) {
// Ignore test cases that can't be run at the command line
if (!([testCase respondsToSelector:@selector(isCLIDisabled)] && [testCase isCLIDisabled] && getenv("GHUNIT_CLI"))) [allTests addTestCase:testCase];
}
return allTests;
}
+ (GHTestSuite *)suiteWithTestCaseClass:(Class)testCaseClass method:(SEL)method {
NSString *name = [NSString stringWithFormat:@"%@/%@", NSStringFromClass(testCaseClass), NSStringFromSelector(method)];
GHTestSuite *testSuite = [[GHTestSuite alloc] initWithName:name testCases:nil delegate:nil];
id testCase = [[testCaseClass alloc] init];
if (!testCase) {
NSLog(@"Couldn't instantiate test: %@", NSStringFromClass(testCaseClass));
return nil;
}
GHTestGroup *group = [[GHTestGroup alloc] initWithTestCase:testCase selector:method delegate:nil];
[testSuite addTestGroup:group];
return testSuite;
}
+ (GHTestSuite *)suiteWithPrefix:(NSString *)prefix options:(NSStringCompareOptions)options {
if (!prefix || [prefix isEqualToString:@""]) return [self allTests];
NSArray *testCases = [[GHTesting sharedInstance] loadAllTestCases];
NSString *name = [NSString stringWithFormat:@"Tests (%@)", prefix];
GHTestSuite *testSuite = [[self alloc] initWithName:name testCases:nil delegate:nil];
for(id testCase in testCases) {
NSString *className = NSStringFromClass([testCase class]);
if ([className compare:prefix options:options range:NSMakeRange(0, [prefix length])] == NSOrderedSame)
[testSuite addTestCase:testCase];
}
return testSuite;
}
+ (GHTestSuite *)suiteWithTestFilter:(NSString *)testFilterString {
NSArray *testFilters = [testFilterString componentsSeparatedByString:@","];
GHTestSuite *testSuite = [[GHTestSuite alloc] initWithName:testFilterString testCases:nil delegate:nil];
for(NSString *testFilter in testFilters) {
NSArray *components = [testFilter componentsSeparatedByString:@"/"];
if ([components count] == 2) {
NSString *testCaseClassName = [components objectAtIndex:0];
Class testCaseClass = NSClassFromString(testCaseClassName);
id testCase = [[testCaseClass alloc] init];
if (!testCase) {
NSLog(@"Couldn't find test: %@", testCaseClassName);
continue;
}
NSString *methodName = [components objectAtIndex:1];
GHTestGroup *group = [[GHTestGroup alloc] initWithTestCase:testCase selector:NSSelectorFromString(methodName) delegate:nil];
[testSuite addTestGroup:group];
} else {
Class testCaseClass = NSClassFromString(testFilter);
id testCase = [[testCaseClass alloc] init];
if (!testCase) {
NSLog(@"Couldn't find test: %@", testFilter);
continue;
}
[testSuite addTestCase:testCase];
}
}
return testSuite;
}
+ (GHTestSuite *)suiteFromEnv {
const char* cTestFilter = getenv("TEST");
if (cTestFilter) {
NSString *testFilter = [NSString stringWithUTF8String:cTestFilter];
return [GHTestSuite suiteWithTestFilter:testFilter];
} else {
if (GHUnitTest != NULL) return [GHTestSuite suiteWithTestFilter:GHUnitTest];
return [GHTestSuite allTests];
}
}
@end
@implementation GHTestSuite (JUnitXML)
/*
Override logic to write children individually, as we want each test group's
JUnit XML to be in its own file.
*/
- (BOOL)writeJUnitXMLToDirectory:(NSString *)directory error:(NSError **)error {
NSParameterAssert(error);
BOOL allSuccess = YES;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:directory]) {
if (![fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:error]) {
NSLog(@"Error while creating results directory: %@", [*error localizedDescription]);
return NO;
}
}
for (id child in self.children) {
if ([child respondsToSelector:@selector(writeJUnitXMLAtPath:error:)]) {
if (![child writeJUnitXMLAtPath:directory error:error]) {
NSLog(@"Error writing JUnit XML: %@", [*error localizedDescription]);
allSuccess = NO;
}
}
}
return allSuccess;
}
@end
//! @endcond