Class: RuboCop::Cop::RSpec::Focus

Inherits:
RuboCop::Cop show all
Defined in:
lib/rubocop/cop/rspec/focus.rb

Overview

Checks if test is focused.

Examples:

# bad
describe MyClass, focus: true do
end

describe MyClass, :focus do
end

fdescribe MyClass do
end

# good
describe MyClass do
end

Constant Summary collapse

MESSAGE =
'Focused spec found.'.freeze
FOCUSABLE_BLOCKS =
[
  :example_group, :describe, :context, :xdescribe, :xcontext,
  :it, :example, :specify, :xit, :xexample, :xspecify,
  :feature, :scenario, :xfeature, :xscenario
].freeze
FOCUSED_BLOCKS =
[
  :fdescribe, :fcontext,
  :focus, :fexample, :fit, :fspecify,
  :ffeature, :fscenario
].freeze
FOCUS_KEY =
s(:sym, :focus)
FOCUS_TRUE_PAIR =
s(:pair, FOCUS_KEY, s(:true))

Instance Method Summary collapse

Instance Method Details

#on_hash(node) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/rubocop/cop/rspec/focus.rb', line 56

def on_hash(node)
  return unless @focusable_block
  return if node.children.any? do |n|
    if [FOCUS_TRUE_PAIR].include?(n)
      add_offense(n, :expression, MESSAGE)
    end
  end
end

#on_send(node) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubocop/cop/rspec/focus.rb', line 42

def on_send(node)
  _receiver, method_name, *_args = *node
  @focusable_block = FOCUSABLE_BLOCKS.include?(method_name)
  if FOCUSED_BLOCKS.include?(method_name)
    add_offense(node, :expression, MESSAGE)
  end

  # check for :focus
  return unless @focusable_block
  node.children.any? do |n|
    add_offense(n, :expression, MESSAGE) if n == FOCUS_KEY
  end
end