Skip to content
Browse files

Get after hooks working properly.

  • Loading branch information...
1 parent 8eda09e commit aa3dcf0c5da3e655f9c097351daa1d7e0ff238e9 @lukeredpath lukeredpath committed Oct 19, 2011
Showing with 97 additions and 2 deletions.
  1. +70 −1 Examples/SimpleSpec.m
  2. +4 −0 Kiwi/KWContextNode.m
  3. +6 −1 Kiwi/KWExample.h
  4. +7 −0 Kiwi/KWExample.m
  5. +4 −0 Kiwi/KWExampleGroupBuilder.m
  6. +1 −0 Kiwi/KWExampleSuite.h
  7. +5 −0 Kiwi/KWExampleSuite.m
View
71 Examples/SimpleSpec.m
@@ -39,7 +39,7 @@
SPEC_END
-SPEC_BEGIN(HooksBehaviour)
+SPEC_BEGIN(BeforeHooksBehaviour)
describe(@"before hooks behaviour", ^{
__block NSInteger calls = 0;
@@ -108,3 +108,72 @@
SPEC_END
+SPEC_BEGIN(AfterHooksBehaviour)
+
+describe(@"after hooks behaviour", ^{
+ __block NSInteger calls = 0;
+
+ afterEach(^{
+ calls++;
+ });
+
+ it(@"will call afterEach but not afterAll after this spec", ^{
+ [[theValue(calls) should] equal:theValue(0)];
+ });
+
+ it(@"will call afterEach but not afterAll after this spec", ^{
+ [[theValue(calls) should] equal:theValue(1)];
+ });
+
+ context(@"with nested contexts", ^{
+ afterAll(^{
+ calls++;
+ });
+
+ afterEach(^{
+ calls++;
+ });
+
+ it(@"will call inner afterEach and outer afterEach but not afterAll after this spec", ^{
+ [[theValue(calls) should] equal:theValue(2)];
+ });
+
+ it(@"will call inner afterEach and outer afterEach but not afterAll after this spec", ^{
+ [[theValue(calls) should] equal:theValue(4)];
+ });
+
+ context(@"and another", ^{
+ afterAll(^{
+ calls++;
+ });
+
+ afterEach(^{
+ calls++;
+ });
+
+ it(@"will call inner afterEach and each outer afterEach but not afterAll after this spec", ^{
+ [[theValue(calls) should] equal:theValue(6)];
+ });
+
+ it(@"will call inner afterEach and each outer afterEach and the inner afterAll after this spec", ^{
+ [[theValue(calls) should] equal:theValue(9)];
+ });
+
+ });
+
+ it(@"will call this after the nested context specs above, reflecting the result of its afterAll, and this context's afterAll after this spec", ^{
+ [[theValue(calls) should] equal:theValue(13)];
+ });
+ });
+
+ it(@"will call this after both nested context specs above", ^{
+ [[theValue(calls) should] equal:theValue(16)];
+ });
+
+ afterAll(^{
+ NSLog(@"Can't test this as it's the last thing to run, but verify with log output");
+ });
+});
+
+SPEC_END
+
View
4 Kiwi/KWContextNode.m
@@ -113,6 +113,10 @@ - (void)performExample:(KWExample *)example withBlock:(void (^)(void))exampleBlo
innerExampleBlock();
[self.afterEachNode acceptExampleNodeVisitor:example];
+
+ if ([example isLastInContext:self]) {
+ [self.afterAllNode acceptExampleNodeVisitor:example];
+ }
} @catch (NSException *exception) {
KWFailure *failure = [KWFailure failureWithCallSite:self.callSite format:@"%@ \"%@\" raised", [exception name], [exception reason]];
View
7 Kiwi/KWExample.h
@@ -23,7 +23,7 @@
id<KWExampleNode> exampleNode;
BOOL passed;
}
-
+@property (nonatomic, retain) KWContextNode *lastInContext;
@property (nonatomic, assign) KWExampleSuite *suite;
- (id)initWithExampleNode:(id<KWExampleNode>)node;
@@ -44,6 +44,11 @@
- (NSString *)generateDescriptionForAnonymousItNode;
+#pragma mark -
+#pragma mark Checking if last in context
+
+- (BOOL)isLastInContext:(KWContextNode *)context;
+
@end
#pragma mark -
View
7 Kiwi/KWExample.m
@@ -42,6 +42,7 @@ @implementation KWExample
@synthesize exampleNodeStack;
@synthesize delegate = _delegate;
@synthesize suite;
+@synthesize lastInContext;
- (id)initWithExampleNode:(id<KWExampleNode>)node
{
@@ -57,13 +58,19 @@ - (id)initWithExampleNode:(id<KWExampleNode>)node
- (void)dealloc
{
+ [lastInContext release];
[exampleNode release];
[exampleNodeStack release];
[matcherFactory release];
[verifiers release];
[super dealloc];
}
+- (BOOL)isLastInContext:(KWContextNode *)context
+{
+ return context == self.lastInContext;
+}
+
- (NSString *)description
{
return [NSString stringWithFormat:@"<KWExample: %@>", exampleNode.description];
View
4 Kiwi/KWExampleGroupBuilder.m
@@ -126,6 +126,10 @@ - (void)pushContextNodeWithCallSite:(KWCallSite *)aCallSite description:(NSStrin
}
- (void)popContextNode {
+ KWContextNode *contextNode = [self.contextNodeStack lastObject];
+
+ [self.exampleSuite markLastExampleAsLastInContext:contextNode];
+
if ([self.contextNodeStack count] == 1)
[NSException raise:@"KWExampleGroupBuilderException" format:@"there is no open context to pop"];
View
1 Kiwi/KWExampleSuite.h
@@ -19,6 +19,7 @@
}
- (id)initWithRootNode:(KWContextNode *)contextNode;
- (void)addExample:(KWExample *)example;
+- (void)markLastExampleAsLastInContext:(KWContextNode *)context;
- (NSArray *)invocationsForTestCase;
@end
View
5 Kiwi/KWExampleSuite.m
@@ -40,6 +40,11 @@ - (void)addExample:(KWExample *)example
[example setSuite:self];
}
+- (void)markLastExampleAsLastInContext:(KWContextNode *)context
+{
+ [[examples objectAtIndex:examples.count-1] setLastInContext:context];
+}
+
- (NSArray *)invocationsForTestCase;
{
NSMutableArray *invocations = [NSMutableArray array];

0 comments on commit aa3dcf0

Please sign in to comment.
Something went wrong with that request. Please try again.