Permalink
Browse files

Expose example.metadata in around hooks

- Closes #221.
  • Loading branch information...
1 parent 2466831 commit b6a7897ef7e105a77984db2a0ece81cf967678a2 @dchelimsky dchelimsky committed Nov 19, 2010
Showing with 32 additions and 7 deletions.
  1. +18 −2 features/hooks/around_hooks.feature
  2. +9 −1 lib/rspec/core/example.rb
  3. +5 −4 lib/rspec/core/example_group.rb
@@ -9,7 +9,7 @@ Feature: around hooks
example, if your database library offers a transaction method that receives
a block, you can use an around hook as described in the first scenario:
- Scenario: use the example as a block within the block passed to around()
+ Scenario: use the example as a proc within the block passed to around()
Given a file named "example_spec.rb" with:
"""
class Database
@@ -41,7 +41,7 @@ Feature: around hooks
Scenario: invoke the example using run()
Given a file named "example_spec.rb" with:
"""
- describe "around filter" do
+ describe "around hook" do
around(:each) do |example|
puts "around each before"
example.run
@@ -61,6 +61,22 @@ Feature: around hooks
around each after
"""
+ Scenario: access the example metadata
+ Given a file named "example_spec.rb" with:
+ """
+ describe "something" do
+ around(:each) do |example|
+ puts example.metadata[:foo]
+ example.run
+ end
+
+ it "does something", :foo => "this should show up in the output" do
+ end
+ end
+ """
+ When I run "rspec example_spec.rb"
+ Then the output should contain "this should show up in the output"
+
Scenario: define a global around hook
Given a file named "example_spec.rb" with:
"""
View
@@ -66,6 +66,14 @@ def fail_fast(reporter, exception)
finish(reporter)
end
+ class Procsy < Proc
+ attr_reader :metadata
+ alias_method :run, :call
+ def initialize(metadata)
+ @metadata = metadata
+ end
+ end
+
private
def with_pending_capture
@@ -79,7 +87,7 @@ def with_around_hooks
if @example_group_class.around_hooks.empty?
yield
else
- @example_group_class.eval_around_eachs(@example_group_instance, Proc.new).call
+ @example_group_class.eval_around_eachs(@example_group_instance, Procsy.new(metadata)).call
end
end
@@ -177,10 +177,11 @@ def self.eval_before_alls(example_group_instance)
store_before_all_ivars(example_group_instance)
end
- def self.eval_around_eachs(example_group_instance, wrapped_example)
- around_hooks.reverse.inject(wrapped_example) do |wrapper, hook|
- def wrapper.run; call; end
- lambda { example_group_instance.instance_eval_with_args(wrapper, &hook) }
+ def self.eval_around_eachs(example_group_instance, procsy)
+ around_hooks.reverse.inject(procsy) do |procsy, around_hook|
+ Example::Procsy.new(procsy.metadata) do
+ example_group_instance.instance_eval_with_args(procsy, &around_hook)
+ end
end
end

0 comments on commit b6a7897

Please sign in to comment.