PageRenderTime 60ms CodeModel.GetById 11ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 0ms

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