Class: RSpec::Core::Metadata

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

Defined Under Namespace

Modules: LocationKeys

Constant Summary collapse

RESERVED_KEYS =
[
  :description,
  :example_group,
  :execution_result,
  :file_path,
  :full_description,
  :line_number,
  :location
]

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Metadata.

Yields:

  • (_self)

Yield Parameters:



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rspec/core/metadata.rb', line 35

def initialize(=nil)
  @superclass_metadata = 
  if @superclass_metadata
    update(@superclass_metadata)
    example_group = {:example_group => @superclass_metadata[:example_group]}
  else
    example_group = {}
  end

  store(:example_group, example_group.extend(LocationKeys))
  yield self if block_given?
end

Instance Method Details

#apply?(predicate, filters) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
# File 'lib/rspec/core/metadata.rb', line 105

def apply?(predicate, filters)
  filters.send(predicate) do |key, value|
    apply_condition(key, value)
  end
end

#apply_condition(key, value, metadata = self) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rspec/core/metadata.rb', line 120

def apply_condition(key, value, =self)
  case value
  when Hash
    value.all? { |k, v| apply_condition(k, v, [key]) }
  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 Fixnum
    if key == :line_number
      relevant_line_numbers().include?(world.preceding_declaration_line(value))
    else
      [key] == value
    end
  else
    [key] == value
  end
end

#configure_for_example(description, user_metadata) ⇒ Object



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

def configure_for_example(description, )
  store(:description, description.to_s)
  store(:full_description, "#{self[:example_group][:full_description]} #{self[:description]}")
  store(:execution_result, {})
  store(:caller, .delete(:caller) || caller)
  update()
end

#ensure_valid_keys(user_metadata) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rspec/core/metadata.rb', line 71

def ensure_valid_keys()
  RESERVED_KEYS.each do |key|
    if .keys.include?(key)
      raise <<-EOM
#{"*"*50}
:#{key} is not allowed

RSpec 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}
EOM
      raise ":#{key} is not allowed"
    end
  end
end

#for_example(description, user_metadata) ⇒ Object



93
94
95
# File 'lib/rspec/core/metadata.rb', line 93

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

#process(*args) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rspec/core/metadata.rb', line 58

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

  self[:example_group].store(:caller, .delete(:caller) || caller)
  self[:example_group].store(:describes, described_class_from(*args))
  self[:example_group].store(:description, description_from(*args))
  self[:example_group].store(:full_description, full_description_from(*args))
  self[:example_group].store(:block, .delete(:example_group_block))

  update()
end

#relevant_line_numbers(metadata) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/rspec/core/metadata.rb', line 111

def relevant_line_numbers()
  line_numbers = [[:line_number]]
  if [:example_group]
    line_numbers + relevant_line_numbers([:example_group])
  else
    line_numbers
  end
end