Permalink
Browse files

Added 'subject' command for example groups.

  • Loading branch information...
1 parent d285c57 commit a8b7a27ac89af0743de80b6f608c118604d16ff9 Adam Milligan committed Dec 28, 2012
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>IDECodeSnippetCompletionPrefix</key>
+ <string>csubj</string>
+ <key>IDECodeSnippetCompletionScopes</key>
+ <array>
+ <string>All</string>
+ </array>
+ <key>IDECodeSnippetContents</key>
+ <string>subject(^{ &lt;#action#&gt; });</string>
+ <key>IDECodeSnippetIdentifier</key>
+ <string>f81e0e39-8b8a-6629-aa6f-4a19198c413d</string>
+ <key>IDECodeSnippetLanguage</key>
+ <string>Xcode.SourceCodeLanguage.Objective-C</string>
+ <key>IDECodeSnippetSummary</key>
+ <string>Cedar subject block</string>
+ <key>IDECodeSnippetTitle</key>
+ <string>Cedar subject</string>
+ <key>IDECodeSnippetUserSnippet</key>
+ <true/>
+ <key>IDECodeSnippetVersion</key>
+ <integer>2</integer>
+ <key>isCedarSnippet</key>
+ <true/>
+</dict>
+</plist>
View
@@ -314,6 +314,35 @@ Note: For improved Xcode integration see
that provides keyboard shortcuts for focusing on specs under editor cursor.
+## Subject blocks
+
+Generally you want each top-level describe block to describe a single method or
+action. Often you end up calling this action in multiple places at multiple
+levels of nesting after various amounts of setup. In this case you can use a
+subject block to simply your specs. A subject block differs from a before each
+block because you may have only one for any given example (if multiple levels
+define a subject block Cedar will throw an exception), and it will run after
+all before each blocks for a given example. For example:
+
+describe(@"thing", ^{
+ __block BOOL parameter;
+
+ subject(^{ [object doThingWithParameter:parameter]; });
+
+ describe(@"when something is true", ^{
+ beforeEach(^{
+ parameter = YES;
+ });
+
+ it(@"should ...", ^{
+ // ...
+ });
+ });
+});
+
+In this case the parameter will be set to YES *before* the subject action runs.
+
+
## Reporters
When running in headless mode by default Cedar uses `CDRDefaultReporter` to
View
@@ -58,6 +58,7 @@ - (void)run {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@try {
[parent_ setUp];
+ if (parent_.subjectBlock) { parent_.subjectBlock(); }
block_();
self.state = CDRExampleStatePassed;
} @catch (CDRSpecFailure *x) {
@@ -31,6 +31,7 @@ - (void)dealloc {
[afterBlocks_ release];
[examples_ release];
[beforeBlocks_ release];
+ self.subjectBlock = nil;
[super dealloc];
}
View
@@ -34,6 +34,10 @@ void afterEach(CDRSpecBlock block) {
CDRExampleGroup* (*context)(NSString *, CDRSpecBlock) = &describe;
+void subject(CDRSpecBlock block) {
+ currentSpec.currentGroup.subjectBlock = block;
+}
+
CDRExample * it(NSString *text, CDRSpecBlock block) {
CDRExample *example = [CDRExample exampleWithText:text andBlock:block];
[currentSpec.currentGroup add:example];
View
@@ -98,6 +98,30 @@ void expectFailure(CDRSpecBlock block) {
// });
});
+describe(@"subject", ^{
+ __block int value;
+
+ subject(^{ value = 5; });
+
+ beforeEach(^{
+ value = 100;
+ });
+
+ it(@"should run after the beforeEach", ^{
+ value should equal(5);
+ });
+
+ describe(@"in a nested describe block", ^{
+ beforeEach(^{
+ value = 200;
+ });
+
+ it(@"should run after all the beforeEach blocks", ^{
+ value should equal(5);
+ });
+ });
+});
+
describe(@"Matchers", ^{
describe(@"equality", ^{
describe(@"with Objective-C types", ^{

0 comments on commit a8b7a27

Please sign in to comment.