Permalink
Browse files

Adding append_after, prepend_after, changes to after hooks storage an…

…d run method
  • Loading branch information...
1 parent 56d679b commit 19e2e2c529bfb6a29971ef38ff8d529c11bbfd5a @preethiramdev preethiramdev committed Mar 16, 2012
Showing with 127 additions and 5 deletions.
  1. +18 −4 lib/rspec/core/hooks.rb
  2. +109 −1 spec/rspec/core/hooks_spec.rb
View
@@ -77,11 +77,11 @@ def run_all!(example_group_instance)
class AfterHooks < HookCollection
def run_all(example_group_instance)
- reverse.each {|h| h.run_in(example_group_instance) }
+ each { |h| h.run_in(example_group_instance) } unless empty?
end
def run_all!(example_group_instance)
- pop.run_in(example_group_instance) until empty?
+ shift.run_in(example_group_instance) until empty?
end
end
@@ -254,11 +254,13 @@ def hooks
# File.delete(file_to_parse)
# end
# end
- def before(*args, &block)
+ def append_before(*args, &block)
scope, options = scope_and_options_from(*args)
hooks[:before][scope] << BeforeHook.new(options, &block)
end
+ alias_method :before, :append_before
+
# Registers a block to be executed before all other before blocks of the same scope.
# This method prepends +block+ to existing before blocks.
#
@@ -317,11 +319,23 @@ def prepend_before(*args, &block)
# This is the reverse of the order in which `before` hooks are run.
# Similarly, if more than one `after` is declared within any one scope,
# they are run in reverse order of that in which they are declared.
- def after(*args, &block)
+ def prepend_after(*args, &block)
+ scope, options = scope_and_options_from(*args)
+ hooks[:after][scope].unshift(AfterHook.new(options, &block))
+ end
+
+ alias_method :after, :prepend_after
+
+ # Registers a block to be executed after each example.
+ # This method appends +block+ to existing after blocks.
+ #
+ def append_after(*args, &block)
scope, options = scope_and_options_from(*args)
hooks[:after][scope] << AfterHook.new(options, &block)
end
+
+
# @api public
# @overload around(&block)
# @overload around(scope, &block)
@@ -119,6 +119,7 @@ class HooksHost
def yielder
yield
end
+
around do |example|
yielder { example.run }
end
@@ -141,7 +142,7 @@ def yielder
end
describe "prepend_before" do
- it "should order before callbacks from global to local" do
+ it "should prepend before callbacks so they are run prior to other before filters for the specified scope" do
fiddle = []
example_group = ExampleGroup.describe do
around do |example|
@@ -168,5 +169,112 @@ def yielder
]
end
end
+
+ describe "#append_before" do
+
+ it "should order before callbacks from global to local" do
+ order = []
+ example_group = ExampleGroup.describe do
+ around do |example|
+ example.run
+ end
+ it "foo" do
+ examples << self
+ end
+ end
+ example_group.append_before(:each) do
+ order << :example_group_append_before_each
+ end
+ RSpec.configure { |config| config.append_before { order << :append_before_each } } # default is :each
+ RSpec.configure { |config| config.append_before(:all) { order << :append_before_all } }
+ RSpec.configure { |config| config.before(:all) { order << :before_all } }
+
+ example_group.append_before(:all) do
+ order << :example_group_append_before_all
+ end
+
+ example_group.run
+ order.should == [
+ :append_before_all,
+ :before_all,
+ :example_group_append_before_all,
+ :append_before_each,
+ :example_group_append_before_each,
+ ]
+ end
+ end
+
+ describe "#prepend_after" do
+
+ it "should order after callbacks from global to local" do
+ order = []
+ example_group = ExampleGroup.describe do
+ around do |example|
+ example.run
+ end
+ it "foo" do
+ examples << self
+ end
+ end
+
+ RSpec.configure { |config| config.prepend_after(:all) { order << :prepend__after_all } }
+ RSpec.configure { |config| config.prepend_after(:each) { order << :prepend__after_each } }
+ example_group.prepend_after(:all) do
+ order << :example_group_prepend_after_all
+ end
+ example_group.prepend_after(:each) do
+ order << :example_group_prepend_after_each
+ end
+ example_group.run
+ order.should == [
+ :example_group_prepend_after_each,
+ :prepend__after_each,
+ :example_group_prepend_after_all,
+ :prepend__after_all
+ ]
+ end
+ end
+
+ describe "#append_after" do
+
+ it "should append callbacks so they are run after other after filters for the specified scope" do
+ order = []
+ example_group = ExampleGroup.describe do
+ around do |example|
+ example.run
+ end
+ it "foo" do
+ examples << self
+ end
+ end
+
+ RSpec.configure { |config| config.append_after(:all) { order << :append__after_all } }
+ RSpec.configure { |config| config.after(:all) { order << :after_all } }
+ example_group.append_after(:all) do
+ order << :example_group_append__after_all
+ end
+
+ RSpec.configure { |config| config.append_after(:each) { order << :append_after_each } }
+
+ example_group.append_after(:each) do
+ order << :example_group_append__after_each
+ end
+
+ example_group.after(:each) do
+ order << :example_group__after_each
+ end
+
+ example_group.run
+ order.should == [
+ :example_group__after_each,
+ :example_group_append__after_each,
+ :append_after_each,
+ :example_group_append__after_all,
+ :after_all,
+ :append__after_all
+ ]
+ end
+
+ end
end
end

0 comments on commit 19e2e2c

Please sign in to comment.