/core/externals/google-toolbox-for-mac/UnitTesting/GTMFoundationUnitTestingUtilities.m

http://macfuse.googlecode.com/ · Objective C · 119 lines · 77 code · 20 blank · 22 comment · 7 complexity · a9e5864501018f79d26338807fef5859 MD5 · raw file

  1. //
  2. // GTMFoundationUnitTestingUtilities.m
  3. //
  4. // Copyright 2006-2010 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 <unistd.h>
  19. #import "GTMFoundationUnitTestingUtilities.h"
  20. @implementation GTMFoundationUnitTestingUtilities
  21. // Returns YES if we are currently being unittested.
  22. + (BOOL)areWeBeingUnitTested {
  23. BOOL answer = NO;
  24. // Check to see if the SenTestProbe class is linked in before we call it.
  25. Class SenTestProbeClass = NSClassFromString(@"SenTestProbe");
  26. if (SenTestProbeClass != Nil) {
  27. // Doing this little dance so we don't actually have to link
  28. // SenTestingKit in
  29. SEL selector = NSSelectorFromString(@"isTesting");
  30. NSMethodSignature *sig = [SenTestProbeClass methodSignatureForSelector:selector];
  31. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
  32. [invocation setSelector:selector];
  33. [invocation invokeWithTarget:SenTestProbeClass];
  34. [invocation getReturnValue:&answer];
  35. }
  36. return answer;
  37. }
  38. + (void)maxRuntimeTimer:(NSTimer*)timer {
  39. // Directly use fprintf so the message always shows up.
  40. fprintf(stderr, "%s:%d: error: %s : Hit app testing timeout!\n",
  41. __FILE__, __LINE__, __func__);
  42. fflush(stderr);
  43. exit(1);
  44. }
  45. + (void)installTestingTimeout:(NSTimeInterval)maxRunInterval {
  46. [NSTimer scheduledTimerWithTimeInterval:maxRunInterval
  47. target:self
  48. selector:@selector(maxRuntimeTimer:)
  49. userInfo:nil
  50. repeats:NO];
  51. }
  52. @end
  53. @implementation GTMUnitTestingBooleanRunLoopContext
  54. + (id)context {
  55. return [[[GTMUnitTestingBooleanRunLoopContext alloc] init] autorelease];
  56. }
  57. - (BOOL)shouldStop {
  58. return shouldStop_;
  59. }
  60. - (void)setShouldStop:(BOOL)stop {
  61. shouldStop_ = stop;
  62. }
  63. - (void)reset {
  64. shouldStop_ = NO;
  65. }
  66. @end
  67. @implementation NSRunLoop (GTMUnitTestingAdditions)
  68. - (BOOL)gtm_runUpToNSeconds:(NSTimeInterval)seconds
  69. context:(id<GTMUnitTestingRunLoopContext>)context {
  70. return [self gtm_runUntilDate:[NSDate dateWithTimeIntervalSinceNow:seconds]
  71. context:context];
  72. }
  73. - (BOOL)gtm_runUpToSixtySecondsWithContext:(id<GTMUnitTestingRunLoopContext>)context {
  74. return [self gtm_runUntilDate:[NSDate dateWithTimeIntervalSinceNow:60]
  75. context:context];
  76. }
  77. - (BOOL)gtm_runUntilDate:(NSDate *)date
  78. context:(id<GTMUnitTestingRunLoopContext>)context {
  79. return [self gtm_runUntilDate:date mode:NSDefaultRunLoopMode context:context];
  80. }
  81. - (BOOL)gtm_runUntilDate:(NSDate *)date
  82. mode:(NSString *)mode
  83. context:(id<GTMUnitTestingRunLoopContext>)context {
  84. BOOL contextShouldStop = NO;
  85. NSRunLoop *rl = [NSRunLoop currentRunLoop];
  86. NSDate* next = nil;
  87. while (1) {
  88. contextShouldStop = [context shouldStop];
  89. if (contextShouldStop) break;
  90. next = [[NSDate alloc] initWithTimeIntervalSinceNow:0.01];
  91. if (!([rl runMode:mode beforeDate:next])) break;
  92. if ([next compare:date] == NSOrderedDescending) break;
  93. [next release];
  94. next = nil;
  95. }
  96. [next release];
  97. return contextShouldStop;
  98. }
  99. @end