class RSpec::Core::SharedExampleGroup::Registry
@private
def add(context, name, *metadata_args, &block)
def add(context, name, *metadata_args, &block) if RSpec.configuration.shared_context_metadata_behavior == :trigger_inclusion return legacy_add(context, name, *metadata_args, &block) end unless valid_name?(name) raise ArgumentError, "Shared example group names can only be a string, " \ "symbol or module but got: #{name.inspect}" end ensure_block_has_source_location(block) { CallerFilter.first_non_rspec_line } warn_if_key_taken context, name, block metadata = Metadata.build_hash_from(metadata_args) shared_module = SharedExampleGroupModule.new(name, block, metadata) shared_example_groups[context][name] = shared_module end
def ensure_block_has_source_location(_block); end
def ensure_block_has_source_location(_block); end
def ensure_block_has_source_location(block)
for 1.8.7
def ensure_block_has_source_location(block) source_location = yield.split(':') block.extend Module.new { define_method(:source_location) { source_location } } end
def find(lookup_contexts, name)
def find(lookup_contexts, name) lookup_contexts.each do |context| found = shared_example_groups[context][name] return found if found end shared_example_groups[:main][name] end
def formatted_location(block)
def formatted_location(block) block.source_location.join ":" end
def legacy_add(context, name, *metadata_args, &block)
the legacy behavior of shared context metadata, which we do
`config.shared_context_metadata_behavior == :trigger_inclusion`,
TODO: remove this in RSpec 4. This exists only to support
def legacy_add(context, name, *metadata_args, &block) ensure_block_has_source_location(block) { CallerFilter.first_non_rspec_line } shared_module = SharedExampleGroupModule.new(name, block, {}) if valid_name?(name) warn_if_key_taken context, name, block shared_example_groups[context][name] = shared_module else metadata_args.unshift name end return if metadata_args.empty? RSpec.configuration.include shared_module, *metadata_args end
def shared_example_groups
def shared_example_groups @shared_example_groups ||= Hash.new { |hash, context| hash[context] = {} } end
def valid_name?(candidate)
def valid_name?(candidate) case candidate when String, Symbol, Module then true else false end end
def warn_if_key_taken(context, key, new_block)
def warn_if_key_taken(context, key, new_block) existing_module = shared_example_groups[context][key] return unless existing_module RSpec.warn_with <<-WARNING.gsub(/^ +\|/, ''), :call_site => nil |WARNING: Shared example group '#{key}' has been previously defined at: | #{formatted_location existing_module.definition} |...and you are now defining it at: | #{formatted_location new_block} |The new definition will overwrite the original one. WARNING end