Skip to content

Commit

Permalink
Don't include or extend when ancestor group is already extended.
Browse files Browse the repository at this point in the history
- Fixes #576.
  • Loading branch information
dchelimsky committed Mar 4, 2012
1 parent 5dc1872 commit 0ecf872
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/rspec/core/configuration.rb
Expand Up @@ -709,10 +709,20 @@ def extend(mod, *filters)
def configure_group(group) def configure_group(group)
include_or_extend_modules.each do |include_or_extend, mod, filters| include_or_extend_modules.each do |include_or_extend, mod, filters|
next unless filters.empty? || group.any_apply?(filters) next unless filters.empty? || group.any_apply?(filters)
group.send(include_or_extend, mod) send("safe_#{include_or_extend}", mod, group)
end end
end end


# @private
def safe_include(mod, host)
host.send(:include,mod) unless host < mod
end

# @private
def safe_extend(mod, host)
host.extend(mod) unless (class << host; self; end) < mod
end

# @private # @private
def configure_mock_framework def configure_mock_framework
RSpec::Core::ExampleGroup.send(:include, mock_framework) RSpec::Core::ExampleGroup.send(:include, mock_framework)
Expand Down
32 changes: 32 additions & 0 deletions spec/rspec/core/configuration_spec.rb
Expand Up @@ -1001,6 +1001,38 @@ def self.included(host)
group.included_modules.should include(mod1) group.included_modules.should include(mod1)
group.included_modules.should include(mod2) group.included_modules.should include(mod2)
end end

module IncludeOrExtendMeOnce
def self.included(host)
raise "included again" if host.instance_methods.include?(:foobar)
host.class_eval { def foobar; end }
end

def self.extended(host)
raise "extended again" if host.respond_to?(:foobar)
def host.foobar; end
end
end

it "doesn't include a module when already included in ancestor" do
config.include(IncludeOrExtendMeOnce, :foo => :bar)

group = ExampleGroup.describe("group", :foo => :bar)
child = group.describe("child")

config.configure_group(group)
config.configure_group(child)
end

it "doesn't extend when ancestor is already extended with same module" do
config.extend(IncludeOrExtendMeOnce, :foo => :bar)

group = ExampleGroup.describe("group", :foo => :bar)
child = group.describe("child")

config.configure_group(group)
config.configure_group(child)
end
end end


describe "#alias_example_to" do describe "#alias_example_to" do
Expand Down

0 comments on commit 0ecf872

Please sign in to comment.