Class: RuboCop::Cop::RSpec::Extra::RestrictBlockTagValue

Inherits:
Base
  • Object
show all
Includes:
Metadata
Defined in:
lib/rubocop/cop/rspec/extra/restrict_block_tag_value.rb

Overview

Restrict to only allowed block tag value.

Examples:

AllowTagValues: { foo: 'baz' }

# bad
RSpec.describe 'Something', foo: :bar do
end

# good
RSpec.describe 'Something', foo: :baz do
end

Constant Summary collapse

MSG =
"This value is not allowed in this tag. Allowed tag value: %<allow_tag_value>s."

Instance Method Summary collapse

Instance Method Details

#on_metadata(_symbols, hash) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubocop/cop/rspec/extra/restrict_block_tag_value.rb', line 22

def (_symbols, hash)
  return if hash.nil?

  offenses = hash.pairs.filter do |pair|
    allow_tag_values.any? do |k, v|
      pair.key.value.to_s == k &&
        pair.value.value.to_s != v
    end
  end

  return unless offenses.any?

  offenses.each do |offense|
    add_offense(offense, message: format(MSG, allow_tag_value: allow_tag_values[offense.key.value.to_s]))
  end
end