Permalink
Browse files

Add scope aliases ':example' => ':each' and ':group' => ':all'

  • Loading branch information...
John Feminella
John Feminella committed Jan 28, 2011
1 parent 0f9deff commit a48a4063e1a45679826b2bd88cbcc44dda063cd3
Showing with 56 additions and 1 deletion.
  1. +12 −1 lib/rspec/core/hooks.rb
  2. +44 −0 spec/rspec/core/example_group_spec.rb
View
@@ -142,7 +142,18 @@ def scope_and_options_from(scope=:each, options={})
options = scope
scope = :each
end
- return scope, options
+ return normalized_scope_for(scope), options
+ end
+
+ def scope_aliases
+ @scope_aliases ||= {
+ :example => :each,
+ :group => :all,
+ }
+ end
+
+ def normalized_scope_for(scope)
+ scope_aliases[scope] || scope
end
end
end
@@ -244,6 +244,50 @@ module RSpec::Core
end
describe "#before, after, and around hooks" do
+ describe "scope aliasing" do
+ it "aliases the `:group` hook scope to `:all`" do
+ group = ExampleGroup.describe
+ order = []
+ group.before(:group) { order << 1 }
+ group.example("example") {}
+
+ group.hooks[:before][:all].run_all(group)
+ order.should == [1]
+ end
+
+ it "aliases the `:example` hook scope to `:each`" do
+ group = ExampleGroup.describe
+ order = []
+ group.before(:example) { order << 1 }
+ group.example("example") {}
+
+ group.hooks[:before][:each].run_all(group)
+ order.should == [1]
+ end
+
+ it "should work with `before`" do
+ group = ExampleGroup.describe
+ order = []
+ group.before(:group) { order << 1 }
+ group.before(:example) { order << 2 }
+ group.example("example") {}
+
+ group.run
+ order.should == [1, 2]
+ end
+
+ it "should work with `after`" do
+ group = ExampleGroup.describe
+ order = []
+ group.after(:example) { order << 1 }
+ group.after(:group) { order << 2 }
+
+ group.example("example") {}
+
+ group.run
+ order.should == [1, 2]
+ end
+ end
it "runs the before alls in order" do
group = ExampleGroup.describe

0 comments on commit a48a406

Please sign in to comment.