Class: RSpec::Core::Metadata

Inherits:
Hash
  • Object
show all
Defined in:
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.

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).

Defined Under Namespace

Modules: ExampleMetadataHash, GroupMetadataHash, MetadataHash

Instance Method Summary collapse

Constructor Details

#initialize(parent_group_metadata = nil) {|_self| ... } ⇒ Metadata

Returns a new instance of Metadata.

Yields:

  • (_self)

Yield Parameters:



133
134
135
136
137
138
139
140
141
142
# File 'lib/rspec/core/metadata.rb', line 133

def initialize(=nil)
  if 
    update()
    store(:example_group, {:example_group => [:example_group].extend(GroupMetadataHash)}.extend(GroupMetadataHash))
  else
    store(:example_group, {}.extend(GroupMetadataHash))
  end

  yield self if block_given?
end

Instance Method Details

#all_apply?(filters) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/rspec/core/metadata.rb', line 166

def all_apply?(filters)
  filters.all? {|k,v| filter_applies?(k,v)}
end

#any_apply?(filters) ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/rspec/core/metadata.rb', line 161

def any_apply?(filters)
  filters.any? {|k,v| filter_applies?(k,v)}
end

#filter_applies?(key, value, metadata = self) ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rspec/core/metadata.rb', line 171

def filter_applies?(key, value, =self)
  return .filter_applies_to_any_value?(key, value) if Array === [key] && !(Proc === value)
  return .line_number_filter_applies?(value)       if key == :line_numbers
  return .location_filter_applies?(value)          if key == :locations
  return .filters_apply?(key, value)               if Hash === value

  case value
  when Regexp
    [key] =~ value
  when Proc
    if value.arity == 2
      # Pass the metadata hash to allow the proc to check if it even has the key.
      # This is necessary for the implicit :if exclusion filter:
      #   {            } # => run the example
      #   { :if => nil } # => exclude the example
      # The value of metadata[:if] is the same in these two cases but
      # they need to be treated differently.
      value.call([key], ) rescue false
    else
      value.call([key]) rescue false
    end
  else
    [key].to_s == value.to_s
  end
end

#filter_applies_to_any_value?(key, value) ⇒ Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/rspec/core/metadata.rb', line 203

def filter_applies_to_any_value?(key, value)
  self[key].any? {|v| filter_applies?(key, v, {key => value})}
end

#filters_apply?(key, value) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/rspec/core/metadata.rb', line 198

def filters_apply?(key, value)
  value.all? {|k, v| filter_applies?(k, v, self[key])}
end

#for_example(description, user_metadata) ⇒ Object



156
157
158
# File 'lib/rspec/core/metadata.rb', line 156

def for_example(description, )
  dup.extend(ExampleMetadataHash).configure_for_example(description, )
end

#line_number_filter_applies?(line_numbers) ⇒ Boolean

Returns:

  • (Boolean)


215
216
217
218
# File 'lib/rspec/core/metadata.rb', line 215

def line_number_filter_applies?(line_numbers)
  preceding_declaration_lines = line_numbers.map {|n| RSpec.world.preceding_declaration_line(n)}
  !(relevant_line_numbers & preceding_declaration_lines).empty?
end

#location_filter_applies?(locations) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
211
212
# File 'lib/rspec/core/metadata.rb', line 208

def location_filter_applies?(locations)
  # it ignores location filters for other files
  line_number = example_group_declaration_line(locations)
  line_number ? line_number_filter_applies?(line_number) : true
end

#process(*args) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/rspec/core/metadata.rb', line 145

def process(*args)
   = args.last.is_a?(Hash) ? args.pop : {}
  ensure_valid_keys()

  self[:example_group].store(:description_args, args)
  self[:example_group].store(:caller, .delete(:caller) || caller)

  update()
end