Module: RSpec::Core::Metadata
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Defined in: | rspec-core/lib/rspec/core/metadata.rb |
Overview
Each ExampleGroup class and Example
instance owns an instance of Metadata
, which is Hash extended to support lazy evaluation of values associated with keys that may or may not be used by any example or group.
In addition to metadata that is used internally, this also stores user-supplied metadata, e.g.
RSpec.describe Something, :type => :ui do
it "does something", :slow => true do
# ...
end
end
‘:type => :ui` is stored in the Metadata
owned by the example group, and `:slow => true` is stored in the Metadata
owned by the example. These can then be used to select which examples are run using the –tag
option on the command line, or several methods on Configuration
used to filter a run (e.g. filter_run_including
, filter_run_excluding
, etc).
Constant Summary
-
RESERVED_KEYS =
Internal use only
# File 'rspec-core/lib/rspec/core/metadata.rb', line 325[ :description, :description_args, :described_class, :example_group, :parent_example_group, :execution_result, :last_run_status, :file_path, :absolute_file_path, :rerun_file_path, :full_description, :line_number, :location, :scoped_id, :block, :shared_group_inclusion_backtrace ]
Class Method Summary
-
.ascend(metadata)
Internal use only
Internal use only
Returns an enumerator that iteratively walks up the given metadata through all example group ancestors, yielding each metadata hash along the way.
-
.ascending(metadata) {|metadata| ... }
Internal use only
Internal use only
Iteratively walks up from the given metadata through all example group ancestors, yielding each metadata hash along the way.
-
.build_hash_from(args, warn_about_example_group_filtering = false)
Internal use only
Internal use only
Used internally to build a hash from an args array.
- .deep_hash_dup(object) Internal use only Internal use only
- .id_from(metadata) Internal use only Internal use only
- .location_tuple_from(metadata) Internal use only Internal use only
- .relative_path(line) ⇒ String Internal use only Internal use only
-
.relative_path_regex
Matches strings either at the beginning of the input or prefixed with a whitespace, containing the current path, either postfixed with the separator, or at the end of the string.
Class Method Details
.ascend(metadata)
Returns an enumerator that iteratively walks up the given metadata through all example group ancestors, yielding each metadata hash along the way.
# File 'rspec-core/lib/rspec/core/metadata.rb', line 71
def self.ascend( ) enum_for(:ascending, ) end
.ascending(metadata) {|metadata| ... }
Iteratively walks up from the given metadata through all example group ancestors, yielding each metadata hash along the way.
# File 'rspec-core/lib/rspec/core/metadata.rb', line 58
def self.ascending( ) yield return unless ( = .fetch(:example_group) { [:parent_example_group] }) loop do yield break unless ( = [:parent_example_group]) end end
.build_hash_from(args, warn_about_example_group_filtering = false)
Used internally to build a hash from an args array. Symbols are converted into hash keys with a value of true
. This is done to support simple tagging using a symbol, rather than needing to do ‘:symbol => true`.
# File 'rspec-core/lib/rspec/core/metadata.rb', line 80
def self.build_hash_from(args, warn_about_example_group_filtering=false) hash = args.last.is_a?(Hash) ? args.pop : {} hash[args.pop] = true while args.last.is_a?(Symbol) if warn_about_example_group_filtering && hash.key?(:example_group) RSpec.deprecate("Filtering by an `:example_group` subhash", :replacement => "the subhash to filter directly") end hash end
.deep_hash_dup(object)
# File 'rspec-core/lib/rspec/core/metadata.rb', line 94
def self.deep_hash_dup(object) return object.dup if Array === object return object unless Hash === object object.inject(object.dup) do |duplicate, (key, value)| duplicate[key] = deep_hash_dup(value) duplicate end end
.id_from(metadata)
# File 'rspec-core/lib/rspec/core/metadata.rb', line 105
def self.id_from( ) "#{ [:rerun_file_path]}[#{ [:scoped_id]}]" end
.location_tuple_from(metadata)
# File 'rspec-core/lib/rspec/core/metadata.rb', line 110
def self.location_tuple_from( ) [ [:absolute_file_path], [:line_number]] end
.relative_path(line) ⇒ String
# File 'rspec-core/lib/rspec/core/metadata.rb', line 44
def self.relative_path(line) line = line.sub(relative_path_regex, "\\1.\\2".freeze) line = line.sub(/\A([^:]+:\d+)$/, '\\1'.freeze) return nil if line == '-e:1'.freeze line rescue SecurityError # :nocov: - SecurityError is no longer produced starting in ruby 2.7 nil # :nocov: end
.relative_path_regex
Matches strings either at the beginning of the input or prefixed with a whitespace, containing the current path, either postfixed with the separator, or at the end of the string. Match groups are the character before and the character after the string if any.
rubular.com/r/fT0gmX6VJX rubular.com/r/duOrD4i3wb rubular.com/r/sbAMHFrOx1
# File 'rspec-core/lib/rspec/core/metadata.rb', line 36
def self.relative_path_regex @relative_path_regex ||= /(\A|\s)#{File. ('.')}(#{File::SEPARATOR}|\s|\Z)/ end