aboutsummaryrefslogtreecommitdiff
path: root/UnitTesting/GTMFoundationUnitTestingUtilities.m
diff options
context:
space:
mode:
authorGravatar gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2010-04-15 17:31:09 +0000
committerGravatar gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2010-04-15 17:31:09 +0000
commit2ddb245fac99802e63742c21f7da0d499e9cecf6 (patch)
tree3eebbf1aeed8269c8b14de81e8b680e784b8709b /UnitTesting/GTMFoundationUnitTestingUtilities.m
parent30ac0ff87b594d7bca3a5cc2789ca415840ea0b1 (diff)
[Author: dmaclach]
refactor GTMUnitTestingUtilities into GTMAppKitUnitTestingUtilties and GTMFoundationUnitTestingUtilities. convert GTMSignalHandler over to using new runloop routines fix bug in GTMSignalHandler API with GC where releasing it wasn't sufficient to stop it listening. R=thomasvl DELTA=1227 (638 added, 566 deleted, 23 changed)
Diffstat (limited to 'UnitTesting/GTMFoundationUnitTestingUtilities.m')
-rw-r--r--UnitTesting/GTMFoundationUnitTestingUtilities.m88
1 files changed, 88 insertions, 0 deletions
diff --git a/UnitTesting/GTMFoundationUnitTestingUtilities.m b/UnitTesting/GTMFoundationUnitTestingUtilities.m
new file mode 100644
index 0000000..ab1f29b
--- /dev/null
+++ b/UnitTesting/GTMFoundationUnitTestingUtilities.m
@@ -0,0 +1,88 @@
+//
+// GTMFoundationUnitTestingUtilities.m
+//
+// Copyright 2006-2010 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy
+// of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+//
+
+#import "GTMFoundationUnitTestingUtilities.h"
+
+@implementation GTMFoundationUnitTestingUtilities
+
+// Returns YES if we are currently being unittested.
++ (BOOL)areWeBeingUnitTested {
+ BOOL answer = NO;
+
+ // Check to see if the SenTestProbe class is linked in before we call it.
+ Class SenTestProbeClass = NSClassFromString(@"SenTestProbe");
+ if (SenTestProbeClass != Nil) {
+ // Doing this little dance so we don't actually have to link
+ // SenTestingKit in
+ SEL selector = NSSelectorFromString(@"isTesting");
+ NSMethodSignature *sig = [SenTestProbeClass methodSignatureForSelector:selector];
+ NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
+ [invocation setSelector:selector];
+ [invocation invokeWithTarget:SenTestProbeClass];
+ [invocation getReturnValue:&answer];
+ }
+ return answer;
+}
+
+@end
+
+@implementation GTMUnitTestingBooleanRunLoopContext
+
++ (id)context {
+ return [[[GTMUnitTestingBooleanRunLoopContext alloc] init] autorelease];
+}
+
+- (BOOL)shouldStop {
+ return shouldStop_;
+}
+
+- (void)setShouldStop:(BOOL)stop {
+ shouldStop_ = stop;
+}
+
+@end
+
+@implementation NSRunLoop (GTMUnitTestingAdditions)
+
+- (BOOL)gtm_runUpToSixtySecondsWithContext:(id<GTMUnitTestingRunLoopContext>)context {
+ return [self gtm_runUntilDate:[NSDate dateWithTimeIntervalSinceNow:60]
+ context:context];
+}
+
+- (BOOL)gtm_runUntilDate:(NSDate *)date
+ context:(id<GTMUnitTestingRunLoopContext>)context {
+ return [self gtm_runUntilDate:date mode:NSDefaultRunLoopMode context:context];
+}
+
+- (BOOL)gtm_runUntilDate:(NSDate *)date
+ mode:(NSString *)mode
+ context:(id<GTMUnitTestingRunLoopContext>)context {
+ BOOL contextShouldStop = NO;
+ NSRunLoop *rl = [NSRunLoop currentRunLoop];
+ while (1) {
+ contextShouldStop = [context shouldStop];
+ if (contextShouldStop) break;
+ NSDate* next = [[NSDate alloc] initWithTimeIntervalSinceNow:0.01];
+ if (!([rl runMode:mode beforeDate:next])) break;
+ if ([next compare:date] == NSOrderedDescending) break;
+ [next release];
+ }
+ return contextShouldStop;
+}
+
+@end