Skip to content
Browse files

changelog and cleanup for #503

  • Loading branch information...
1 parent 2273007 commit 96ef2031b2cdb0e603971e7d59769a8d9de6ebf1 @dchelimsky dchelimsky committed
Showing with 56 additions and 143 deletions.
  1. +1 −0 Changelog.md
  2. +6 −24 lib/rspec/core/example_group.rb
  3. +49 −119 spec/rspec/core/example_group_spec.rb
View
1 Changelog.md
@@ -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
View
30 lib/rspec/core/example_group.rb
@@ -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
@@ -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
View
168 spec/rspec/core/example_group_spec.rb
@@ -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|
@@ -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 })
@@ -999,7 +937,7 @@ 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
@@ -1007,20 +945,14 @@ def foo; end
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
@@ -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

0 comments on commit 96ef203

Please sign in to comment.
Something went wrong with that request. Please try again.