Skip to content

Commit

Permalink
Added 'action' block which is run after all beforeEach and just befor…
Browse files Browse the repository at this point in the history
…e example (it()). This one allows cleaner testing with less code duplication.
  • Loading branch information
lukasz-warchol committed Sep 11, 2012
1 parent 423c7c6 commit d5f0a2f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions Source/CDRExample.m
Expand Up @@ -57,6 +57,7 @@ - (void)run {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@try {
[parent_ setUp];
[parent_ runAction];
block_();
self.state = CDRExampleStatePassed;
} @catch (CDRSpecFailure *x) {
Expand Down
11 changes: 11 additions & 0 deletions Source/CDRExampleGroup.m
Expand Up @@ -8,6 +8,8 @@ - (void)stopObservingExamples;
@implementation CDRExampleGroup

@synthesize examples = examples_;
@synthesize action = action_;


+ (id)groupWithText:(NSString *)text {
return [[[[self class] alloc] initWithText: text] autorelease];
Expand All @@ -29,6 +31,7 @@ - (id)initWithText:(NSString *)text isRoot:(BOOL)isRoot {

- (void)dealloc {
[afterBlocks_ release];
[action_ release];
[examples_ release];
[beforeBlocks_ release];
[super dealloc];
Expand Down Expand Up @@ -111,6 +114,14 @@ - (void)setUp {
}
}

- (void)runAction
{
[parent_ runAction];
if (self.action) {
self.action();
}
}

- (void)tearDown {
for (CDRSpecBlock afterBlock in afterBlocks_) {
afterBlock();
Expand Down
4 changes: 4 additions & 0 deletions Source/CDRSpec.m
Expand Up @@ -10,6 +10,10 @@ void beforeEach(CDRSpecBlock block) {
[currentSpec.currentGroup addBefore:block];
}

void action(CDRSpecBlock block) {
[currentSpec.currentGroup setAction:block];
}

void afterEach(CDRSpecBlock block) {
[currentSpec.currentGroup addAfter:block];
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Headers/CDRExampleGroup.h
Expand Up @@ -4,7 +4,7 @@
NSMutableArray *beforeBlocks_, *examples_, *afterBlocks_;
BOOL isRoot_;
}

@property(nonatomic, copy) CDRSpecBlock action;
@property (nonatomic, readonly) NSArray *examples;

+ (id)groupWithText:(NSString *)text;
Expand Down
1 change: 1 addition & 0 deletions Source/Headers/CDRExampleParent.h
Expand Up @@ -5,6 +5,7 @@
- (BOOL)shouldRun;

- (void)setUp;
- (void)runAction;
- (void)tearDown;

@optional
Expand Down
1 change: 1 addition & 0 deletions Source/Headers/CDRSpec.h
Expand Up @@ -13,6 +13,7 @@ extern const CDRSpecBlock PENDING;
extern "C" {
#endif
void beforeEach(CDRSpecBlock);
void action(CDRSpecBlock block);
void afterEach(CDRSpecBlock);

CDRExampleGroup * describe(NSString *, CDRSpecBlock);
Expand Down
3 changes: 3 additions & 0 deletions Source/SpecHelper.m
Expand Up @@ -45,6 +45,9 @@ - (BOOL)shouldRun {
return NO;
}

- (void)runAction
{}

- (void)setUp {
if ([self respondsToSelector:@selector(beforeEach)]) {
NSLog(@"********************************************************************************");
Expand Down

1 comment on commit d5f0a2f

@robertwijas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super useful. We should create a pull request for that!

Please sign in to comment.