5 // Created by Wincent Colaiuta on 12 March 2005.
7 // Copyright 2004-2007 Wincent Colaiuta.
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #import "WOTestRunner.h"
26 #import <Foundation/Foundation.h>
27 #import <objc/objc-runtime.h>
33 // make what(1) produce meaningful output
34 #import "WOTestRunner_Version.h"
37 #pragma mark Implementation
39 int main(int argc, const char *argv[])
41 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
42 int exitCode = EXIT_SUCCESS;
44 // an example of using the framework without linking to it
45 WO_TEST_LOAD_FRAMEWORK;
46 if (!WO_TEST_FRAMEWORK_IS_LOADED)
48 exitCode = EXIT_FAILURE;
49 fprintf(stderr, "error: unable to load WOTest framework\n");
53 // parse commandline arguments
55 NSMutableArray *testClasses = [NSMutableArray array];
56 NSMutableArray *excludeClasses = [NSMutableArray array];
57 NSMutableArray *testBundles = [NSMutableArray array];
58 NSMutableArray *excludeBundles = [NSMutableArray array];
63 static struct option longopts[] = {
64 { "help", no_argument, NULL, 'h' },
65 { "verbose", no_argument, NULL, 'v' },
66 { "version", no_argument, NULL, 'V' },
67 { "test-class", required_argument, NULL, 't' },
68 { "exclude-class", required_argument, NULL, 'e' },
69 { "test-bundle", required_argument, NULL, 'b' },
70 { "exclude-bundle", required_argument, NULL, 'x' }
72 while ((ch = getopt_long(argc, (char * const *)argv, "hvVt:e:b:", longopts, NULL)) != -1)
76 case 'h': // show help
80 case 'v': // be verbose
83 case 'V': // show version information
87 case 't': // test this class
88 [testClasses addObject:[NSString stringWithUTF8String:optarg]];
90 case 'e': // exclude this class
91 [excludeClasses addObject:[NSString stringWithUTF8String:optarg]];
93 case 'b': // test this bundle (loading into memory if necessary)
94 [testBundles addObject:[NSString stringWithUTF8String:optarg]];
96 case 'x': // exclude this bundle
97 [excludeBundles addObject:[NSString stringWithUTF8String:optarg]];
101 exitCode = EXIT_FAILURE;
106 // TODO: automatically modify DYLD_FRAMEWORK_PATH based on passed-in bundles, restore to previous setting on exit
108 // - save DYLD_FRAMEWORK_PATH
109 // - for each absolute bundle path, is bundle path already in DYLD_FRAMEWORK_PATH? if not add it
110 // - at end, restore DYLD_FRAMEWORK_PATH
111 // more sophisticated algorithm:
112 // - inspect mach-o headers to see which libraries it wants to load; if they're non-standard locations, add them
116 Example error when trying to run WOCommon tests without first setting DYLD_FRAMEWORK_PATH
118 2006-10-23 01:19:51.727 WOTestRunner[892] *** -[NSBundle load]: Error loading code /Users/wincent/trabajo/build/Debug/WOCommon.bundle/Contents/MacOS/WOCommon for bundle /Users/wincent/trabajo/build/Debug/WOCommon.bundle, error code 4 (link edit error code 4, error number 0 (Library not loaded: @executable_path/../Frameworks/WOTest.framework/Versions/A/WOTest
119 warning: could not load bundle /Users/wincent/trabajo/build/Debug/WOCommon.bundle
121 of course, may be more complicated than all that. would be nice to handle case where WOTestRunner, WOTest.framework and the bundle being tested were all in completely unrelated locations
125 if ([testBundles count] > 0) // test only these bundles
127 for (NSString *bundlePath in testBundles)
129 bundlePath = [bundlePath WOTest_stringByConvertingToAbsolutePath];
130 NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
131 if (bundle && [bundle load])
133 if ([testClasses count] > 0) // test only these classes
135 for (NSString *class in testClasses)
136 [WO_TEST_SHARED_INSTANCE runTestsForClassName:class];
138 else // test all classes
140 NSArray *classes = [WO_TEST_SHARED_INSTANCE testableClassesFrom:bundle];
141 for (NSString *class in classes)
143 if ([excludeClasses containsObject:class]) continue;
144 [WO_TEST_SHARED_INSTANCE runTestsForClassName:class];
149 fprintf(stderr, "warning: could not load bundle %s\n", [bundlePath UTF8String]);
152 else // test all bundles
154 if ([testClasses count] > 0) // test only these classes
156 for (NSString *class in testClasses)
157 [WO_TEST_SHARED_INSTANCE runTestsForClassName:class];
159 else // test all classes
161 for (NSString *class in [WO_TEST_SHARED_INSTANCE testableClasses])
163 if ([excludeClasses containsObject:class]) continue;
164 [WO_TEST_SHARED_INSTANCE runTestsForClassName:class];
169 [WO_TEST_SHARED_INSTANCE printTestResultsSummary];
170 if (![WO_TEST_SHARED_INSTANCE testsWereSuccessful])
171 exitCode = EXIT_FAILURE;
178 void showUsage(const char *name)
182 //------------------------------- 80 columns ----------------------------------->|
183 "WOTestRunner, part of the WOTest framework <http://test.wincent.com/>.\n"
186 "This program is free software: you can redistribute it and/or modify\n"
187 "it under the terms of the GNU General Public License as published by\n"
188 "the Free Software Foundation, either version 3 of the License, or\n"
189 "(at your option) any later version.\n"
191 "This program is distributed in the hope that it will be useful,\n"
192 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
193 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
194 "GNU General Public License for more details.\n"
196 "You should have received a copy of the GNU General Public License\n"
197 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n"
199 "Usage: %s [option]...\n"
200 "-t, --test-class=CLASS test only CLASS\n"
201 "-e, --exclude-class=CLASS test all but CLASS\n"
202 "-b, --test-bundle=BUNDLE test only BUNDLE, loading if necessary\n"
203 "-x, --exclude-bundle=BUNDLE test all but BUNDLE\n"
204 "-v, --verbose verbose output (repeat for more verbosity)\n"
205 "-V, --version show version information\n"
206 "-h, --help show this usage information\n",
207 WO_RCSID_STRING(copyright), name);
210 void showVersion(void)
214 //------------------------------- 80 columns ----------------------------------->|
215 "WOTestRunner, part of the WOTest framework <http://test.wincent.com/>.\n"
218 "This program is free software: you can redistribute it and/or modify\n"
219 "it under the terms of the GNU General Public License as published by\n"
220 "the Free Software Foundation, either version 3 of the License, or\n"
221 "(at your option) any later version.\n"
223 "This program is distributed in the hope that it will be useful,\n"
224 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
225 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
226 "GNU General Public License for more details.\n"
228 "You should have received a copy of the GNU General Public License\n"
229 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n"
231 "%s\n", WO_RCSID_STRING(copyright), WO_RCSID_STRING(version));