Class: RuboCop::Cop::RSpec::PredicateMatcher

Inherits:
Cop
  • Object
show all
Includes:
ConfigurableEnforcedStyle, ExplicitHelper, InflectedHelper
Defined in:
lib/rubocop/cop/rspec/predicate_matcher.rb

Overview

Prefer using predicate matcher over using predicate method directly.

RSpec defines magic matchers for predicate methods. This cop recommends to use the predicate matcher instead of using predicate method directly.

Examples:

Strict: true, EnforcedStyle: inflected (default)

# bad
expect(foo.something?).to be_truthy

# good
expect(foo).to be_something

# also good - It checks "true" strictly.
expect(foo).to be(true)

Strict: false, EnforcedStyle: inflected

# bad
expect(foo.something?).to be_truthy
expect(foo).to be(true)

# good
expect(foo).to be_something

Strict: true, EnforcedStyle: explicit

# bad
expect(foo).to be_something

# good - the above code is rewritten to it by this cop
expect(foo.something?).to be(true)

Strict: false, EnforcedStyle: explicit

# bad
expect(foo).to be_something

# good - the above code is rewritten to it by this cop
expect(foo.something?).to be_truthy

Constant Summary

Constants included from ExplicitHelper

ExplicitHelper::BUILT_IN_MATCHERS, ExplicitHelper::MSG_EXPLICIT

Constants included from RSpec::Language

RSpec::Language::ALL, RSpec::Language::RSPEC

Constants included from InflectedHelper

InflectedHelper::MSG_INFLECTED

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#autocorrect(node) ⇒ Object



320
321
322
323
324
325
326
327
# File 'lib/rubocop/cop/rspec/predicate_matcher.rb', line 320

def autocorrect(node)
  case style
  when :inflected
    autocorrect_inflected(node)
  when :explicit
    autocorrect_explicit(node)
  end
end

#on_block(node) ⇒ Object



316
317
318
# File 'lib/rubocop/cop/rspec/predicate_matcher.rb', line 316

def on_block(node)
  check_explicit(node) if style == :explicit
end

#on_send(node) ⇒ Object



307
308
309
310
311
312
313
314
# File 'lib/rubocop/cop/rspec/predicate_matcher.rb', line 307

def on_send(node)
  case style
  when :inflected
    check_inflected(node)
  when :explicit
    check_explicit(node)
  end
end