Permalink
Browse files

When we have anonymous specify nodes, we should be able to generate a…

… meaningful description

based on the actual matcher/expectation.

This assumes there is only one expectation in a specify() block but it will use the first
one regardless. (issue #43)
  • Loading branch information...
lukeredpath committed Aug 31, 2011
1 parent 5ff815e commit 5472d94a71ee4a8f0c54d801cebdf5524198e5eb
View
@@ -35,4 +35,9 @@ - (void)exampleWillEnd {
self.notifiedOfEndOfExample = YES;
}
+- (NSString *)descriptionForAnonymousItNode
+{
+ return @"";
+}
+
@end
View
@@ -22,15 +22,15 @@
[arr addObject:@"dolphin"];
});
- it(@"has 2 items", ^{
- [[arr should] contain:@"dolphin"];
- arr = nil;
+ it(@"has the first item", ^{
+ [[arr should] contain:@"shark"];
});
- it(@"has 2 items", ^{
+ it(@"has the second item", ^{
[[arr should] contain:@"dolphin"];
- arr = nil;
});
+
+ specify(^{ [[arr should] haveCountOf:2]; });
});
});
});
View
@@ -29,7 +29,7 @@
#pragma mark -
#pragma mark Getting Descriptions
-@property (nonatomic, readonly) NSString *description;
+@property (nonatomic, copy) NSString *description;
#pragma mark -
#pragma mark Getting Blocks
View
@@ -41,7 +41,7 @@ - (void)dealloc {
#pragma mark -
#pragma mark Getting Descriptions
-@synthesize description;
+@synthesize description = _description;
#pragma mark -
#pragma mark Accepting Visitors
View
@@ -45,6 +45,14 @@ - (void)dealloc {
[super dealloc];
}
+- (NSString *)descriptionForAnonymousItNode
+{
+ if (self.expectationType == KWExpectationTypeShould) {
+ return @"should exist";
+ }
+ return @"should not exist";
+}
+
#pragma mark -
#pragma mark Properties
View
@@ -151,6 +151,14 @@ - (NSString *)failureMessageForShouldNot {
[self itemPhrase]];
}
+#pragma mark -
+#pragma mark Description
+
+- (NSString *)description
+{
+ return [NSString stringWithFormat:@"%@ %u %@", [self verbPhrase], self.count, [self itemPhrase]];
+}
+
#pragma mark -
#pragma mark Configuring Matchers
View
@@ -9,9 +9,12 @@
#import "KWExampleNode.h"
@class KWPendingNode;
+@class KWSpec;
@interface KWItNode : KWBlockNode<KWExampleNode>
+@property (nonatomic, assign) KWSpec *spec;
+
#pragma mark -
#pragma mark Initializing
View
@@ -6,9 +6,13 @@
#import "KWItNode.h"
#import "KWExampleNodeVisitor.h"
+#import "KWSpec.h"
+#import "KWVerifying.h"
@implementation KWItNode
+@synthesize spec;
+
#pragma mark -
#pragma mark Initializing
@@ -23,4 +27,16 @@ - (void)acceptExampleNodeVisitor:(id<KWExampleNodeVisitor>)aVisitor {
[aVisitor visitItNode:self];
}
+#pragma mark -
+#pragma mark Runtime Description support
+
+- (NSString *)description
+{
+ NSString *description = [super description];
+ if (description == nil) {
+ description = [self.spec generateDescriptionForAnonymousItNode];
+ }
+ return description;
+}
+
@end
View
@@ -21,11 +21,14 @@ @interface KWMatchVerifier()
#pragma mark Properties
@property (nonatomic, readwrite, retain) id<KWMatching> endOfExampleMatcher;
+@property (nonatomic, readwrite, retain) id<KWMatching> matcher;
@end
@implementation KWMatchVerifier
+@synthesize matcher;
+
#pragma mark -
#pragma mark Initializing
@@ -55,10 +58,26 @@ + (id)matchVerifierWithExpectationType:(KWExpectationType)anExpectationType call
- (void)dealloc {
[subject release];
[callSite release];
+ [matcher release];
[endOfExampleMatcher release];
[super dealloc];
}
+- (NSString *)descriptionForAnonymousItNode
+{
+ NSString *typeString = @"";
+
+ switch (self.expectationType) {
+ case KWExpectationTypeShould:
+ typeString = @"should";
+ break;
+ case KWExpectationTypeShouldNot:
+ typeString = @"should not";
+ }
+ id<KWMatching> actualMatcher = (self.endOfExampleMatcher == nil) ? self.matcher : self.endOfExampleMatcher;
+ return [NSString stringWithFormat:@"%@ %@", typeString, actualMatcher];
+}
+
#pragma mark -
#pragma mark Properties
@@ -126,14 +145,14 @@ - (void)forwardInvocation:(NSInvocation *)anInvocation {
@try {
#endif // #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
- id matcher = [self.matcherFactory matcherFromInvocation:anInvocation subject:self.subject];
+ self.matcher = (id<KWMatching>)[self.matcherFactory matcherFromInvocation:anInvocation subject:self.subject];
- if (matcher == nil) {
+ if (self.matcher == nil) {
KWFailure *failure = [KWFailure failureWithCallSite:self.callSite format:@"could not create matcher for -%@",
NSStringFromSelector(anInvocation.selector)];
[self.reporter reportFailure:failure];
}
- [anInvocation invokeWithTarget:matcher];
+ [anInvocation invokeWithTarget:self.matcher];
#if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
// A matcher might have set an exception within the -invokeWithTarget, so
@@ -142,10 +161,13 @@ - (void)forwardInvocation:(NSInvocation *)anInvocation {
[exception raise];
#endif // #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
- if ([matcher respondsToSelector:@selector(shouldBeEvaluatedAtEndOfExample)] && [matcher shouldBeEvaluatedAtEndOfExample])
- self.endOfExampleMatcher = matcher;
- else
- [self verifyWithMatcher:matcher];
+ if ([self.matcher respondsToSelector:@selector(shouldBeEvaluatedAtEndOfExample)] && [self.matcher shouldBeEvaluatedAtEndOfExample]) {
+ self.endOfExampleMatcher = self.matcher;
+ self.matcher = nil;
+ }
+ else {
+ [self verifyWithMatcher:self.matcher];
+ }
#if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
} @catch (NSException *exception) {
View
@@ -43,4 +43,9 @@
- (void)buildExampleGroups;
+#pragma mark -
+#pragma mark Anonymous It Node Descriptions
+
+- (NSString *)generateDescriptionForAnonymousItNode;
+
@end
View
@@ -234,6 +234,8 @@ - (void)visitAfterEachNode:(KWAfterEachNode *)aNode {
- (void)visitItNode:(KWItNode *)aNode {
if (aNode.block == nil)
return;
+
+ aNode.spec = self;
@try {
for (KWContextNode *contextNode in self.exampleNodeStack) {
@@ -253,9 +255,16 @@ - (void)visitItNode:(KWItNode *)aNode {
#endif // #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
// Finish verifying and clear
- for (id<KWVerifying> verifier in self.verifiers)
- [verifier exampleWillEnd];
+ for (id<KWVerifying> verifier in self.verifiers) {
+ [verifier exampleWillEnd];
+ }
+
} @catch (NSException *exception) {
+ if (aNode.description == nil) {
+ // anonymous specify blocks should only have one verifier, but use the first in any case
+ aNode.description = [[self.verifiers objectAtIndex:0] descriptionForAnonymousItNode];
+ }
+
KWFailure *failure = [KWFailure failureWithCallSite:aNode.callSite format:@"%@ \"%@\" raised",
[exception name],
[exception reason]];
@@ -289,6 +298,11 @@ - (void)visitPendingNode:(KWPendingNode *)aNode {
[self.exampleNodeStack removeLastObject];
}
+- (NSString *)generateDescriptionForAnonymousItNode
+{
+ return [[self.verifiers objectAtIndex:0] descriptionForAnonymousItNode];
+}
+
#pragma mark -
#pragma mark Running Specs
View
@@ -8,6 +8,8 @@
@protocol KWVerifying<NSObject>
+- (NSString *)descriptionForAnonymousItNode;
+
#pragma mark -
#pragma mark Setting Subjects

0 comments on commit 5472d94

Please sign in to comment.