PageRenderTime 9ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/MapView/GTM/GTMIPhoneUnitTestDelegate.m

http://github.com/route-me/route-me
Objective C | 165 lines | 128 code | 10 blank | 27 comment | 10 complexity | 43f137ba6c30598bb31effd48acf54e8 MD5 | raw file
  1. //
  2. // GTMIPhoneUnitTestDelegate.m
  3. //
  4. // Copyright 2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import "GTMIPhoneUnitTestDelegate.h"
  19. #import "GTMDefines.h"
  20. #if !GTM_IPHONE_SDK
  21. #error GTMIPhoneUnitTestDelegate for iPhone only
  22. #endif
  23. #import <objc/runtime.h>
  24. #import <stdio.h>
  25. #import <UIKit/UIKit.h>
  26. #import "GTMSenTestCase.h"
  27. @implementation GTMIPhoneUnitTestDelegate
  28. // Run through all the registered classes and run test methods on any
  29. // that are subclasses of SenTestCase. Terminate the application upon
  30. // test completion.
  31. - (void)applicationDidFinishLaunching:(UIApplication *)application {
  32. [self runTests];
  33. if (!getenv("GTM_DISABLE_TERMINATION")) {
  34. // To help using xcodebuild, make the exit status 0/1 to signal the tests
  35. // success/failure.
  36. int exitStatus = (([self totalFailures] == 0U) ? 0 : 1);
  37. // Alternative to exit(status); so it cleanly terminates the UIApplication
  38. // and classes that depend on this signal to exit cleanly.
  39. if ([application respondsToSelector:@selector(_terminateWithStatus:)]) {
  40. [application performSelector:@selector(_terminateWithStatus:)
  41. withObject:(id)exitStatus];
  42. } else {
  43. exit(exitStatus);
  44. }
  45. }
  46. }
  47. // Run through all the registered classes and run test methods on any
  48. // that are subclasses of SenTestCase. Print results and run time to
  49. // the default output.
  50. - (void)runTests {
  51. int count = objc_getClassList(NULL, 0);
  52. NSMutableData *classData
  53. = [NSMutableData dataWithLength:sizeof(Class) * count];
  54. Class *classes = (Class*)[classData mutableBytes];
  55. _GTMDevAssert(classes, @"Couldn't allocate class list");
  56. objc_getClassList(classes, count);
  57. totalFailures_ = 0;
  58. totalSuccesses_ = 0;
  59. NSString *suiteName = [[NSBundle mainBundle] bundlePath];
  60. NSDate *suiteStartDate = [NSDate date];
  61. NSString *suiteStartString
  62. = [NSString stringWithFormat:@"Test Suite '%@' started at %@\n",
  63. suiteName, suiteStartDate];
  64. fputs([suiteStartString UTF8String], stderr);
  65. fflush(stderr);
  66. for (int i = 0; i < count; ++i) {
  67. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  68. Class currClass = classes[i];
  69. if (class_respondsToSelector(currClass, @selector(conformsToProtocol:)) &&
  70. [currClass conformsToProtocol:@protocol(SenTestCase)]) {
  71. NSDate *fixtureStartDate = [NSDate date];
  72. NSString *fixtureName = NSStringFromClass(currClass);
  73. NSString *fixtureStartString
  74. = [NSString stringWithFormat:@"Test Suite '%@' started at %@\n",
  75. fixtureName, fixtureStartDate];
  76. int fixtureSuccesses = 0;
  77. int fixtureFailures = 0;
  78. fputs([fixtureStartString UTF8String], stderr);
  79. fflush(stderr);
  80. NSArray *invocations = [currClass testInvocations];
  81. if ([invocations count]) {
  82. NSInvocation *invocation;
  83. GTM_FOREACH_OBJECT(invocation, invocations) {
  84. GTMTestCase *testCase
  85. = [[currClass alloc] initWithInvocation:invocation];
  86. BOOL failed = NO;
  87. NSDate *caseStartDate = [NSDate date];
  88. NSString *selectorName = NSStringFromSelector([invocation selector]);
  89. NSString *caseStartString
  90. = [NSString stringWithFormat:@"Test Case '-[%@ %@]' started.\n",
  91. fixtureName, selectorName];
  92. fputs([caseStartString UTF8String], stderr);
  93. fflush(stderr);
  94. @try {
  95. [testCase performTest];
  96. } @catch (NSException *exception) {
  97. failed = YES;
  98. }
  99. if (failed) {
  100. fixtureFailures += 1;
  101. } else {
  102. fixtureSuccesses += 1;
  103. }
  104. NSTimeInterval caseEndTime
  105. = [[NSDate date] timeIntervalSinceDate:caseStartDate];
  106. NSString *caseEndString
  107. = [NSString stringWithFormat:@"Test Case '-[%@ %@]' %@ (%0.3f "
  108. @"seconds).\n",
  109. fixtureName, selectorName,
  110. failed ? @"failed" : @"passed",
  111. caseEndTime];
  112. fputs([caseEndString UTF8String], stderr);
  113. fflush(stderr);
  114. [testCase release];
  115. }
  116. }
  117. NSDate *fixtureEndDate = [NSDate date];
  118. NSTimeInterval fixtureEndTime
  119. = [fixtureEndDate timeIntervalSinceDate:fixtureStartDate];
  120. NSString *fixtureEndString
  121. = [NSString stringWithFormat:@"Test Suite '%@' finished at %@.\n"
  122. @"Executed %d tests, with %d failures (%d "
  123. @"unexpected) in %0.3f (%0.3f) seconds\n\n",
  124. fixtureName, fixtureEndDate,
  125. fixtureSuccesses + fixtureFailures,
  126. fixtureFailures, fixtureFailures,
  127. fixtureEndTime, fixtureEndTime];
  128. fputs([fixtureEndString UTF8String], stderr);
  129. fflush(stderr);
  130. totalSuccesses_ += fixtureSuccesses;
  131. totalFailures_ += fixtureFailures;
  132. }
  133. [pool release];
  134. }
  135. NSDate *suiteEndDate = [NSDate date];
  136. NSTimeInterval suiteEndTime
  137. = [suiteEndDate timeIntervalSinceDate:suiteStartDate];
  138. NSString *suiteEndString
  139. = [NSString stringWithFormat:@"Test Suite '%@' finished at %@.\n"
  140. @"Executed %d tests, with %d failures (%d "
  141. @"unexpected) in %0.3f (%0.3f) seconds\n\n",
  142. suiteName, suiteEndDate,
  143. totalSuccesses_ + totalFailures_,
  144. totalFailures_, totalFailures_,
  145. suiteEndTime, suiteEndTime];
  146. fputs([suiteEndString UTF8String], stderr);
  147. fflush(stderr);
  148. }
  149. - (NSUInteger)totalSuccesses {
  150. return totalSuccesses_;
  151. }
  152. - (NSUInteger)totalFailures {
  153. return totalFailures_;
  154. }
  155. @end