Skip to content

Commit

Permalink
ExampleGroup#alias_example_group_to
Browse files Browse the repository at this point in the history
  • Loading branch information
Michi Huber authored and myronmarston committed Jan 23, 2014
1 parent bb61140 commit acf7bad
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/rspec/core/configuration.rb
Expand Up @@ -663,6 +663,11 @@ def alias_example_to(new_name, *args)
RSpec::Core::ExampleGroup.alias_example_to(new_name, extra_options)
end

def alias_example_group_to(new_name, *args)
extra_options = build_metadata_hash_from(args)
RSpec::Core::ExampleGroup.alias_example_group_to(new_name, extra_options)
end

# Define an alias for it_should_behave_like that allows different
# language (like "it_has_behavior" or "it_behaves_like") to be
# employed when including shared examples.
Expand Down
8 changes: 8 additions & 0 deletions lib/rspec/core/example_group.rb
Expand Up @@ -124,6 +124,14 @@ def alias_example_to name, extra={}
(class << self; self; end).define_example_method name, extra
end

def alias_example_group_to(name, metadata={})
define_singleton_method(name) do |*args, &block|
combined_metadata = metadata.dup
combined_metadata.merge!(args.delete_at(-1)) if args.last.is_a? Hash
describe(*args, combined_metadata, &block)
end
end

# @private
# @macro [attach] define_nested_shared_group_method
#
Expand Down
24 changes: 24 additions & 0 deletions spec/rspec/core/configuration_spec.rb
Expand Up @@ -1281,6 +1281,30 @@ def host.foobar; end
end
end

describe "#alias_example_group_to" do
after do
RSpec::Core::ExampleGroup.module_eval do
class << self
undef :my_group_method if method_defined? :my_group_method
end
end
end

it_behaves_like "metadata hash builder" do
def metadata_hash(*args)
config.alias_example_group_to :my_group_method, *args
group = ExampleGroup.my_group_method("a group")
group.metadata
end
end

it "allows adding additional metadata" do
config.alias_example_group_to :my_group_method, { some: "thing" }
group = ExampleGroup.my_group_method("a group", another: "thing")
expect(group.metadata).to include(some: "thing", another: "thing")
end
end

describe "#alias_example_to" do
it_behaves_like "metadata hash builder" do
after do
Expand Down

0 comments on commit acf7bad

Please sign in to comment.