Skip to content

Commit

Permalink
Test CDRHooks with more rigor
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Coward and Tim Jarratt committed Apr 14, 2016
1 parent 82debd2 commit 873eabf
Showing 1 changed file with 55 additions and 11 deletions.
66 changes: 55 additions & 11 deletions Spec/CDRHooksSpec.mm
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#import "Cedar.h"
#import "CDRSpec.h"
#import "CDRSpecRun.h"

static BOOL conformantClassBeforeEachWasTriggered__ = NO;
static BOOL conformantClassBeforeEachWasTriggeredBeforeSpecBeforeEach__ = NO;
static BOOL nonConformantClassBeforeEachWasTriggered__ = NO;
static BOOL conformantClassAfterEachWasTriggered__ = NO;
static BOOL conformantClassAfterEachWasTriggeredBeforeSpecAfterEach__ = NO;
static BOOL nonConformantClassAfterEachWasTriggered__ = NO;

using namespace Cedar::Matchers;
Expand Down Expand Up @@ -37,23 +41,63 @@ + (void)afterEach {
}
@end

void verifyAfterEachCalledAsExpected() {
NSCAssert(conformantClassAfterEachWasTriggered__, @"Expected +[ClassThatConformsToCDRHooks afterEach] to be called, but it wasn't. (From %s)", __FILE__);
NSCAssert(!nonConformantClassAfterEachWasTriggered__, @"Expected +[ClassThatDoesntConformToCDRHooks afterEach] to NOT be called, but it was. (From %s)", __FILE__);
@interface DummySpecForTestingHooks : CDRSpec
@end
@implementation DummySpecForTestingHooks \
- (void)declareBehaviors {
self.fileName = [NSString stringWithUTF8String:__FILE__];
beforeEach(^{
conformantClassBeforeEachWasTriggeredBeforeSpecBeforeEach__ = conformantClassBeforeEachWasTriggered__;
});
it(@"just needs to have a spec", ^{
});
afterEach(^{
conformantClassAfterEachWasTriggeredBeforeSpecAfterEach__ = conformantClassAfterEachWasTriggered__;
});
}
@end

SPEC_BEGIN(CDRHooksSpec)

describe(@"global beforeEach", ^{
it(@"should run the +beforeEach before only for CDRHooks conformant classes", ^{
expect(conformantClassBeforeEachWasTriggered__).to(be_truthy);
expect(nonConformantClassBeforeEachWasTriggered__).to(be_falsy);
//NB: If you focus any test here, you must also focus "just needs to run this spec" in DummySpecForTestingHooks
describe(@"CDRHooks", ^{
beforeEach(^{
nonConformantClassBeforeEachWasTriggered__ =
nonConformantClassAfterEachWasTriggered__ =
conformantClassBeforeEachWasTriggered__ =
conformantClassBeforeEachWasTriggeredBeforeSpecBeforeEach__ =
conformantClassAfterEachWasTriggered__ =
conformantClassAfterEachWasTriggeredBeforeSpecAfterEach__ = NO;

CDRSpec *dummySpec = [[[DummySpecForTestingHooks class] alloc] init];
[dummySpec defineBehaviors];

CDRSpecRun *specRun = [[CDRSpecRun alloc] initWithExampleReporters:@[]];
[specRun performSpecRun:^{
[dummySpec.rootGroup runWithDispatcher:specRun.dispatcher];
}];
});
});

describe(@"global afterEach", ^{
it(@"should run after all specs", ^{
atexit(verifyAfterEachCalledAsExpected);
describe(@"+beforeEach", ^{
it(@"should not call +beforeEach on non-conformant classes", ^{
expect(nonConformantClassBeforeEachWasTriggered__).to(be_falsy);
});

it(@"should run the +beforeEach BEFORE spec beforeEaches for CDRHooks conformers", ^{
expect(conformantClassBeforeEachWasTriggered__).to(be_truthy);
expect(conformantClassBeforeEachWasTriggeredBeforeSpecBeforeEach__).to(be_truthy);
});
});

describe(@"+afterEach", ^{
it(@"should not call +afterEach on non-conformant classes", ^{
expect(nonConformantClassAfterEachWasTriggered__).to(be_falsy);
});

it(@"should run the +afterEach AFTER spec afterEaches for CDRHooks conformers", ^{
expect(conformantClassAfterEachWasTriggered__).to(be_truthy);
expect(conformantClassAfterEachWasTriggeredBeforeSpecAfterEach__).to(be_falsy);
});
});
});

Expand Down

0 comments on commit 873eabf

Please sign in to comment.