Class: RSpec::Core::Metadata

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

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:



5
6
7
8
9
10
11
12
13
14
# File 'lib/rspec/core/metadata.rb', line 5

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

  store(:example_group, example_group || {})
  yield self if block_given?
end

Instance Method Details

#all_apply?(filters) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/rspec/core/metadata.rb', line 82

def all_apply?(filters)
  filters.all? do |key, value|
    apply_condition(key, value)
  end
end

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rspec/core/metadata.rb', line 97

def apply_condition(key, value, =nil)
   ||= self
  case value
  when Hash
    value.all? { |k, v| apply_condition(k, v, [key]) }
  when Regexp
    [key] =~ value
  when Proc
    value.call([key]) rescue false
  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, options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rspec/core/metadata.rb', line 69

def configure_for_example(description, options)
  store(:description, description.to_s)
  store(:full_description, "#{self[:example_group][:full_description]} #{self[:description]}")
  store(:execution_result, {})
  store(:caller, options.delete(:caller))
  if self[:caller]
    store(:file_path, file_path_from(self))
    store(:line_number, line_number_from(self))
  end
  self[:location] = location_from(self)
  update(options)
end

#ensure_valid_keys(user_metadata) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rspec/core/metadata.rb', line 43

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, options) ⇒ Object



65
66
67
# File 'lib/rspec/core/metadata.rb', line 65

def for_example(description, options)
  dup.configure_for_example(description,options)
end

#process(*args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rspec/core/metadata.rb', line 26

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

  self[:example_group][:describes] = described_class_from(*args)
  self[:example_group][:description] = description_from(*args)
  self[:example_group][:full_description] = full_description_from(*args)

  self[:example_group][:block] = .delete(:example_group_block)
  self[:example_group][:caller] = .delete(:caller) || caller(1)
  self[:example_group][:file_path] = file_path_from(self[:example_group], .delete(:file_path))
  self[:example_group][:line_number] = line_number_from(self[:example_group], .delete(:line_number))
  self[:example_group][:location] = location_from(self[:example_group])

  update()
end

#relevant_line_numbers(metadata) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/rspec/core/metadata.rb', line 88

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