* Added BSD license to more .m files.
* Updated some copyright notices.
* Added some #imports to make the MYNetwork classes build without using the MYUtilities.pch prefix header.
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++;
24 Log(@"XXX FAILED test case -- backtrace:\n%@\n\n", x.my_callStack);
27 static BOOL RunTestCase( struct TestCaseLink *test )
29 BOOL oldLogging = EnableLog(YES);
30 gRunningTestCase = YES;
32 NSAutoreleasePool *pool = [NSAutoreleasePool new];
33 Log(@"=== Testing %s ...",test->name);
35 sCurTestCaseExceptions = 0;
36 MYSetExceptionReporter(&TestCaseExceptionReporter);
38 test->testptr(); //SHAZAM!
40 if( sCurTestCaseExceptions == 0 ) {
41 Log(@"√√√ %s passed\n\n",test->name);
45 Log(@"XXX FAILED test case '%s' due to %i exception(s) already reported above",
46 test->name,sCurTestCaseExceptions);
49 }@catch( NSException *x ) {
50 if( [x.name isEqualToString: @"TestCaseSkipped"] )
51 Log(@"... skipping test %s since %@\n\n", test->name, x.reason);
53 Log(@"XXX FAILED test case '%s' due to:\nException: %@\n%@\n\n",
54 test->name,x,x.my_callStack);
59 test->testptr = NULL; // prevents test from being run again
62 gRunningTestCase = NO;
63 EnableLog(oldLogging);
68 static BOOL RunTestCaseNamed( const char *name )
70 for( struct TestCaseLink *test = gAllTestCases; test; test=test->next )
71 if( strcmp(name,test->name)==0 ) {
72 return RunTestCase(test);
74 Log(@"... WARNING: Could not find test case named '%s'\n\n",name);
79 void _RequireTestCase( const char *name )
81 if( ! RunTestCaseNamed(name) ) {
82 [NSException raise: @"TestCaseSkipped"
83 format: @"prerequisite %s failed", name];
88 void RunTestCases( int argc, const char **argv )
90 sPassed = sFailed = 0;
91 BOOL stopAfterTests = NO;
92 for( int i=1; i<argc; i++ ) {
93 const char *arg = argv[i];
94 if( strncmp(arg,"Test_",5)==0 ) {
96 if( strcmp(arg,"Only")==0 )
98 else if( strcmp(arg,"All") == 0 ) {
99 for( struct TestCaseLink *link = gAllTestCases; link; link=link->next )
102 RunTestCaseNamed(arg);
106 if( sPassed>0 || sFailed>0 || stopAfterTests ) {
108 Log(@"√√√√√√ ALL %i TESTS PASSED √√√√√√", sPassed);
110 Log(@"****** %i TESTS FAILED, %i PASSED ******", sFailed,sPassed);
113 if( stopAfterTests ) {
114 Log(@"Stopping after tests ('Test_Only' arg detected)");
125 #pragma mark ASSERTION FAILURE HANDLER:
128 void _AssertFailed( id rcvr, const char *selOrFn, const char *sourceFile, int sourceLine,
129 const char *condString, NSString *message, ... )
133 va_start(args,message);
134 message = [[[NSString alloc] initWithFormat: message arguments: args] autorelease];
137 message = [NSString stringWithUTF8String: condString];
140 [[NSAssertionHandler currentHandler] handleFailureInMethod: (SEL)selOrFn
142 file: [NSString stringWithUTF8String: sourceFile]
143 lineNumber: sourceLine
144 description: @"%@", message];
146 [[NSAssertionHandler currentHandler] handleFailureInFunction: [NSString stringWithUTF8String:selOrFn]
147 file: [NSString stringWithUTF8String: sourceFile]
148 lineNumber: sourceLine
149 description: @"%@", message];
150 abort(); // unreachable, but appeases compiler
155 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
157 Redistribution and use in source and binary forms, with or without modification, are permitted
158 provided that the following conditions are met:
160 * Redistributions of source code must retain the above copyright notice, this list of conditions
161 and the following disclaimer.
162 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
163 and the following disclaimer in the documentation and/or other materials provided with the
166 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
167 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
168 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
169 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
170 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
171 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
172 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
173 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.