Permalink
Browse files

get rid of the before_eachs (and others) delegator(s)

  • Loading branch information...
1 parent 182d5ba commit be8b82db95becc4aee671a0cc658b328b2e166fa @dchelimsky dchelimsky committed Jun 19, 2010
Showing with 31 additions and 47 deletions.
  1. +2 −2 lib/rspec/core/example.rb
  2. +8 −8 lib/rspec/core/example_group.rb
  3. +21 −37 lib/rspec/core/hooks.rb
@@ -43,10 +43,10 @@ def run(example_group_instance, reporter)
run_before_each
pending_declared_in_example = catch(:pending_declared_in_example) do
@in_block = true
- if @example_group_class.around_eachs.empty?
+ if @example_group_class.hooks[:around][:each].empty?
@example_group_instance.instance_eval(&example_block) unless pending
else
- @example_group_class.around_eachs.first.call(AroundProxy.new(self, &example_block))
+ @example_group_class.hooks[:around][:each].first.call(AroundProxy.new(self, &example_block))
end
throw :pending_declared_in_example, false
end
@@ -143,27 +143,27 @@ def self.before_all_ivars
def self.eval_before_alls(example)
return if descendant_filtered_examples.empty?
superclass.before_all_ivars.each { |ivar, val| example.instance_variable_set(ivar, val) }
- world.run_hook(:before, :all, self, example)
+ world.run_hook_filtered(:before, :all, self, example)
- run_hook_unfiltered!(:before, :all, example, :reverse => true)
+ run_hook!(: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.run_hook_unfiltered(:before, :each, example) }
+ world.run_hook_filtered(:before, :each, self, example)
+ ancestors.reverse.each { |ancestor| ancestor.run_hook(:before, :each, example) }
end
def self.eval_after_eachs(example)
- ancestors.each { |ancestor| ancestor.run_hook_unfiltered(:after, :each, example, :reverse => true) }
- world.run_hook(:after, :each, self, example)
+ ancestors.each { |ancestor| ancestor.run_hook(:after, :each, example, :reverse => true) }
+ world.run_hook_filtered(: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 {|ancestor| ancestor.run_hook_unfiltered!(:after, :all, example) }
- world.run_hook(:after, :all, self, example)
+ ancestors.each {|ancestor| ancestor.run_hook!(:after, :all, example) }
+ world.run_hook_filtered(:after, :all, self, example)
end
def self.run(reporter)
View
@@ -1,6 +1,14 @@
module RSpec
module Core
module Hooks
+ def hooks
+ @hooks ||= {
+ :around => { :each => [] },
+ :before => { :each => [], :all => [], :suite => [] },
+ :after => { :each => [], :all => [], :suite => [] }
+ }
+ end
+
def before(scope=:each, options={}, &block)
hooks[:before][scope] << [options, block]
end
@@ -9,48 +17,20 @@ def after(scope=:each, options={}, &block)
hooks[:after][scope] << [options, block]
end
- def before_eachs
- hooks[:before][:each]
- end
-
- def before_alls
- hooks[:before][:all]
- end
-
- def after_eachs
- hooks[:after][:each]
- end
-
- def after_alls
- hooks[:after][:all]
- end
-
- def around_eachs
- hooks[:around][:each]
- end
-
def around(scope=:each, &block)
RSpec::deprecate("around", "before and after")
hooks[:around][scope] << block
end
- def hooks
- @hooks ||= {
- :around => { :each => [] },
- :before => { :each => [], :all => [], :suite => [] },
- :after => { :each => [], :all => [], :suite => [] }
- }
- end
-
- def run_hook_unfiltered(hook, scope, example, options={})
+ def run_hook(hook, scope, example=nil, options={})
if options[:reverse]
- hooks[hook][scope].reverse.each { |arr| example.instance_eval(&arr.last) }
+ hooks[hook][scope].reverse.each &run_hook_in(example)
else
- hooks[hook][scope].each { |arr| example.instance_eval(&arr.last) }
+ hooks[hook][scope].each &run_hook_in(example)
end
end
- def run_hook_unfiltered!(hook, scope, example, options={})
+ def run_hook!(hook, scope, example, options={})
until hooks[hook][scope].empty?
if options[:reverse]
example.instance_eval &hooks[hook][scope].shift.last
@@ -60,16 +40,20 @@ def run_hook_unfiltered!(hook, scope, example, options={})
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
- end
+ def run_hook_filtered(hook, scope, group, example)
+ find_hook(hook, scope, group).each &run_hook_in(example)
end
def find_hook(hook, scope, group)
hooks[hook][scope].select do |filters, block|
!group || group.all_apply?(filters)
- end.map { |filters, block| block }
+ end
+ end
+
+ private
+
+ def run_hook_in(example)
+ lambda {|arr| example ? example.instance_eval(&arr.last) : arr.last.call}
end
end
end

0 comments on commit be8b82d

Please sign in to comment.