Hash
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).
@see Example#metadata @see ExampleGroup.metadata @see FilterManager @see Configuration#filter_run_including @see Configuration#filter_run_excluding
# File lib/rspec/core/metadata.rb, line 140 140: def initialize(parent_group_metadata=nil) 141: if parent_group_metadata 142: update(parent_group_metadata) 143: store(:example_group, {:example_group => parent_group_metadata[:example_group].extend(GroupMetadataHash)}.extend(GroupMetadataHash)) 144: else 145: store(:example_group, {}.extend(GroupMetadataHash)) 146: end 147: 148: yield self if block_given? 149: end
@private
# File lib/rspec/core/metadata.rb, line 173 173: def all_apply?(filters) 174: filters.all? {|k,v| filter_applies?(k,v)} 175: end
@private
# File lib/rspec/core/metadata.rb, line 168 168: def any_apply?(filters) 169: filters.any? {|k,v| filter_applies?(k,v)} 170: end
@private
# File lib/rspec/core/metadata.rb, line 178 178: def filter_applies?(key, value, metadata=self) 179: return metadata.filter_applies_to_any_value?(key, value) if Array === metadata[key] && !(Proc === value) 180: return metadata.line_number_filter_applies?(value) if key == :line_numbers 181: return metadata.location_filter_applies?(value) if key == :locations 182: return metadata.filters_apply?(key, value) if Hash === value 183: 184: return false unless metadata.has_key?(key) 185: 186: case value 187: when Regexp 188: metadata[key] =~ value 189: when Proc 190: case value.arity 191: when 0 then value.call 192: when 2 then value.call(metadata[key], metadata) 193: else value.call(metadata[key]) 194: end 195: else 196: metadata[key].to_s == value.to_s 197: end 198: end
@private
# File lib/rspec/core/metadata.rb, line 206 206: def filter_applies_to_any_value?(key, value) 207: self[key].any? {|v| filter_applies?(key, v, {key => value})} 208: end
@private
# File lib/rspec/core/metadata.rb, line 201 201: def filters_apply?(key, value) 202: value.all? {|k, v| filter_applies?(k, v, self[key])} 203: end
@private
# File lib/rspec/core/metadata.rb, line 163 163: def for_example(description, user_metadata) 164: dup.extend(ExampleMetadataHash).configure_for_example(description, user_metadata) 165: end
@private
# File lib/rspec/core/metadata.rb, line 218 218: def line_number_filter_applies?(line_numbers) 219: preceding_declaration_lines = line_numbers.map {|n| RSpec.world.preceding_declaration_line(n)} 220: !(relevant_line_numbers & preceding_declaration_lines).empty? 221: end
@private
# File lib/rspec/core/metadata.rb, line 211 211: def location_filter_applies?(locations) 212: # it ignores location filters for other files 213: line_number = example_group_declaration_line(locations) 214: line_number ? line_number_filter_applies?(line_number) : true 215: end
@private
# File lib/rspec/core/metadata.rb, line 152 152: def process(*args) 153: user_metadata = args.last.is_a?(Hash) ? args.pop : {} 154: ensure_valid_keys(user_metadata) 155: 156: self[:example_group].store(:description_args, args) 157: self[:example_group].store(:caller, user_metadata.delete(:caller) || caller) 158: 159: update(user_metadata) 160: end
# File lib/rspec/core/metadata.rb, line 225 225: def configure_for_example(description, user_metadata) 226: store(:description_args, [description]) 227: store(:caller, user_metadata.delete(:caller) || caller) 228: update(user_metadata) 229: end
# File lib/rspec/core/metadata.rb, line 243 243: def ensure_valid_keys(user_metadata) 244: RESERVED_KEYS.each do |key| 245: if user_metadata.has_key?(key) 246: raise #{"*"*50}:#{key} is not allowedRSpec reserves some hash keys for its own internal use,including :#{key}, which is used on: #{caller(0)[4]}.Here are all of RSpec's reserved hash keys: #{RESERVED_KEYS.join("\n ")} #{"*"*50} 247: end 248: end 249: end
# File lib/rspec/core/metadata.rb, line 264 264: def example_group_declaration_line(locations) 265: locations[File.expand_path(self[:example_group][:file_path])] if self[:example_group] 266: end
TODO - make this a method on metadata - the problem is metadata[:example_group] is not always a kind of GroupMetadataHash.
# File lib/rspec/core/metadata.rb, line 270 270: def relevant_line_numbers(metadata=self) 271: [metadata[:line_number]] + (metadata[:example_group] ? relevant_line_numbers(metadata[:example_group]) : []) 272: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.