PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Test.m

https://bitbucket.org/tgaul/myutilities
Objective C | 202 lines | 146 code | 28 blank | 28 comment | 26 complexity | 3f6c9f2033d50a17f60da1d1958debbf 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. #import "ExceptionUtils.h"
  10. #if DEBUG
  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. if( rcvr )
  135. [[NSAssertionHandler currentHandler] handleFailureInMethod: (SEL)selOrFn
  136. object: rcvr
  137. file: [NSString stringWithUTF8String: sourceFile]
  138. lineNumber: sourceLine
  139. description: @"%@", message];
  140. else
  141. [[NSAssertionHandler currentHandler] handleFailureInFunction: [NSString stringWithUTF8String:selOrFn]
  142. file: [NSString stringWithUTF8String: sourceFile]
  143. lineNumber: sourceLine
  144. description: @"%@", message];
  145. abort(); // unreachable, but appeases compiler
  146. }
  147. void _AssertAbstractMethodFailed( id rcvr, SEL cmd)
  148. {
  149. [NSException raise: NSInternalInconsistencyException
  150. format: @"Class %@ forgot to implement abstract method %@",
  151. [rcvr class], NSStringFromSelector(cmd)];
  152. abort(); // unreachable, but appeases compiler
  153. }
  154. /*
  155. Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
  156. Redistribution and use in source and binary forms, with or without modification, are permitted
  157. provided that the following conditions are met:
  158. * Redistributions of source code must retain the above copyright notice, this list of conditions
  159. and the following disclaimer.
  160. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
  161. and the following disclaimer in the documentation and/or other materials provided with the
  162. distribution.
  163. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  164. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  165. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
  166. BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  167. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  168. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  169. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  170. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  171. */