Class: RSpec::Core::Metadata

Inherits:
Hash
  • Object
show all
Defined in:
lib/rspec/core/metadata.rb

Defined Under Namespace

Modules: 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:



97
98
99
100
101
102
103
104
105
106
# File 'lib/rspec/core/metadata.rb', line 97

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

  yield self if block_given?
end

Instance Method Details

#all_apply?(filters) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#any_apply?(filters) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/rspec/core/metadata.rb', line 122

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

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

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rspec/core/metadata.rb', line 130

def filter_applies?(key, value, =self)
  case value
  when Hash
    if key == :locations
      file_path     = (self[:example_group] || {})[:file_path]
      expanded_path = file_path && File.expand_path( file_path )
      if expanded_path && line_numbers = value[expanded_path]
        filter_applies?(:line_numbers, line_numbers)
      else
        true
      end
    else
      value.all? { |k, v| filter_applies?(k, v, [key]) }
    end
  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
  when String
    [key].to_s == value.to_s
  when Enumerable
    if key == :line_numbers
      preceding_declaration_lines = value.map{|v| world.preceding_declaration_line(v)}
      !(relevant_line_numbers() & preceding_declaration_lines).empty?
    else
      [key] == value
    end
  else
    [key].to_s == value.to_s
  end
end

#for_example(description, user_metadata) ⇒ Object



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

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

#process(*args) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/rspec/core/metadata.rb', line 108

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