-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a cop to check for consistent method usage in feature specs.
- Loading branch information
Showing
5 changed files
with
124 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Checks for consistent method usage in feature specs. | ||
# | ||
# @example | ||
# # bad | ||
# feature 'User logs in' do | ||
# given(:user) { User.new } | ||
# | ||
# background do | ||
# visit new_session_path | ||
# end | ||
# | ||
# scenario 'with OAuth' do | ||
# # ... | ||
# end | ||
# end | ||
# | ||
# # good | ||
# describe 'User logs in' do | ||
# let(:user) { User.new } | ||
# | ||
# before do | ||
# visit new_session_path | ||
# end | ||
# | ||
# it 'with OAuth' do | ||
# # ... | ||
# end | ||
# end | ||
class FeatureMethods < Cop | ||
MSG = 'Use `%s` instead of `%s`.'.freeze | ||
|
||
# https://git.io/v7rLu | ||
MAP = { | ||
background: :before, | ||
scenario: :it, | ||
xscenario: :xit, | ||
given: :let, | ||
given!: :let!, | ||
feature: :describe | ||
}.freeze | ||
|
||
def_node_matcher :feature_method?, <<-PATTERN | ||
(send {(const nil :RSpec) nil} ${:#{MAP.keys.join(' :')}} ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
feature_method?(node) do |match| | ||
add_offense(node, :selector, format(MSG, MAP[match], match)) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
corrector.replace(node.loc.selector, MAP[node.method_name].to_s) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
RSpec.describe RuboCop::Cop::RSpec::FeatureMethods do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'flags violations for `background`' do | ||
expect_offense(<<-RUBY) | ||
background do; end | ||
^^^^^^^^^^ Use `before` instead of `background`. | ||
RUBY | ||
end | ||
|
||
it 'flags violations for `scenario`' do | ||
expect_offense(<<-RUBY) | ||
scenario 'Foo' do; end | ||
^^^^^^^^ Use `it` instead of `scenario`. | ||
RUBY | ||
end | ||
|
||
it 'flags violations for `xscenario`' do | ||
expect_offense(<<-RUBY) | ||
RSpec.xscenario 'Foo' do; end | ||
^^^^^^^^^ Use `xit` instead of `xscenario`. | ||
RUBY | ||
end | ||
|
||
it 'flags violations for `given`' do | ||
expect_offense(<<-RUBY) | ||
given(:foo) { :foo } | ||
^^^^^ Use `let` instead of `given`. | ||
RUBY | ||
end | ||
|
||
it 'flags violations for `given!`' do | ||
expect_offense(<<-RUBY) | ||
given!(:foo) { :foo } | ||
^^^^^^ Use `let!` instead of `given!`. | ||
RUBY | ||
end | ||
|
||
it 'flags violations for `feature`' do | ||
expect_offense(<<-RUBY) | ||
RSpec.feature 'Foo' do; end | ||
^^^^^^^ Use `describe` instead of `feature`. | ||
RUBY | ||
end | ||
|
||
include_examples 'autocorrect', 'background { }', 'before { }' | ||
include_examples 'autocorrect', 'scenario { }', 'it { }' | ||
include_examples 'autocorrect', 'xscenario { }', 'xit { }' | ||
include_examples 'autocorrect', 'given(:foo) { }', 'let(:foo) { }' | ||
include_examples 'autocorrect', 'given!(:foo) { }', 'let!(:foo) { }' | ||
include_examples 'autocorrect', 'RSpec.feature { }', 'RSpec.describe { }' | ||
end |