123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Core::SharedExampleGroup::Registry Private

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: rspec-core/lib/rspec/core/shared_example_group.rb

Instance Method Summary

Instance Method Details

#add(context, name, *metadata_args, &block)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/shared_example_group.rb', line 150

def add(context, name, *, &block)
  unless block
    RSpec.warning "Shared example group #{name} was defined without a "\
                  "block and will have no effect. Please define a "\
                  "block or remove the definition."
  end

  if RSpec.configuration. == :trigger_inclusion
    return legacy_add(context, name, *, &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.build_hash_from()
  shared_module = SharedExampleGroupModule.new(name, block, )
  shared_example_groups[context][name] = shared_module
end

#ensure_block_has_source_location(block) (private)

:nocov:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/shared_example_group.rb', line 260

def ensure_block_has_source_location(_block); end

#find(lookup_contexts, name)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/shared_example_group.rb', line 174

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

#formatted_location(block) (private)

:nocov:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/shared_example_group.rb', line 250

def formatted_location(block)
  block.source_location.join(":")
end

#legacy_add(context, name, *metadata_args, &block) (private)

TODO: remove this in ::RSpec 4. This exists only to support ‘config.shared_context_metadata_behavior == :trigger_inclusion`, the legacy behavior of shared context metadata, which we do not want to support in ::RSpec 4.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/shared_example_group.rb', line 189

def legacy_add(context, name, *, &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
    .unshift name
  end

  return if .empty?
  RSpec.configuration.include shared_module, *
end

#shared_example_groups (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/shared_example_group.rb', line 204

def shared_example_groups
  @shared_example_groups ||= Hash.new { |hash, context| hash[context] = {} }
end

#valid_name?(candidate) ⇒ Boolean (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/shared_example_group.rb', line 208

def valid_name?(candidate)
  case candidate
  when String, Symbol, Module then true
  else false
  end
end

#warn_if_key_taken(context, key, new_block) (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/shared_example_group.rb', line 215

def warn_if_key_taken(context, key, new_block)
  existing_module = shared_example_groups[context][key]
  return unless existing_module

  old_definition_location = formatted_location existing_module.definition
  new_definition_location = formatted_location new_block
  loaded_spec_files = RSpec.configuration.loaded_spec_files

  if loaded_spec_files.include?(new_definition_location) && old_definition_location == new_definition_location
    RSpec.warn_with <<-WARNING.gsub(/^ +\|/, ''), :call_site => nil
      |WARNING: Your shared example group, '#{key}', defined at:
      | #{old_definition_location}
      |was automatically loaded by RSpec because the file name
      |matches the configured autoloading pattern (#{RSpec.configuration.pattern}),
      |and is also being required from somewhere else. To fix this
      |warning, either rename the file to not match the pattern, or
      |do not explicitly require the file.
    WARNING
  else
    RSpec.warn_with <<-WARNING.gsub(/^ +\|/, ''), :call_site => nil
      |WARNING: Shared example group '#{key}' has been previously defined at:
      |  #{old_definition_location}
      |...and you are now defining it at:
      |  #{new_definition_location}
      |The new definition will overwrite the original one.
    WARNING
  end
end