Permalink
Browse files

Don't include or extend when ancestor group is already extended.

- Fixes #576.
  • Loading branch information...
dchelimsky committed Mar 4, 2012
1 parent 5dc1872 commit 0ecf8721ebd7be698c27247ca08b09c96f46c04d
Showing with 43 additions and 1 deletion.
  1. +11 −1 lib/rspec/core/configuration.rb
  2. +32 −0 spec/rspec/core/configuration_spec.rb
@@ -709,10 +709,20 @@ def extend(mod, *filters)
def configure_group(group)
include_or_extend_modules.each do |include_or_extend, mod, filters|
next unless filters.empty? || group.any_apply?(filters)
- group.send(include_or_extend, mod)
+ send("safe_#{include_or_extend}", mod, group)
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
def configure_mock_framework
RSpec::Core::ExampleGroup.send(:include, mock_framework)
@@ -1001,6 +1001,38 @@ def self.included(host)
group.included_modules.should include(mod1)
group.included_modules.should include(mod2)
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
describe "#alias_example_to" do

0 comments on commit 0ecf872

Please sign in to comment.