Permalink
Browse files

add unfiltered run_hook methods

  • Loading branch information...
1 parent 9edfa29 commit 5e05632bf48222514ce2f16b0fe62a4968484e33 @dchelimsky dchelimsky committed Jun 19, 2010
Showing with 22 additions and 10 deletions.
  1. +4 −10 lib/rspec/core/example_group.rb
  2. +18 −0 lib/rspec/core/hooks.rb
@@ -145,30 +145,24 @@ def self.eval_before_alls(example)
superclass.before_all_ivars.each { |ivar, val| example.instance_variable_set(ivar, val) }
world.run_hook(:before, :all, self, example)
- until before_alls.empty?
- example.instance_eval &before_alls.shift.last
- end
+ run_hook_unfiltered!(:before, :all, example, :reverse => true)
example.instance_variables.each { |ivar| before_all_ivars[ivar] = example.instance_variable_get(ivar) }
end
def self.eval_before_eachs(example)
world.run_hook(:before, :each, self, example)
- ancestors.reverse.each { |ancestor| ancestor.before_eachs.each { |arr| example.instance_eval(&arr.last) } }
+ ancestors.reverse.each { |ancestor| ancestor.run_hook_unfiltered(:before, :each, example) }
end
def self.eval_after_eachs(example)
- ancestors.each { |ancestor| ancestor.after_eachs.reverse.each { |arr| example.instance_eval(&arr.last) } }
+ ancestors.each { |ancestor| ancestor.run_hook_unfiltered(:after, :each, example, :reverse => true) }
world.run_hook(:after, :each, self, example)
end
def self.eval_after_alls(example)
return if descendant_filtered_examples.empty?
before_all_ivars.each { |ivar, val| example.instance_variable_set(ivar, val) }
- ancestors.each do |ancestor|
- until ancestor.after_alls.empty?
- example.instance_eval &ancestor.after_alls.pop.last
- end
- end
+ ancestors.each {|ancestor| ancestor.run_hook_unfiltered!(:after, :all, example) }
world.run_hook(:after, :all, self, example)
end
@@ -42,6 +42,24 @@ def hooks
}
end
+ def run_hook_unfiltered(hook, scope, example, options={})
+ if options[:reverse]
+ hooks[hook][scope].reverse.each { |arr| example.instance_eval(&arr.last) }
+ else
+ hooks[hook][scope].each { |arr| example.instance_eval(&arr.last) }
+ end
+ end
+
+ def run_hook_unfiltered!(hook, scope, example, options={})
+ until hooks[hook][scope].empty?
+ if options[:reverse]
+ example.instance_eval &hooks[hook][scope].shift.last
+ else
+ example.instance_eval &hooks[hook][scope].pop.last
+ end
+ end
+ end
+
def run_hook(hook, scope, group=nil, example=nil)
find_hook(hook, scope, group).each do |blk|
example ? example.instance_eval(&blk) : blk.call

0 comments on commit 5e05632

Please sign in to comment.