From b3cb1bec910d1c64adcf02843a1ade83c49ca53f Mon Sep 17 00:00:00 2001 From: Gregory Schlomoff Date: Mon, 11 Nov 2013 13:23:45 -0800 Subject: [PATCH] Fix issue #5 - tests were reporting success if an exception was being thrown --- SenTestingKitAsync.xcodeproj/project.pbxproj | 2 - SenTestingKitAsync/SenTestingKitAsync.m | 52 +++++++++---------- .../SenTestingKitAsyncTests.m | 17 ++++++ 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/SenTestingKitAsync.xcodeproj/project.pbxproj b/SenTestingKitAsync.xcodeproj/project.pbxproj index c42b67b..eeaad14 100644 --- a/SenTestingKitAsync.xcodeproj/project.pbxproj +++ b/SenTestingKitAsync.xcodeproj/project.pbxproj @@ -15,7 +15,6 @@ F6B78911169B545C00034660 /* libSenTestingKitAsynciOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F6B788F8169B545C00034660 /* libSenTestingKitAsynciOS.a */; }; F6B78917169B545C00034660 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = F6B78915169B545C00034660 /* InfoPlist.strings */; }; F6B7891A169B545C00034660 /* SenTestingKitAsyncTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F6B78919169B545C00034660 /* SenTestingKitAsyncTests.m */; }; - F6B78927169B608400034660 /* SenTestingKitAsync.m in Sources */ = {isa = PBXBuildFile; fileRef = F6B78902169B545C00034660 /* SenTestingKitAsync.m */; }; F6F77D4F1712EAA9008727C6 /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F6B7890A169B545C00034660 /* SenTestingKit.framework */; }; F6F77D7D1712F334008727C6 /* SenTestingKitAsyncTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F6B78919169B545C00034660 /* SenTestingKitAsyncTests.m */; }; F6F77D7F1712F655008727C6 /* SenTestingKitAsync.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = F6B78900169B545C00034660 /* SenTestingKitAsync.h */; }; @@ -363,7 +362,6 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - F6B78927169B608400034660 /* SenTestingKitAsync.m in Sources */, F6B7891A169B545C00034660 /* SenTestingKitAsyncTests.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/SenTestingKitAsync/SenTestingKitAsync.m b/SenTestingKitAsync/SenTestingKitAsync.m index 34f144e..3d6c2bf 100644 --- a/SenTestingKitAsync/SenTestingKitAsync.m +++ b/SenTestingKitAsync/SenTestingKitAsync.m @@ -79,7 +79,7 @@ + (void)load; method_getImplementation(oldMethod), method_getTypeEncoding(oldMethod)); } - + Method newMethod = class_getInstanceMethod([self class], @selector(asyncFailWithException:)); if (newMethod) { class_replaceMethod(objc_getClass(class_getName(self)), @@ -92,27 +92,27 @@ + (void)load; - (void)performTest:(SenTestRun *)aRun withCompletionHandler:(SenTestCompletionHandler)aCompletionHandler; { __unsafe_unretained SenTestCase *weak = self; - + __block NSException *exception = nil; - + [weak setValue:aRun forKey:@"run"]; [weak setUp]; - + [self setUpWithCompletionHandler:^{ - + [aRun start]; - + if ([NSStringFromSelector([[weak invocation] selector]) hasSuffix:@"Async"]) { weak.testRun = aRun; weak.completionHandler = aCompletionHandler; - + @try { [[weak invocation] invoke]; } @catch (NSException *anException) { exception = anException; } - + if (exception != nil) { [aRun stop]; [weak tearDownWithCompletionHandler:^{ @@ -124,7 +124,7 @@ - (void)performTest:(SenTestRun *)aRun withCompletionHandler:(SenTestCompletionH weak.completionHandler = nil; }]; } - + } else { @try { [[weak invocation] invoke]; @@ -132,19 +132,19 @@ - (void)performTest:(SenTestRun *)aRun withCompletionHandler:(SenTestCompletionH @catch (NSException *anException) { exception = anException; } - - [aRun stop]; - + + [weak tearDownWithCompletionHandler:^{ [weak tearDown]; - + if (exception != nil) { [weak performSelector:@selector(logException:) withObject:exception]; } - + [aRun stop]; // this has to be called *after* we call logException otherwise the exception won't be logged + [weak setValue:nil forKey:@"run"]; - - aCompletionHandler(aRun); + + aCompletionHandler(aRun); }]; } }]; @@ -160,19 +160,19 @@ - (void)asyncFailWithException:(NSException *)anException; SenTestCompletionHandler aCompletionHandler = self.completionHandler; self.completionHandler = nil; dispatch_async(dispatch_get_main_queue(), ^{ - + SenTestRun *testRun = self.testRun; self.testRun = nil; - + if (anException != nil) { [self performSelector:@selector(logException:) withObject:anException]; } - + [testRun stop]; [self tearDownWithCompletionHandler:^{ [self tearDown]; [self setValue:nil forKey:@"run"]; - + aCompletionHandler(testRun); }]; }); @@ -207,9 +207,9 @@ - (void)performTest:(SenTestRun *)aTestRun withCompletionHandler:(SenTestComplet { [self setUp]; [aTestRun start]; - + NSEnumerator *testEnumerator = [[self valueForKey:@"tests"] objectEnumerator]; - + [self performTestRun:aTestRun withTestEnumerator:testEnumerator completionHandler:aCompletionHandler]; @@ -220,7 +220,7 @@ - (void)performTestRun:(SenTestRun *)aTestRun completionHandler:(SenTestCompletionHandler)aCompletionHandler { SenTest *aTest = [aTestEnumerator nextObject]; - + if (aTest) { [aTest runWithCompletionHandler:^(SenTestRun *run) { [(SenTestSuiteRun *)aTestRun addTestRun:run]; @@ -258,16 +258,16 @@ + (void)runTestsAsync:(id)ignored; @autoreleasepool { [[NSBundle allFrameworks] makeObjectsPerformSelector:@selector(principalClass)]; [SenTestObserver class]; - + NSRunLoop *mainRunLoop = [NSRunLoop mainRunLoop]; - + dispatch_async(dispatch_get_main_queue(), ^{ [[self specifiedTestSuite] runWithCompletionHandler:^(SenTestRun *run) { BOOL hasFailed = [run hasSucceeded]; exit((int)hasFailed); }]; }); - + [mainRunLoop run]; } } diff --git a/SenTestingKitAsyncTests/SenTestingKitAsyncTests.m b/SenTestingKitAsyncTests/SenTestingKitAsyncTests.m index 6e93abe..3dea4b3 100644 --- a/SenTestingKitAsyncTests/SenTestingKitAsyncTests.m +++ b/SenTestingKitAsyncTests/SenTestingKitAsyncTests.m @@ -82,4 +82,21 @@ - (void)testFailImmediatelyAsync STFail(@"Fail immediately."); } +- (void)testFailIfException +{ + NSAssert(NO, @""); + STFail(@"this should fail"); +} + +- (void)testFailIfExceptionAsync +{ + dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC); + dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ + NSAssert(NO, @""); + STFail(@"this should fail"); + }); + + STFailAfter(3, @"timed out"); +} + @end