Skip to content

Commit

Permalink
Raise error when passing params to shared example groups in Ruby 1.8.6.
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Aug 3, 2010
1 parent 8430361 commit 3cea7b8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
1 change: 1 addition & 0 deletions features/example_groups/shared_example_group.feature
Expand Up @@ -100,6 +100,7 @@ Feature: Shared example group
adds objects to the end of the collection
"""

@wip
Scenario: Passing parameters to a shared example group
Given a file named "shared_example_group_params_spec.rb" with:
"""
Expand Down
14 changes: 14 additions & 0 deletions lib/rspec/core/example_group.rb
Expand Up @@ -59,6 +59,20 @@ class << self
alias_example_to :focused, :focused => true
alias_example_to :pending, :pending => true

class << self
unless respond_to?(:module_exec)
def module_exec(*args, &block)
unless args.empty?
raise <<-MSG
RSpec only supports parameterized shared groups with Ruby versions of Ruby that
support module_exec. You are using Ruby #{RUBY_VERSION}, which does not.
MSG
end
module_eval(&block)
end
end
end

def self.define_shared_group_method(new_name, report_label=nil)
module_eval(<<-END_RUBY, __FILE__, __LINE__)
def self.#{new_name}(name, *args, &customization_block)
Expand Down
36 changes: 25 additions & 11 deletions spec/rspec/core/shared_example_group_spec.rb
Expand Up @@ -81,22 +81,36 @@ def self.foo; end
end

context "given some parameters" do
it "passes the parameters to the shared example group" do
passed_params = {}
context "running with Ruby 1.8.7 or better", :ruby => "> 1.8.6" 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

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

group = ExampleGroup.describe("group") do
it_should_behave_like "thing", :value1, :value2
passed_params.should == { :param1 => :value1, :param2 => :value2 }
end
group.run_all
end

context "running with Ruby 1.8.6 or better", :ruby => "1.8.6" do
it "raises an error with a friendly message" do
shared_examples_for("thing") {}

passed_params.should == { :param1 => :value1, :param2 => :value2 }
lambda do
ExampleGroup.describe("group") do
it_should_behave_like "thing", :value1, :value2
end
end.should raise_error(/^RSpec only supports parameterized shared groups/)
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -62,6 +62,8 @@ def in_editor?
case version.to_s
when "!jruby"
RUBY_ENGINE != "jruby"
when /^> (.*)/
!(RUBY_VERSION.to_s > $1)
else
!(RUBY_VERSION.to_s =~ /^#{version.to_s}/)
end
Expand Down

0 comments on commit 3cea7b8

Please sign in to comment.