PageRenderTime 36ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/Test.m

https://bitbucket.org/coradine/third-party-myutilities
Objective C | 204 lines | 147 code | 29 blank | 28 comment | 26 complexity | 3464f5f085ef6dfd1aae3b817ae59820 MD5 | raw file
  1. //
  2. // Test.m
  3. // MYUtilities
  4. //
  5. // Created by Jens Alfke on 1/5/08.
  6. // Copyright 2008 Jens Alfke. All rights reserved.
  7. //
  8. #import "Test.h"
  9. #if DEBUG
  10. #import "ExceptionUtils.h"
  11. BOOL gRunningTestCase;
  12. struct TestCaseLink *gAllTestCases;
  13. static int sPassed, sFailed;
  14. static NSMutableArray* sFailedTestNames;
  15. static int sCurTestCaseExceptions;
  16. static void TestCaseExceptionReporter( NSException *x ) {
  17. sCurTestCaseExceptions++;
  18. fflush(stderr);
  19. Log(@"XXX FAILED test case -- backtrace:\n%@\n\n", x.my_callStack);
  20. }
  21. static void RecordFailedTest( struct TestCaseLink *test ) {
  22. if (!sFailedTestNames)
  23. sFailedTestNames = [[NSMutableArray alloc] init];
  24. [sFailedTestNames addObject: [NSString stringWithUTF8String: test->name]];
  25. }
  26. static BOOL RunTestCase( struct TestCaseLink *test )
  27. {
  28. BOOL oldLogging = EnableLog(YES);
  29. gRunningTestCase = YES;
  30. if( test->testptr ) {
  31. NSAutoreleasePool *pool = [NSAutoreleasePool new];
  32. Log(@"=== Testing %s ...",test->name);
  33. @try{
  34. sCurTestCaseExceptions = 0;
  35. MYSetExceptionReporter(&TestCaseExceptionReporter);
  36. test->testptr(); //SHAZAM!
  37. if( sCurTestCaseExceptions == 0 ) {
  38. Log(@"√√√ %s passed\n\n",test->name);
  39. test->passed = YES;
  40. sPassed++;
  41. } else {
  42. Log(@"XXX FAILED test case '%s' due to %i exception(s) already reported above",
  43. test->name,sCurTestCaseExceptions);
  44. sFailed++;
  45. RecordFailedTest(test);
  46. }
  47. }@catch( NSException *x ) {
  48. if( [x.name isEqualToString: @"TestCaseSkipped"] )
  49. Log(@"... skipping test %s since %@\n\n", test->name, x.reason);
  50. else {
  51. fflush(stderr);
  52. Log(@"XXX FAILED test case '%s' due to:\nException: %@\n%@\n\n",
  53. test->name,x,x.my_callStack);
  54. sFailed++;
  55. RecordFailedTest(test);
  56. }
  57. }@finally{
  58. [pool drain];
  59. test->testptr = NULL; // prevents test from being run again
  60. }
  61. }
  62. gRunningTestCase = NO;
  63. EnableLog(oldLogging);
  64. return test->passed;
  65. }
  66. static BOOL RunTestCaseNamed( const char *name )
  67. {
  68. for( struct TestCaseLink *test = gAllTestCases; test; test=test->next )
  69. if( strcmp(name,test->name)==0 ) {
  70. return RunTestCase(test);
  71. }
  72. Log(@"... WARNING: Could not find test case named '%s'\n\n",name);
  73. return NO;
  74. }
  75. void _RequireTestCase( const char *name )
  76. {
  77. if( ! RunTestCaseNamed(name) ) {
  78. [NSException raise: @"TestCaseSkipped"
  79. format: @"prerequisite %s failed", name];
  80. }
  81. }
  82. void RunTestCases( int argc, const char **argv )
  83. {
  84. sPassed = sFailed = 0;
  85. sFailedTestNames = nil;
  86. BOOL stopAfterTests = NO;
  87. for( int i=1; i<argc; i++ ) {
  88. const char *arg = argv[i];
  89. if( strncmp(arg,"Test_",5)==0 ) {
  90. arg += 5;
  91. if( strcmp(arg,"Only")==0 )
  92. stopAfterTests = YES;
  93. else if( strcmp(arg,"All") == 0 ) {
  94. for( struct TestCaseLink *link = gAllTestCases; link; link=link->next )
  95. RunTestCase(link);
  96. } else {
  97. RunTestCaseNamed(arg);
  98. }
  99. }
  100. }
  101. if( sPassed>0 || sFailed>0 || stopAfterTests ) {
  102. NSAutoreleasePool *pool = [NSAutoreleasePool new];
  103. if( sFailed==0 )
  104. AlwaysLog(@"√√√√√√ ALL %i TESTS PASSED √√√√√√", sPassed);
  105. else {
  106. Warn(@"****** %i of %i TESTS FAILED: %@ ******",
  107. sFailed, sPassed+sFailed,
  108. [sFailedTestNames componentsJoinedByString: @", "]);
  109. exit(1);
  110. }
  111. if( stopAfterTests ) {
  112. Log(@"Stopping after tests ('Test_Only' arg detected)");
  113. exit(0);
  114. }
  115. [pool drain];
  116. }
  117. [sFailedTestNames release];
  118. sFailedTestNames = nil;
  119. }
  120. #endif // DEBUG
  121. #pragma mark -
  122. #pragma mark ASSERTION FAILURE HANDLER:
  123. void _AssertFailed( id rcvr, const void *selOrFn, const char *sourceFile, int sourceLine,
  124. const char *condString, NSString *message, ... )
  125. {
  126. if( message ) {
  127. va_list args;
  128. va_start(args,message);
  129. message = [[[NSString alloc] initWithFormat: message arguments: args] autorelease];
  130. message = [@"Assertion failed: " stringByAppendingString: message];
  131. va_end(args);
  132. } else
  133. message = [NSString stringWithUTF8String: condString];
  134. Log(@"*** ASSERTION FAILED: %@ ... NOT!", message);
  135. if( rcvr )
  136. [[NSAssertionHandler currentHandler] handleFailureInMethod: (SEL)selOrFn
  137. object: rcvr
  138. file: [NSString stringWithUTF8String: sourceFile]
  139. lineNumber: sourceLine
  140. description: @"%@", message];
  141. else
  142. [[NSAssertionHandler currentHandler] handleFailureInFunction: [NSString stringWithUTF8String:selOrFn]
  143. file: [NSString stringWithUTF8String: sourceFile]
  144. lineNumber: sourceLine
  145. description: @"%@", message];
  146. abort(); // unreachable, but appeases compiler
  147. }
  148. void _AssertAbstractMethodFailed( id rcvr, SEL cmd)
  149. {
  150. [NSException raise: NSInternalInconsistencyException
  151. format: @"Class %@ forgot to implement abstract method %@",
  152. [rcvr class], NSStringFromSelector(cmd)];
  153. abort(); // unreachable, but appeases compiler
  154. }
  155. /*
  156. 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:
  159. * Redistributions of source code must retain the above copyright notice, this list of conditions
  160. and the following disclaimer.
  161. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
  162. and the following disclaimer in the documentation and/or other materials provided with the
  163. distribution.
  164. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  165. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  166. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
  167. BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  168. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  169. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  170. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  171. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  172. */