-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1893 from krororo/add-expectation_target_method
Add new `RSpec/MissingExpectationTargetMethod` cop
- Loading branch information
Showing
8 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
lib/rubocop/cop/rspec/missing_expectation_target_method.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Checks if `.to`, `not_to` or `to_not` are used. | ||
# | ||
# The RSpec::Expectations::ExpectationTarget must use `to`, `not_to` or | ||
# `to_not` to run. Therefore, this cop checks if other methods are used. | ||
# | ||
# @example | ||
# # bad | ||
# expect(something).kind_of? Foo | ||
# is_expected == 42 | ||
# expect{something}.eq? BarError | ||
# | ||
# # good | ||
# expect(something).to be_a Foo | ||
# is_expected.to eq 42 | ||
# expect{something}.to raise_error BarError | ||
# | ||
class MissingExpectationTargetMethod < Base | ||
MSG = 'Use `.to`, `.not_to` or `.to_not` to set an expectation.' | ||
RESTRICT_ON_SEND = %i[expect is_expected].freeze | ||
|
||
# @!method expect?(node) | ||
def_node_matcher :expect?, <<~PATTERN | ||
{ | ||
(send nil? :expect ...) | ||
(send nil? :is_expected) | ||
} | ||
PATTERN | ||
|
||
# @!method expect_block?(node) | ||
def_node_matcher :expect_block?, <<~PATTERN | ||
(block #expect? (args) _body) | ||
PATTERN | ||
|
||
# @!method expectation_without_runner?(node) | ||
def_node_matcher :expectation_without_runner?, <<~PATTERN | ||
(send {#expect? #expect_block?} !#Runners.all ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
node = node.parent if node.parent&.block_type? | ||
|
||
expectation_without_runner?(node.parent) do | ||
add_offense(node.parent.loc.selector) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
spec/rubocop/cop/rspec/missing_expectation_target_method_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::MissingExpectationTargetMethod do | ||
it 'registers offenses to other than `to` or `not_to`' do | ||
expect_offense(<<~RUBY) | ||
it 'something' do | ||
something = 1 | ||
expect(something).kind_of? Integer | ||
^^^^^^^^ Use `.to`, `.not_to` or `.to_not` to set an expectation. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers offenses to other than `to` or `not_to` ' \ | ||
'when block has one expression' do | ||
expect_offense(<<~RUBY) | ||
it 'something' do | ||
expect(something).eq? 42 | ||
^^^ Use `.to`, `.not_to` or `.to_not` to set an expectation. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers offenses to other than `to` or `not_to` with `is_expected`' do | ||
expect_offense(<<~RUBY) | ||
it 'something' do | ||
is_expected == 42 | ||
^^ Use `.to`, `.not_to` or `.to_not` to set an expectation. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers offenses to other than `to` or `not_to` with block' do | ||
expect_offense(<<~RUBY) | ||
it 'something' do | ||
expect{something}.kind_of? StandardError | ||
^^^^^^^^ Use `.to`, `.not_to` or `.to_not` to set an expectation. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'accepts void `expect`' do | ||
expect_no_offenses(<<~RUBY) | ||
it 'something' do | ||
expect(something) | ||
end | ||
RUBY | ||
end | ||
|
||
it 'accepts only `expect`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect | ||
RUBY | ||
end | ||
|
||
%w[to not_to to_not].each do |method| | ||
it "accepts `.#{method}`" do | ||
expect_no_offenses(<<~RUBY) | ||
it 'something' do | ||
expect(something).#{method} be_a Integer | ||
end | ||
RUBY | ||
end | ||
|
||
it "accepts `.#{method}` with `is_expected`" do | ||
expect_no_offenses(<<~RUBY) | ||
it 'something' do | ||
is_expected.#{method} eq 42 | ||
end | ||
RUBY | ||
end | ||
|
||
it "accepts `.#{method}` with block" do | ||
expect_no_offenses(<<~RUBY) | ||
it 'something' do | ||
expect{something}.#{method} raise_error(StandardError) | ||
end | ||
RUBY | ||
end | ||
end | ||
end |