123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Core::Metadata::HashPopulator Private

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
RSpec::Core::Metadata::ExampleGroupHash, RSpec::Core::Metadata::ExampleHash
Inherits: Object
Defined in: rspec-core/lib/rspec/core/metadata.rb

Overview

Used internally to populate metadata hashes with computed keys managed by ::RSpec.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#block (readonly)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 118

attr_reader :, :, :description_args, :block

#description_args (readonly)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 118

attr_reader :, :, :description_args, :block

#metadata (readonly)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 118

attr_reader :, :, :description_args, :block

#user_metadata (readonly)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 118

attr_reader :, :, :description_args, :block

Instance Method Details

#build_description_from(parent_description = nil, my_description = nil) (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 178

def build_description_from(parent_description=nil, my_description=nil)
  return parent_description.to_s unless my_description
  return my_description.to_s if parent_description.to_s == ''
  separator = description_separator(parent_description, my_description)
  (parent_description.to_s + separator) << my_description.to_s
end

#build_scoped_id_for(file_path) (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 185

def build_scoped_id_for(file_path)
  index = @index_provider.call(file_path).to_s
  parent_scoped_id = .fetch(:scoped_id) { return index }
  "#{parent_scoped_id}:#{index}"
end

#description_separator(parent_part, child_part) (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 170

def description_separator(parent_part, child_part)
  if parent_part.is_a?(Module) && /^(?:#|::|\.)/.match(child_part.to_s)
    ''.freeze
  else
    ' '.freeze
  end
end

#ensure_valid_user_keys (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 191

def ensure_valid_user_keys
  RESERVED_KEYS.each do |key|
    next unless .key?(key)
    raise <<-EOM.gsub(/^\s+\|/, '')
      |#{"*" * 50}
      |:#{key} is not allowed
      |
      |RSpec reserves some hash keys for its own internal use,
      |including :#{key}, which is used on:
      |
      |  #{CallerFilter.first_non_rspec_line}.
      |
      |Here are all of RSpec's reserved hash keys:
      |
      |  #{RESERVED_KEYS.join("\n  ")}
      |#{"*" * 50}
    EOM
  end
end

#file_path_and_line_number_from(backtrace) (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 164

def file_path_and_line_number_from(backtrace)
  first_caller_from_outside_rspec = backtrace.find { |l| l !~ CallerFilter::LIB_REGEX }
  first_caller_from_outside_rspec ||= backtrace.first
  /(.+?):(\d+)(?:|:\d+)/.match(first_caller_from_outside_rspec).captures
end

#populate

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 128

def populate
  ensure_valid_user_keys

  [:block]            = block
  [:description_args] = description_args
  [:description]      = build_description_from(*[:description_args])
  [:full_description] = full_description
  [:described_class]  = described_class

  populate_location_attributes
  .update()
end

#populate_location_attributes (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 143

def populate_location_attributes
  backtrace = .delete(:caller)

  file_path, line_number = if backtrace
                             file_path_and_line_number_from(backtrace)
                           elsif block.respond_to?(:source_location)
                             block.source_location
                           else
                             file_path_and_line_number_from(caller)
                           end

  relative_file_path            = Metadata.relative_path(file_path)
  absolute_file_path            = File.expand_path(relative_file_path)
  [:file_path]          = relative_file_path
  [:line_number]        = line_number.to_i
  [:location]           = "#{relative_file_path}:#{line_number}"
  [:absolute_file_path] = absolute_file_path
  [:rerun_file_path]  ||= relative_file_path
  [:scoped_id]          = build_scoped_id_for(absolute_file_path)
end