Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for passing parameters and a block through include_examples #503

Merged
merged 4 commits into from Nov 15, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 27 additions & 9 deletions lib/rspec/core/example_group.rb
Expand Up @@ -77,12 +77,8 @@ class << self
def self.define_nested_shared_group_method(new_name, report_label=nil) def self.define_nested_shared_group_method(new_name, report_label=nil)
module_eval(<<-END_RUBY, __FILE__, __LINE__) module_eval(<<-END_RUBY, __FILE__, __LINE__)
def self.#{new_name}(name, *args, &customization_block) def self.#{new_name}(name, *args, &customization_block)
shared_block = find_shared("examples", name)
raise "Could not find shared examples \#{name.inspect}" unless shared_block

group = describe("#{report_label || "it should behave like"} \#{name}") do group = describe("#{report_label || "it should behave like"} \#{name}") do
module_eval_with_args(*args, &shared_block) find_and_execute_shared_block("examples", name, *args, &customization_block)
module_eval(&customization_block) if customization_block
end end
group.metadata[:shared_group_name] = name group.metadata[:shared_group_name] = name
group group
Expand All @@ -101,15 +97,37 @@ class << self
# Includes shared content declared with `name`. # Includes shared content declared with `name`.
# #
# @see SharedExampleGroup # @see SharedExampleGroup
def self.include_context(name) def self.include_context(name, *args)
module_eval(&find_shared("context", name)) if block_given?
block_not_supported("context")
return
end

find_and_execute_shared_block("context", name, *args)
end end


# Includes shared content declared with `name`. # Includes shared content declared with `name`.
# #
# @see SharedExampleGroup # @see SharedExampleGroup
def self.include_examples(name) def self.include_examples(name, *args)
module_eval(&find_shared("examples", name)) if block_given?
block_not_supported("examples")
return
end

find_and_execute_shared_block("examples", name, *args)
end

def self.block_not_supported(label)
warn("Customization blocks not supported for include_#{label}. Use it_behaves_like instead.")
end

def self.find_and_execute_shared_block(label, name, *args, &customization_block)
shared_block = find_shared(label, name)
raise "Could not find shared #{label} #{name.inspect}" unless shared_block

module_eval_with_args(*args, &shared_block)
module_eval(&customization_block) if customization_block
end end


def self.find_shared(label, name) def self.find_shared(label, name)
Expand Down
80 changes: 80 additions & 0 deletions spec/rspec/core/example_group_spec.rb
Expand Up @@ -919,6 +919,37 @@ def foo; 'foo'; end
end.to raise_error(ArgumentError,%q|Could not find shared context "shared stuff"|) end.to raise_error(ArgumentError,%q|Could not find shared context "shared stuff"|)
end end


context "given some parameters" do
it "passes the parameters to the shared context" do
passed_params = {}

shared_context "named this with params" do |param1, param2|
it("has access to the given parameters") do
passed_params[:param1] = param1
passed_params[:param2] = param2
end
end

group = ExampleGroup.describe do
include_context "named this with params", :value1, :value2
end
group.run

passed_params.should eq({ :param1 => :value1, :param2 => :value2 })
end
end

context "given a block" do
it "warns the user that blocks are not supported" do
group = ExampleGroup.describe do
self.should_receive(:warn).with(/blocks not supported for include_context/)
include_context "named this with block" do
true
end
end
group.run
end
end
end end


describe "#include_examples" do describe "#include_examples" do
Expand All @@ -943,6 +974,55 @@ def foo; 'foo'; end
end end
end.to raise_error(ArgumentError,%q|Could not find shared examples "shared stuff"|) end.to raise_error(ArgumentError,%q|Could not find shared examples "shared stuff"|)
end end

context "given some parameters" do
it "passes the parameters to the named examples" do
passed_params = {}

shared_examples "named this with params" do |param1, param2|
it("has access to the given parameters") do
passed_params[:param1] = param1
passed_params[:param2] = param2
end
end

group = ExampleGroup.describe do
include_examples "named this with params", :value1, :value2
end
group.run

passed_params.should eq({ :param1 => :value1, :param2 => :value2 })
end

it "adds shared instance methods to the group" do
shared_examples "named this with params" do |param1|
def foo; end
end
group = ExampleGroup.describe('fake group')
group.include_examples("named this with params", :a)
group.public_instance_methods.map{|m| m.to_s}.should include("foo")
end

it "evals the shared example group only once" do
eval_count = 0
shared_examples("named this with params") { |p| eval_count += 1 }
group = ExampleGroup.describe('fake group')
group.include_examples("named this with params", :a)
eval_count.should eq(1)
end
end

context "given a block" do
it "warns the user that blocks are not supported" do
group = ExampleGroup.describe do
self.should_receive(:warn).with(/blocks not supported for include_examples/)
include_examples "named this with block" do
true
end
end
group.run
end
end
end end


describe "#it_should_behave_like" do describe "#it_should_behave_like" do
Expand Down