Permalink
Browse files

begin merging hook implementations

  • Loading branch information...
1 parent 28d492d commit 9edfa29a71028e1c5dad0b19283461b62f8361e9 @dchelimsky dchelimsky committed Jun 19, 2010
@@ -20,10 +20,10 @@ def run(err, out)
configuration.reporter.report(example_count) do |reporter|
begin
- configuration.run_before_suite
+ configuration.run_hook(:before, :suite)
example_groups.run_examples(reporter)
ensure
- configuration.run_after_suite
+ configuration.run_hook(:after, :suite)
end
end
@@ -3,6 +3,8 @@
module RSpec
module Core
class Configuration
+ include RSpec::Core::Hooks
+
def self.add_setting(name, opts={})
if opts[:alias]
alias_method name, opts[:alias]
@@ -85,13 +87,6 @@ def puts(message)
output_stream.puts(message)
end
- def hooks
- @hooks ||= {
- :before => { :each => [], :all => [], :suite => [] },
- :after => { :each => [], :all => [], :suite => [] }
- }
- end
-
def settings
@settings ||= {}
end
@@ -246,28 +241,6 @@ def find_modules(group)
end
end
- def before(scope=:each, options={}, &block)
- hooks[:before][scope] << [options, block]
- end
-
- def after(scope=:each, options={}, &block)
- hooks[:after][scope] << [options, block]
- end
-
- def run_before_suite
- hooks[:before][:suite].each {|hook| hook.last.call}
- end
-
- def run_after_suite
- hooks[:after][:suite].each {|hook| hook.last.call}
- end
-
- def find_hook(hook, each_or_all, group)
- hooks[hook][each_or_all].select do |filters, block|
- group.all_apply?(filters)
- end.map { |filters, block| block }
- end
-
def configure_mock_framework
require_mock_framework_adapter
RSpec::Core::ExampleGroup.send(:include, RSpec::Core::MockFrameworkAdapter)
@@ -140,36 +140,36 @@ def self.before_all_ivars
@before_all_ivars ||= {}
end
- def self.eval_before_alls(running_example)
+ def self.eval_before_alls(example)
return if descendant_filtered_examples.empty?
- superclass.before_all_ivars.each { |ivar, val| running_example.instance_variable_set(ivar, val) }
- world.run_hook(:before, :all, self, running_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?
- running_example.instance_eval &before_alls.shift
+ example.instance_eval &before_alls.shift.last
end
- running_example.instance_variables.each { |ivar| before_all_ivars[ivar] = running_example.instance_variable_get(ivar) }
+ example.instance_variables.each { |ivar| before_all_ivars[ivar] = example.instance_variable_get(ivar) }
end
- def self.eval_before_eachs(running_example)
- world.run_hook(:before, :each, self, running_example)
- ancestors.reverse.each { |ancestor| ancestor.before_eachs.each { |blk| running_example.instance_eval(&blk) } }
+ 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) } }
end
- def self.eval_after_eachs(running_example)
- ancestors.each { |ancestor| ancestor.after_eachs.reverse.each { |blk| running_example.instance_eval(&blk) } }
- world.run_hook(:after, :each, self, running_example)
+ def self.eval_after_eachs(example)
+ ancestors.each { |ancestor| ancestor.after_eachs.reverse.each { |arr| example.instance_eval(&arr.last) } }
+ world.run_hook(:after, :each, self, example)
end
- def self.eval_after_alls(running_example)
+ def self.eval_after_alls(example)
return if descendant_filtered_examples.empty?
- before_all_ivars.each { |ivar, val| running_example.instance_variable_set(ivar, val) }
+ before_all_ivars.each { |ivar, val| example.instance_variable_set(ivar, val) }
ancestors.each do |ancestor|
until ancestor.after_alls.empty?
- running_example.instance_eval &ancestor.after_alls.pop
+ example.instance_eval &ancestor.after_alls.pop.last
end
end
- world.run_hook(:after, :all, self, running_example)
+ world.run_hook(:after, :all, self, example)
end
def self.run(reporter)
@@ -1,49 +1,57 @@
module RSpec
module Core
module Hooks
- def before_blocks
- @before_blocks ||= { :all => [], :each => [] }
+ def before(scope=:each, options={}, &block)
+ hooks[:before][scope] << [options, block]
end
- def after_blocks
- @after_blocks ||= { :all => [], :each => [] }
- end
-
- def around_blocks
- @around_blocks ||= { :each => [] }
+ def after(scope=:each, options={}, &block)
+ hooks[:after][scope] << [options, block]
end
def before_eachs
- before_blocks[:each]
+ hooks[:before][:each]
end
def before_alls
- before_blocks[:all]
- end
-
- def before(type=:each, &block)
- before_blocks[type] << block
+ hooks[:before][:all]
end
def after_eachs
- after_blocks[:each]
+ hooks[:after][:each]
end
def after_alls
- after_blocks[:all]
- end
-
- def after(type=:each, &block)
- after_blocks[type] << block
+ hooks[:after][:all]
end
def around_eachs
- around_blocks[:each]
+ hooks[:around][:each]
end
- def around(type=:each, &block)
+ def around(scope=:each, &block)
RSpec::deprecate("around", "before and after")
- around_blocks[type] << block
+ hooks[:around][scope] << block
+ end
+
+ def hooks
+ @hooks ||= {
+ :around => { :each => [] },
+ :before => { :each => [], :all => [], :suite => [] },
+ :after => { :each => [], :all => [], :suite => [] }
+ }
+ 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
+ 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
end
@@ -67,15 +67,11 @@ def announce_inclusion_filter
end
end
end
-
- def run_hook(hook, scope, group, example)
- find_hook(hook, scope, group).each { |blk| example.instance_eval(&blk) }
- end
+
+ include RSpec::Core::Hooks
def find_hook(hook, scope, group)
- RSpec.configuration.hooks[hook][scope].select do |filters, block|
- group.all_apply?(filters)
- end.map { |filters, block| block }
+ RSpec.configuration.find_hook(hook, scope, group)
end
private
@@ -1,4 +1,5 @@
require "spec_helper"
+require "stringio"
module RSpec::Core
describe CommandLine do
@@ -29,31 +30,41 @@ module RSpec::Core
CommandLine.new(config_options)
end
- it "runs before suite hooks" do
- err = out = StringIO.new
- config = RSpec::Core::Configuration.new
- config.should_receive(:run_before_suite)
+ let(:config) do
+ RSpec::Core::Configuration.new
+ end
+
+ let(:out) { ::StringIO.new }
+
+ before do
command_line.stub(:configuration) { config }
- command_line.run(err, out)
+ config.stub(:run_hook)
+ end
+
+ it "runs before suite hooks" do
+ config.should_receive(:run_hook).with(:before, :suite)
+ command_line.run(out, out)
end
it "runs after suite hooks" do
- err = out = StringIO.new
- config = RSpec::Core::Configuration.new
- config.should_receive(:run_after_suite)
- command_line.stub(:configuration) { config }
- command_line.run(err, out)
+ config.should_receive(:run_hook).with(:after, :suite)
+ command_line.run(out, out)
end
it "runs after suite hooks even after an error" do
- err = out = StringIO.new
- config = RSpec::Core::Configuration.new
- config.stub(:run_before_suite) { raise "this error" }
- config.should_receive(:run_after_suite)
- command_line.stub(:configuration) { config }
+ after_suite_called = false
+ config.stub(:run_hook) do |*args|
+ case args.first
+ when :before
+ raise "this error"
+ when :after
+ after_suite_called = true
+ end
+ end
expect do
- command_line.run(err, out)
+ command_line.run(out, out)
end.to raise_error
+ after_suite_called.should be_true
end
end
@@ -401,27 +401,5 @@ def that_thing
end
end
end
-
- describe "#run_before_suite" do
- it "runs any before suite hooks" do
- calls = 0
- config.before(:suite) { calls += 1 }
- config.before(:suite) { calls += 1 }
- config.before(:suite) { calls += 1 }
- config.run_before_suite
- calls.should == 3
- end
- end
-
- describe "#run_after_suite" do
- it "runs any after suite hooks" do
- calls = 0
- config.after(:suite) { calls += 1 }
- config.after(:suite) { calls += 1 }
- config.after(:suite) { calls += 1 }
- config.run_after_suite
- calls.should == 3
- end
- end
end
end

0 comments on commit 9edfa29

Please sign in to comment.