Permalink
Browse files

Example group hierarchy returns a subject block

  • Loading branch information...
1 parent 75535b0 commit d285c57dc9c892d018292de5dedd6b21e335c2ee Adam Milligan committed Dec 28, 2012
View
20 Source/CDRExampleGroup.m
@@ -7,7 +7,7 @@ - (void)stopObservingExamples;
@implementation CDRExampleGroup
-@synthesize examples = examples_;
+@synthesize examples = examples_, subjectBlock = subjectBlock_;
+ (id)groupWithText:(NSString *)text {
return [[[[self class] alloc] initWithText: text] autorelease];
@@ -36,7 +36,23 @@ - (void)dealloc {
#pragma mark Public interface
- (NSString *)description {
- return [NSString stringWithFormat:@"Example Group: %@", self.text];
+ return [NSString stringWithFormat:@"Example Group: \"%@\"", self.fullText];
+}
+
+- (CDRSpecBlock)subjectBlock {
+ CDRSpecBlock parentSubjectBlock = self.parent.subjectBlock;
+ if (subjectBlock_) {
+ if (parentSubjectBlock) {
+ [[NSException exceptionWithName:NSInternalInconsistencyException
+ reason:[NSString stringWithFormat:@"%@ has more than one subject block", self]
+ userInfo:nil]
+ raise];
+ } else {
+ return subjectBlock_;
+ }
+ } else {
+ return parentSubjectBlock;
+ }
}
- (void)add:(CDRExampleBase *)example {
View
2 Source/Headers/CDRExampleBase.h
@@ -3,8 +3,6 @@
@protocol CDRExampleReporter;
-typedef void (^CDRSpecBlock)(void);
-
enum CDRExampleState {
CDRExampleStateIncomplete = 0x00,
CDRExampleStateSkipped = 0x01,
View
1 Source/Headers/CDRExampleGroup.h
@@ -6,6 +6,7 @@
BOOL isRoot_;
}
+@property (nonatomic, copy) CDRSpecBlock subjectBlock;
@property (nonatomic, readonly) NSArray *examples;
+ (id)groupWithText:(NSString *)text;
View
3 Source/Headers/CDRExampleParent.h
@@ -1,10 +1,13 @@
#import <Foundation/Foundation.h>
+typedef void (^CDRSpecBlock)(void);
+
@protocol CDRExampleParent
- (BOOL)shouldRun;
- (void)setUp;
+- (CDRSpecBlock)subjectBlock;
- (void)tearDown;
@optional
View
2 Source/Headers/CDRSpec.h
@@ -17,10 +17,12 @@ void afterEach(CDRSpecBlock);
CDRExampleGroup * describe(NSString *, CDRSpecBlock);
extern CDRExampleGroup* (*context)(NSString *, CDRSpecBlock);
+
CDRExample * it(NSString *, CDRSpecBlock);
CDRExampleGroup * xdescribe(NSString *, CDRSpecBlock);
extern CDRExampleGroup* (*xcontext)(NSString *, CDRSpecBlock);
+void subject(CDRSpecBlock);
CDRExample * xit(NSString *, CDRSpecBlock);
CDRExampleGroup * fdescribe(NSString *, CDRSpecBlock);
View
4 Source/SpecHelper.m
@@ -58,6 +58,10 @@ - (void)setUp {
[self.globalBeforeEachClasses makeObjectsPerformSelector:@selector(beforeEach)];
}
+- (CDRSpecBlock)subjectBlock {
+ return nil;
+}
+
- (void)tearDown {
if ([self respondsToSelector:@selector(afterEach)]) {
NSLog(@"********************************************************************************");
View
119 Spec/CDRExampleGroupSpec.mm
@@ -26,23 +26,13 @@
NSString *groupText = @"Group!";
beforeEach(^{
- group = [[CDRExampleGroup alloc] initWithText:groupText];
- incompleteExample = [[CDRExample alloc] initWithText:@"incomplete" andBlock:^{}];
- passingExample = [[CDRExample alloc] initWithText:@"I should pass" andBlock:^{}];
- failingExample = [[CDRExample alloc] initWithText:@"I should fail" andBlock:^{fail(@"I have failed.");}];
- pendingExample = [[CDRExample alloc] initWithText:@"I should pend" andBlock:nil];
- errorExample = [[CDRExample alloc] initWithText:@"I should raise an error" andBlock:^{ @throw @"wibble"; }];
- nonFocusedExample = [[CDRExample alloc] initWithText:@"I should not be focused" andBlock:^{}];
- });
-
- afterEach(^{
- [errorExample release];
- [pendingExample release];
- [failingExample release];
- [passingExample release];
- [incompleteExample release];
- [nonFocusedExample release];
- [group release];
+ group = [[[CDRExampleGroup alloc] initWithText:groupText] autorelease];
+ incompleteExample = [[[CDRExample alloc] initWithText:@"incomplete" andBlock:^{}] autorelease];
+ passingExample = [[[CDRExample alloc] initWithText:@"I should pass" andBlock:^{}] autorelease];
+ failingExample = [[[CDRExample alloc] initWithText:@"I should fail" andBlock:^{fail(@"I have failed.");}] autorelease];
+ pendingExample = [[[CDRExample alloc] initWithText:@"I should pend" andBlock:nil] autorelease];
+ errorExample = [[[CDRExample alloc] initWithText:@"I should raise an error" andBlock:^{ @throw @"wibble"; }] autorelease];
+ nonFocusedExample = [[[CDRExample alloc] initWithText:@"I should not be focused" andBlock:^{}] autorelease];
});
describe(@"hasChildren", ^{
@@ -633,6 +623,101 @@
exampleGroup.runTime should be_greater_than_or_equal_to(firstExample.runTime + secondExample.runTime);
});
});
+
+ describe(@"subjectBlock", ^{
+ context(@"with a subject block set", ^{
+ CDRSpecBlock subjectBlock = ^{};
+
+ beforeEach(^{
+ group.subjectBlock = subjectBlock;
+ });
+
+ context(@"and with a parent", ^{
+ __block CDRExampleGroup *parent;
+
+ beforeEach(^{
+ parent = [[[CDRExampleGroup alloc] initWithText:@"Parent"] autorelease];
+ group.parent = parent;
+ });
+
+ context(@"which has a subject block", ^{
+ CDRSpecBlock parentSubjectBlock = ^{};
+
+ beforeEach(^{
+ parent.subjectBlock = parentSubjectBlock;
+ });
+
+ it(@"should raise a duplicate subject block exception", ^{
+ __block id dummy;
+ ^{ dummy = group.subjectBlock; } should raise_exception;
+ });
+ });
+
+ context(@"which does not have a subject block", ^{
+ beforeEach(^{
+ parent.subjectBlock should_not be_truthy;
+ });
+
+ it(@"should return its subject block", ^{
+ group.subjectBlock should equal(subjectBlock);
+ });
+ });
+ });
+
+ context(@"and with no parent", ^{
+ beforeEach(^{
+ group.parent should be_nil;
+ });
+
+ it(@"should return its subject block", ^{
+ group.subjectBlock should equal(subjectBlock);
+ });
+ });
+ });
+
+ context(@"with no subject block set", ^{
+ context(@"and with a parent", ^{
+ __block CDRExampleGroup *parent;
+
+ beforeEach(^{
+ parent = [[[CDRExampleGroup alloc] initWithText:@"Parent"] autorelease];
+ group.parent = parent;
+ });
+
+ context(@"which has a subject block", ^{
+ CDRSpecBlock parentSubjectBlock = ^{};
+
+ beforeEach(^{
+ parent.subjectBlock = parentSubjectBlock;
+ });
+
+ it(@"should return its parent's subject block", ^{
+ group.subjectBlock should equal(parentSubjectBlock);
+ });
+ });
+
+ context(@"which does not have a subject block", ^{
+ beforeEach(^{
+ parent.subjectBlock should_not be_truthy;
+ });
+
+ it(@"should return nil", ^{
+ group.subjectBlock should_not be_truthy;
+ });
+ });
+ });
+
+ context(@"and with no parent", ^{
+ beforeEach(^{
+ group.parent should be_nil;
+ });
+
+ it(@"should return nil", ^{
+ group.subjectBlock should_not be_truthy;
+ });
+ });
+ });
+ });
});
SPEC_END

0 comments on commit d285c57

Please sign in to comment.