Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

yield current running example to it/example and before/after hooks

  • Loading branch information...
commit 478a8e45bce244148eec4f45166d42ae9d6980dc 1 parent 8752b9a
@dchelimsky dchelimsky authored
View
4 lib/rspec/core/example.rb
@@ -110,7 +110,7 @@ def run(example_group_instance, reporter)
with_around_each_hooks do
begin
run_before_each
- @example_group_instance.instance_eval(&@example_block)
+ @example_group_instance.instance_eval_with_args(self, &@example_block)
rescue Pending::PendingDeclaredInExample => e
@pending_declared_in_example = e.message
rescue Exception => e
@@ -238,7 +238,7 @@ def instance_eval(&block)
# @private
def instance_eval_with_rescue(context = nil, &block)
- @example_group_instance.instance_eval_with_rescue(context, &block)
+ @example_group_instance.instance_eval_with_rescue(self, context, &block)
end
# @private
View
21 lib/rspec/core/example_group.rb
@@ -429,12 +429,19 @@ def self.set_ivars(instance, ivars)
# @attr_reader
# Returns the {Example} object that wraps this instance of
# `ExampleGroup`
- attr_accessor :example
+ def example
+ RSpec.deprecate("example", "a block arg")
+ @_current_rspec_example
+ end
+
+ def example=(example)
+ @_current_rspec_example = example
+ end
# @deprecated use {ExampleGroup#example}
def running_example
- RSpec.deprecate("running_example", "example")
- example
+ RSpec.deprecate("running_example", "a block arg")
+ @_current_rspec_example
end
# Returns the class or module passed to the `describe` method (or alias).
@@ -454,12 +461,12 @@ def described_class
# @private
# instance_evals the block, capturing and reporting an exception if
# raised
- def instance_eval_with_rescue(context = nil, &hook)
+ def instance_eval_with_rescue(example, context = nil, &hook)
begin
- instance_eval(&hook)
+ instance_eval_with_args(example, &hook)
rescue Exception => e
- raise unless example
- example.set_exception(e, context)
+ raise unless @_current_rspec_example
+ @_current_rspec_example.set_exception(e, context)
end
end
end
View
34 lib/rspec/core/hooks.rb
@@ -20,7 +20,19 @@ module BeforeHookExtension
include HookExtension
def run(example)
- example.instance_eval(&self)
+ example.instance_eval_with_args(example, &self)
+ end
+
+ def display_name
+ "before hook"
+ end
+ end
+
+ module BeforeAllExtension
+ include HookExtension
+
+ def run(example)
+ example.instance_eval_with_args(nil, &self)
end
def display_name
@@ -40,6 +52,18 @@ def display_name
end
end
+ module AfterAllExtension
+ include HookExtension
+
+ def run(example)
+ example.instance_eval_with_rescue(nil, &self)
+ end
+
+ def display_name
+ "after hook"
+ end
+ end
+
module AroundHookExtension
include HookExtension
@@ -434,9 +458,9 @@ def around_each_hooks_for(example, initial_procsy=nil)
SCOPES = [:each, :all, :suite]
EXTENSIONS = {
- :before => BeforeHookExtension,
- :after => AfterHookExtension,
- :around => AroundHookExtension
+ :before => { :each => BeforeHookExtension, :all => BeforeAllExtension, :suite => BeforeHookExtension },
+ :after => { :each => AfterHookExtension, :all => AfterAllExtension, :suite => AfterHookExtension },
+ :around => { :each => AroundHookExtension, :all => AroundHookExtension, :suite => AroundHookExtension }
}
def before_all_hooks_for(group)
@@ -457,7 +481,7 @@ def after_each_hooks_for(example)
def register_hook prepend_or_append, hook, *args, &block
scope, options = scope_and_options_from(*args)
- hooks[hook][scope].send(prepend_or_append, block.extend(EXTENSIONS[hook]).with(options))
+ hooks[hook][scope].send(prepend_or_append, block.extend(EXTENSIONS[hook][scope]).with(options))
end
def find_hook(hook, scope, example_or_group, initial_procsy)
View
12 lib/rspec/core/pending.rb
@@ -69,7 +69,7 @@ def pending_fixed?; true; end
# # ...
# end
def pending(*args)
- return self.class.before(:each) { pending(*args) } unless example
+ return self.class.before(:each) { pending(*args) } unless @_current_rspec_example
options = args.last.is_a?(Hash) ? args.pop : {}
message = args.first || NO_REASON_GIVEN
@@ -78,17 +78,17 @@ def pending(*args)
return block_given? ? yield : nil
end
- example.metadata[:pending] = true
- example.metadata[:execution_result][:pending_message] = message
+ @_current_rspec_example.metadata[:pending] = true
+ @_current_rspec_example.metadata[:execution_result][:pending_message] = message
if block_given?
begin
result = begin
yield
- example.example_group_instance.instance_eval { verify_mocks_for_rspec }
+ @_current_rspec_example.example_group_instance.instance_eval { verify_mocks_for_rspec }
end
- example.metadata[:pending] = false
+ @_current_rspec_example.metadata[:pending] = false
rescue Exception => e
- example.execution_result[:exception] = e
+ @_current_rspec_example.execution_result[:exception] = e
ensure
teardown_mocks_for_rspec
end
View
20 spec/rspec/core/deprecations_spec.rb
@@ -23,16 +23,18 @@
end
end
- describe RSpec::Core::ExampleGroup do
- describe 'running_example' do
- it 'is deprecated' do
- RSpec.should_receive(:warn_deprecation)
- self.running_example
- end
+ %w[example running_example].each do |example_accessor|
+ describe RSpec::Core::ExampleGroup do
+ describe example_accessor do
+ it 'is deprecated' do
+ RSpec.should_receive(:warn_deprecation)
+ send(example_accessor)
+ end
- it "delegates to example" do
- RSpec.stub(:warn_deprecation)
- running_example.should eq(example)
+ it "delegates to yielded example" do |ex|
+ RSpec.stub(:warn_deprecation)
+ send(example_accessor).should eq(ex)
+ end
end
end
end
View
48 spec/rspec/core/example_group_spec.rb
@@ -57,8 +57,8 @@ def metadata_hash(*args)
examples_run = []
group = ExampleGroup.describe("parent") do
describe("child") do
- it "does something" do
- examples_run << example
+ it "does something" do |ex|
+ examples_run << ex
end
end
end
@@ -71,13 +71,13 @@ def metadata_hash(*args)
it "runs its children " do
examples_run = []
group = ExampleGroup.describe("parent") do
- it "fails" do
- examples_run << example
+ it "fails" do |ex|
+ examples_run << ex
raise "fail"
end
describe("child") do
- it "does something" do
- examples_run << example
+ it "does something" do |ex|
+ examples_run << ex
end
end
end
@@ -596,19 +596,19 @@ def metadata_hash(*args)
end
end
- it "has no 'running example' within before(:all)" do
+ it "yields no example to before(:all)" do
group = ExampleGroup.describe
running_example = :none
- group.before(:all) { running_example = example }
+ group.before(:all) {|ex| running_example = ex}
group.example("no-op") { }
group.run
- running_example.should be(nil)
+ running_example.should be_nil
end
it "has access to example options within before(:each)" do
group = ExampleGroup.describe
option = nil
- group.before(:each) { option = example.options[:data] }
+ group.before(:each) {|ex| option = ex.options[:data] }
group.example("no-op", :data => :sample) { }
group.run
option.should eq(:sample)
@@ -617,7 +617,7 @@ def metadata_hash(*args)
it "has access to example options within after(:each)" do
group = ExampleGroup.describe
option = nil
- group.after(:each) { option = example.options[:data] }
+ group.after(:each) {|ex| option = ex.options[:data] }
group.example("no-op", :data => :sample) { }
group.run
option.should eq(:sample)
@@ -626,7 +626,7 @@ def metadata_hash(*args)
it "has no 'running example' within after(:all)" do
group = ExampleGroup.describe
running_example = :none
- group.after(:all) { running_example = example }
+ group.after(:all) {|ex| running_example = ex }
group.example("no-op") { }
group.run
running_example.should be(nil)
@@ -688,20 +688,20 @@ def metadata_hash(*args)
describe Object, "describing nested example_groups", :little_less_nested => 'yep' do
describe "A sample nested group", :nested_describe => "yep" do
- it "sets the described class to the described class of the outer most group" do
- example.example_group.described_class.should eq(ExampleGroup)
+ it "sets the described class to the described class of the outer most group" do |ex|
+ ex.example_group.described_class.should eq(ExampleGroup)
end
- it "sets the description to 'A sample nested describe'" do
- example.example_group.description.should eq('A sample nested group')
+ it "sets the description to 'A sample nested describe'" do |ex|
+ ex.example_group.description.should eq('A sample nested group')
end
- it "has top level metadata from the example_group and its parent groups" do
- example.example_group.metadata.should include(:little_less_nested => 'yep', :nested_describe => 'yep')
+ it "has top level metadata from the example_group and its parent groups" do |ex|
+ ex.example_group.metadata.should include(:little_less_nested => 'yep', :nested_describe => 'yep')
end
- it "exposes the parent metadata to the contained examples" do
- example.metadata.should include(:little_less_nested => 'yep', :nested_describe => 'yep')
+ it "exposes the parent metadata to the contained examples" do |ex|
+ ex.metadata.should include(:little_less_nested => 'yep', :nested_describe => 'yep')
end
end
@@ -759,12 +759,12 @@ def metadata_hash(*args)
@before_all_top_level.should eq('before_all_top_level')
end
- it "can access the before all ivars in the before_all_ivars hash", :ruby => 1.8 do
- example.example_group.before_all_ivars.should include('@before_all_top_level' => 'before_all_top_level')
+ it "can access the before all ivars in the before_all_ivars hash", :ruby => 1.8 do |ex|
+ ex.example_group.before_all_ivars.should include('@before_all_top_level' => 'before_all_top_level')
end
- it "can access the before all ivars in the before_all_ivars hash", :ruby => 1.9 do
- example.example_group.before_all_ivars.should include(:@before_all_top_level => 'before_all_top_level')
+ it "can access the before all ivars in the before_all_ivars hash", :ruby => 1.9 do |ex|
+ ex.example_group.before_all_ivars.should include(:@before_all_top_level => 'before_all_top_level')
end
describe "but now I am nested" do
View
22 spec/rspec/core/example_spec.rb
@@ -128,19 +128,19 @@ def assert(val)
end
describe "accessing metadata within a running example" do
- it "has a reference to itself when running" do
- example.description.should eq("has a reference to itself when running")
+ it "has a reference to itself when running" do |ex|
+ ex.description.should eq("has a reference to itself when running")
end
- it "can access the example group's top level metadata as if it were its own" do
- example.example_group.metadata.should include(:parent_metadata => 'sample')
- example.metadata.should include(:parent_metadata => 'sample')
+ it "can access the example group's top level metadata as if it were its own" do |ex|
+ ex.example_group.metadata.should include(:parent_metadata => 'sample')
+ ex.metadata.should include(:parent_metadata => 'sample')
end
end
describe "accessing options within a running example" do
- it "can look up option values by key", :demo => :data do
- example.metadata[:demo].should eq(:data)
+ it "can look up option values by key", :demo => :data do |ex|
+ ex.metadata[:demo].should eq(:data)
end
end
@@ -328,7 +328,7 @@ def run_and_capture_reported_message(group)
blah.should be(:success)
end
end
-
+
context "in before(:each)" do
it "sets each example to pending" do
group = RSpec::Core::ExampleGroup.describe do
@@ -365,6 +365,12 @@ def run_and_capture_reported_message(group)
group.examples.first.should be_pending
end
end
+ end
+ describe "optional block argument" do
+ it "contains the example" do |ex|
+ expect(ex).to be_an(RSpec::Core::Example)
+ expect(ex.description).to match(/contains the example/)
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.