Skip to content

Commit

Permalink
Fix for running specs under SenTestingKit or XCTest.
Browse files Browse the repository at this point in the history
Remove global state from +defaultTestSuite.

When we stored this on the class, we would cause things to fail if we
created multiple test suites. Setting it on the instance allows multiple
test suites to coexist in harmony. In theory.

Rename -allExamples to -allExamplesToRun.
  • Loading branch information
tjarratt authored and Eric Tsiliacos and Jeff Hui committed Oct 1, 2014
1 parent 7dd1bee commit 78d209a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Cedar.xcodeproj/project.pbxproj
Expand Up @@ -361,6 +361,7 @@
AE9BA627184D203000079A97 /* ConformTo.h in Copy headers to framework */ = {isa = PBXBuildFile; fileRef = 5898AEAF3FE8C683E6F23C1D /* ConformTo.h */; };
AE9EAAD9178C789800CCF7DA /* CDRDefaultReporterSpec.mm in Sources */ = {isa = PBXBuildFile; fileRef = AEBCDD7E173ACD6700B42B58 /* CDRDefaultReporterSpec.mm */; };
AE9EAADA178C789900CCF7DA /* CDRDefaultReporterSpec.mm in Sources */ = {isa = PBXBuildFile; fileRef = AEBCDD7E173ACD6700B42B58 /* CDRDefaultReporterSpec.mm */; };
AEA8962C19D0C242007D5C08 /* CDROTestRunner.m in Sources */ = {isa = PBXBuildFile; fileRef = 96EA1CA7142C6425001A78E0 /* CDROTestRunner.m */; };
AEB1A74215F304A9002E4167 /* StubbedMethod.mm in Sources */ = {isa = PBXBuildFile; fileRef = AEB1A74115F304A9002E4167 /* StubbedMethod.mm */; };
AEB1A74315F304A9002E4167 /* StubbedMethod.mm in Sources */ = {isa = PBXBuildFile; fileRef = AEB1A74115F304A9002E4167 /* StubbedMethod.mm */; };
AEB45A911496C8D800845D09 /* RaiseException.h in Headers */ = {isa = PBXBuildFile; fileRef = AEB45A901496C8D800845D09 /* RaiseException.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -2571,6 +2572,7 @@
E31179D6161FD937007D3CDE /* CDRSlowTestStatistics.m in Sources */,
22FB7B4416ACBB3A00012D69 /* HeadlessSimulatorWorkaround.m in Sources */,
AE7F170B172730B000E1146D /* NSInvocation+Cedar.m in Sources */,
AEA8962C19D0C242007D5C08 /* CDROTestRunner.m in Sources */,
34ADD2E11921F18100B057AC /* AnyInstanceOfClassArgument.mm in Sources */,
AED23E6D173D5545003D7A41 /* BeCloseTo.mm in Sources */,
AE02021A17452007009A7915 /* StringifiersBase.mm in Sources */,
Expand Down
15 changes: 15 additions & 0 deletions OCUnitAppTests/OCUnitApplicationTestsWithSenTestingKit.m
@@ -1,6 +1,7 @@
#define SENTEST_IGNORE_DEPRECATION_WARNING
#import <SenTestingKit/SenTestingKit.h>
#import "OCUnitAppAppDelegate.h" // should NOT be included in OCUnitAppTests target
#import "CDRXTestSuite.h"

@interface ExampleApplicationTestsWithSenTestingKit : SenTestCase
@end
Expand All @@ -24,4 +25,18 @@ - (void)testCanLoadNibFilesFromApp {
NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"DummyView" owner:nil options:nil];
STAssertEquals([[views lastObject] class], [UIView class], @"expected last view of DummyView nib to be UIView kind");
}

- (void)testRunningCedarExamples {
SenTestSuite *defaultSuite = [SenTestSuite defaultTestSuite];
STAssertTrue([[defaultSuite valueForKeyPath:@"tests.name"] containsObject:@"Cedar"], @"should contain a Cedar test suite");
}

- (void)testCallingDefaultTestSuiteMultipleTimesShouldHaveDifferentReporters {
SenTestSuite *defaultSuite1 = [SenTestSuite defaultTestSuite];
SenTestSuite *defaultSuite2 = [SenTestSuite defaultTestSuite];

CDRXTestSuite *suite1 = [[defaultSuite1 valueForKey:@"tests"] lastObject];
CDRXTestSuite *suite2 = [[defaultSuite2 valueForKey:@"tests"] lastObject];
STAssertTrue(suite1.dispatcher != suite2.dispatcher, @"Each test suite should have its own dispatcher");
}
@end
2 changes: 1 addition & 1 deletion Source/CDRFunctions.m
Expand Up @@ -393,7 +393,7 @@ static id CDRCreateXCTestSuite() {

CDRReportDispatcher *dispatcher = [[[CDRReportDispatcher alloc] initWithReporters:CDRReportersToRun()] autorelease];

[CDRXTestSuite setDispatcher:dispatcher];
[testSuite setDispatcher:dispatcher];

NSArray *groups = CDRRootGroupsFromSpecs(specs);
[dispatcher runWillStartWithGroups:groups andRandomSeed:seed];
Expand Down
2 changes: 0 additions & 2 deletions Source/CDROTestRunner.m
Expand Up @@ -8,10 +8,8 @@ @interface CDROTestRunner ()

@implementation CDROTestRunner

#if !TARGET_OS_IPHONE
+ (void)load {
CDRHijackOCUnitAndXCTestRun((IMP)CDRRunTests);
}
#endif

@end
4 changes: 2 additions & 2 deletions Source/iPhone/XCTest/CDRSpec+XCTestSupport.m
Expand Up @@ -33,7 +33,7 @@ - (id)testSuiteWithRandomSeed:(unsigned int)seed dispatcher:(CDRReportDispatcher
Class newXCTestSubclass = [self createTestCaseSubclass];

CDROTestNamer *namer = [[[CDROTestNamer alloc] init] autorelease];
NSArray *examples = [self allExamples];
NSArray *examples = [self allExamplesToRun];

NSMutableArray *testInvocations = [NSMutableArray array];
for (CDRExample *example in examples) {
Expand Down Expand Up @@ -84,7 +84,7 @@ - (Class)createTestCaseSubclass {
return newXCTestSubclass;
}

- (NSArray *)allExamples {
- (NSArray *)allExamplesToRun {
NSMutableArray *examples = [NSMutableArray array];
NSMutableArray *groupsQueue = [NSMutableArray arrayWithArray:self.rootGroup.examples];
while (groupsQueue.count) {
Expand Down
4 changes: 2 additions & 2 deletions Source/iPhone/XCTest/CDRXTestSuite.h
Expand Up @@ -4,7 +4,7 @@

@interface CDRXTestSuite : NSObject

+ (void)setDispatcher:(CDRReportDispatcher *)dispatcher;
+ (CDRReportDispatcher *)dispatcher;
- (void)setDispatcher:(CDRReportDispatcher *)dispatcher;
- (CDRReportDispatcher *)dispatcher;

@end
14 changes: 6 additions & 8 deletions Source/iPhone/XCTest/CDRXTestSuite.m
Expand Up @@ -2,26 +2,24 @@
#import "CDRReportDispatcher.h"
#import <objc/runtime.h>

static CDRReportDispatcher *__CDR_XCTestSuiteDispatcher;
const char *CDRXDispatcherKey;

@implementation CDRXTestSuite

+ (void)setDispatcher:(CDRReportDispatcher *)dispatcher {
CDRReportDispatcher *oldValue = __CDR_XCTestSuiteDispatcher;
__CDR_XCTestSuiteDispatcher = [dispatcher retain];
[oldValue release];
- (void)setDispatcher:(CDRReportDispatcher *)dispatcher {
objc_setAssociatedObject(self, &CDRXDispatcherKey, dispatcher, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

+ (CDRReportDispatcher *)dispatcher {
return __CDR_XCTestSuiteDispatcher;
- (CDRReportDispatcher *)dispatcher {
return objc_getAssociatedObject(self, &CDRXDispatcherKey);
}

- (void)performTest:(id)aRun {
Class parentClass = class_getSuperclass([self class]);
IMP superPerformTest = class_getMethodImplementation(parentClass, @selector(performTest:));
((void (*)(id instance, SEL cmd, id run))superPerformTest)(self, _cmd, aRun);

[__CDR_XCTestSuiteDispatcher runDidComplete];
[[self dispatcher] runDidComplete];
}

@end
5 changes: 5 additions & 0 deletions XCUnitAppTests/XCUnitApplicationTestsWithXCTest.m
Expand Up @@ -23,4 +23,9 @@ - (void)testCanLoadNibFilesFromApp {
NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"DummyView" owner:nil options:nil];
XCTAssertEqual([[views lastObject] class], [UIView class], @"expected last view of DummyView nib to be UIView kind");
}

- (void)testRunningCedarExamples {
XCTestSuite *defaultSuite = [XCTestSuite defaultTestSuite];
XCTAssert([[defaultSuite valueForKeyPath:@"tests.name"] containsObject:@"Cedar"]);
}
@end

0 comments on commit 78d209a

Please sign in to comment.