* MYTask: Added -commandLine, overhauled logging.
* MYDirectoryWatcher: Overhauled logging.
* MYErrorUtils: Disabled use of Security API (so it'll build without linking against Security.framework.)
5 // Created by Jens Alfke on 1/5/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
10 #import "ExceptionUtils.h"
15 BOOL gRunningTestCase;
17 struct TestCaseLink *gAllTestCases;
18 static int sPassed, sFailed;
19 static int sCurTestCaseExceptions;
22 static void TestCaseExceptionReporter( NSException *x ) {
23 sCurTestCaseExceptions++;
25 Log(@"XXX FAILED test case -- backtrace:\n%@\n\n", x.my_callStack);
28 static BOOL RunTestCase( struct TestCaseLink *test )
30 BOOL oldLogging = EnableLog(YES);
31 gRunningTestCase = YES;
33 NSAutoreleasePool *pool = [NSAutoreleasePool new];
34 Log(@"=== Testing %s ...",test->name);
36 sCurTestCaseExceptions = 0;
37 MYSetExceptionReporter(&TestCaseExceptionReporter);
39 test->testptr(); //SHAZAM!
41 if( sCurTestCaseExceptions == 0 ) {
42 Log(@"√√√ %s passed\n\n",test->name);
46 Log(@"XXX FAILED test case '%s' due to %i exception(s) already reported above",
47 test->name,sCurTestCaseExceptions);
50 }@catch( NSException *x ) {
51 if( [x.name isEqualToString: @"TestCaseSkipped"] )
52 Log(@"... skipping test %s since %@\n\n", test->name, x.reason);
55 Log(@"XXX FAILED test case '%s' due to:\nException: %@\n%@\n\n",
56 test->name,x,x.my_callStack);
61 test->testptr = NULL; // prevents test from being run again
64 gRunningTestCase = NO;
65 EnableLog(oldLogging);
70 static BOOL RunTestCaseNamed( const char *name )
72 for( struct TestCaseLink *test = gAllTestCases; test; test=test->next )
73 if( strcmp(name,test->name)==0 ) {
74 return RunTestCase(test);
76 Log(@"... WARNING: Could not find test case named '%s'\n\n",name);
81 void _RequireTestCase( const char *name )
83 if( ! RunTestCaseNamed(name) ) {
84 [NSException raise: @"TestCaseSkipped"
85 format: @"prerequisite %s failed", name];
90 void RunTestCases( int argc, const char **argv )
92 sPassed = sFailed = 0;
93 BOOL stopAfterTests = NO;
94 for( int i=1; i<argc; i++ ) {
95 const char *arg = argv[i];
96 if( strncmp(arg,"Test_",5)==0 ) {
98 if( strcmp(arg,"Only")==0 )
100 else if( strcmp(arg,"All") == 0 ) {
101 for( struct TestCaseLink *link = gAllTestCases; link; link=link->next )
104 RunTestCaseNamed(arg);
108 if( sPassed>0 || sFailed>0 || stopAfterTests ) {
110 AlwaysLog(@"√√√√√√ ALL %i TESTS PASSED √√√√√√", sPassed);
112 Warn(@"****** %i TESTS FAILED, %i PASSED ******", sFailed,sPassed);
115 if( stopAfterTests ) {
116 Log(@"Stopping after tests ('Test_Only' arg detected)");
127 #pragma mark ASSERTION FAILURE HANDLER:
130 void _AssertFailed( id rcvr, const void *selOrFn, const char *sourceFile, int sourceLine,
131 const char *condString, NSString *message, ... )
135 va_start(args,message);
136 message = [[[NSString alloc] initWithFormat: message arguments: args] autorelease];
137 message = [@"Assertion failed: " stringByAppendingString: message];
140 message = [NSString stringWithUTF8String: condString];
143 [[NSAssertionHandler currentHandler] handleFailureInMethod: (SEL)selOrFn
145 file: [NSString stringWithUTF8String: sourceFile]
146 lineNumber: sourceLine
147 description: @"%@", message];
149 [[NSAssertionHandler currentHandler] handleFailureInFunction: [NSString stringWithUTF8String:selOrFn]
150 file: [NSString stringWithUTF8String: sourceFile]
151 lineNumber: sourceLine
152 description: @"%@", message];
153 abort(); // unreachable, but appeases compiler
157 void _AssertAbstractMethodFailed( id rcvr, SEL cmd)
159 [NSException raise: NSInternalInconsistencyException
160 format: @"Class %@ forgot to implement abstract method %@",
161 [rcvr class], NSStringFromSelector(cmd)];
162 abort(); // unreachable, but appeases compiler
167 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
169 Redistribution and use in source and binary forms, with or without modification, are permitted
170 provided that the following conditions are met:
172 * Redistributions of source code must retain the above copyright notice, this list of conditions
173 and the following disclaimer.
174 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
175 and the following disclaimer in the documentation and/or other materials provided with the
178 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
179 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
180 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
181 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
182 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
183 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
184 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
185 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.