Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
changelog and cleanup for #503
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Nov 15, 2011
1 parent 2273007 commit 96ef203
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 143 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Enhancments
* merged with and deprecates `--configure` command, which generated
`.rspec`
* use `require_relative` when available (Ian Leitch)
* `include_context` and `include_examples` accept params (Calvin Bascom)

### 2.8.0.rc1 / 2011-11-06

Expand Down
30 changes: 6 additions & 24 deletions lib/rspec/core/example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def self.define_nested_shared_group_method(new_name, report_label=nil)
module_eval(<<-END_RUBY, __FILE__, __LINE__)
def self.#{new_name}(name, *args, &customization_block)
group = describe("#{report_label || "it should behave like"} \#{name}") do
find_and_execute_shared_block("examples", name, *args, &customization_block)
find_and_eval_shared("examples", name, *args, &customization_block)
end
group.metadata[:shared_group_name] = name
group
Expand All @@ -98,46 +98,28 @@ class << self
#
# @see SharedExampleGroup
def self.include_context(name, *args)
if block_given?
block_not_supported("context")
return
end

find_and_execute_shared_block("context", name, *args)
block_given? ? block_not_supported("context") : find_and_eval_shared("context", name, *args)
end

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

find_and_execute_shared_block("examples", name, *args)
block_given? ? block_not_supported("examples") : find_and_eval_shared("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
def self.find_and_eval_shared(label, name, *args, &customization_block)
raise ArgumentError, "Could not find shared #{label} #{name.inspect}" unless
shared_block = world.shared_example_groups[name]

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

def self.find_shared(label, name)
if world.shared_example_groups.has_key?(name)
world.shared_example_groups[name]
else
raise ArgumentError, "Could not find shared #{label} #{name.inspect}"
end
end

def self.examples
@examples ||= []
end
Expand Down
168 changes: 49 additions & 119 deletions spec/rspec/core/example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -894,89 +894,28 @@ def metadata_hash(*args)
end
end

describe "#include_context" do
before do
shared_context "named this" do
def foo; 'foo'; end
end
end

it "includes the named context" do
group = ExampleGroup.describe do
include_context "named this"
it "accesses foo" do
foo.should eq('foo')
end
end
group.run.should be_true
end

it "raises a helpful error message when shared context is not found" do
expect do
ExampleGroup.describe do
include_context "shared stuff"
end
end.to raise_error(ArgumentError,%q|Could not find shared context "shared stuff"|)
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
%w[include_examples include_context].each do |name|
describe "##{name}" do
before do
shared_examples "named this" do
example("does something") {}
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

describe "#include_examples" do
before do
shared_examples "named this" do
example("does something") do
end
it "includes the named examples" do
group = ExampleGroup.describe
group.send(name, "named this")
group.examples.first.description.should eq("does something")
end
end

it "includes the named examples" do
group = ExampleGroup.describe do
include_examples "named this"
it "raises a helpful error message when shared content is not found" do
group = ExampleGroup.describe
expect do
group.send(name, "shared stuff")
end.to raise_error(ArgumentError, /Could not find .* "shared stuff"/)
end
group.examples.first.description.should eq("does something")
end

it "raises a helpful error message when shared context is not found" do
expect do
ExampleGroup.describe do
include_examples "shared stuff"
end
end.to raise_error(ArgumentError,%q|Could not find shared examples "shared stuff"|)
end

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

shared_examples "named this with params" do |param1, param2|
Expand All @@ -986,9 +925,8 @@ def foo; 'foo'; end
end
end

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

passed_params.should eq({ :param1 => :value1, :param2 => :value2 })
Expand All @@ -999,28 +937,22 @@ def foo; 'foo'; end
def foo; end
end
group = ExampleGroup.describe('fake group')
group.include_examples("named this with params", :a)
group.send(name, "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)
group.send(name, "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
it "warns the user that blocks are not supported when given a block" do
group = ExampleGroup.describe
group.should_receive(:warn).with(/blocks not supported for #{name}/)
group.send(name, "named this with block") {}
end
end
end
Expand Down Expand Up @@ -1068,41 +1000,39 @@ def self.foo; end
shared_group.methods.map{|m| m.to_s}.should include("foo")
end

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

shared_examples_for("thing") do |param1, param2|
it("has access to the given parameters") do
passed_params[:param1] = param1
passed_params[:param2] = param2
end
end
it "passes parameters to the shared example group" do
passed_params = {}

group = ExampleGroup.describe("group") do
it_should_behave_like "thing", :value1, :value2
shared_examples_for("thing") do |param1, param2|
it("has access to the given parameters") do
passed_params[:param1] = param1
passed_params[:param2] = param2
end
group.run

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

it "adds shared instance methods to nested group" do
shared_examples_for("thing") do |param1|
def foo; end
end
group = ExampleGroup.describe('fake group')
shared_group = group.it_should_behave_like("thing", :a)
shared_group.public_instance_methods.map{|m| m.to_s}.should include("foo")
group = ExampleGroup.describe("group") do
it_should_behave_like "thing", :value1, :value2
end
group.run

it "evals the shared example group only once" do
eval_count = 0
shared_examples_for("thing") { |p| eval_count += 1 }
group = ExampleGroup.describe('fake group')
group.it_should_behave_like("thing", :a)
eval_count.should eq(1)
passed_params.should eq({ :param1 => :value1, :param2 => :value2 })
end

it "adds shared instance methods to nested group" do
shared_examples_for("thing") do |param1|
def foo; end
end
group = ExampleGroup.describe('fake group')
shared_group = group.it_should_behave_like("thing", :a)
shared_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_for("thing") { |p| eval_count += 1 }
group = ExampleGroup.describe('fake group')
group.it_should_behave_like("thing", :a)
eval_count.should eq(1)
end

context "given a block" do
Expand Down

0 comments on commit 96ef203

Please sign in to comment.