-
-
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.
Merge pull request #1796 from rubocop/add-xstr-only
Add new `RSpec/UndescriptiveLiteralsDescription` cop
- Loading branch information
Showing
8 changed files
with
274 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
69 changes: 69 additions & 0 deletions
69
lib/rubocop/cop/rspec/undescriptive_literals_description.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,69 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Description should be descriptive. | ||
# | ||
# If example group or example contains only `execute string`, numbers | ||
# and regular expressions, the description is not clear. | ||
# | ||
# @example | ||
# # bad | ||
# describe `time` do | ||
# # ... | ||
# end | ||
# | ||
# # bad | ||
# context /when foo/ do | ||
# # ... | ||
# end | ||
# | ||
# # bad | ||
# it 10000 do | ||
# # ... | ||
# end | ||
# | ||
# # good | ||
# describe Foo do | ||
# # ... | ||
# end | ||
# | ||
# # good | ||
# describe '#foo' do | ||
# # ... | ||
# end | ||
# | ||
# # good | ||
# context "when #{foo} is bar" do | ||
# # ... | ||
# end | ||
# | ||
# # good | ||
# it 'does something' do | ||
# # ... | ||
# end | ||
# | ||
class UndescriptiveLiteralsDescription < Base | ||
MSG = 'Description should be descriptive.' | ||
|
||
# @!method example_groups_or_example?(node) | ||
def_node_matcher :example_groups_or_example?, <<~PATTERN | ||
(block (send #rspec? {#ExampleGroups.all #Examples.all} $_) ...) | ||
PATTERN | ||
|
||
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler | ||
example_groups_or_example?(node) do |arg| | ||
add_offense(arg) if offense?(arg) | ||
end | ||
end | ||
|
||
private | ||
|
||
def offense?(node) | ||
%i[xstr int regexp].include?(node.type) | ||
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
133 changes: 133 additions & 0 deletions
133
spec/rubocop/cop/rspec/undescriptive_literals_description_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,133 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::UndescriptiveLiteralsDescription, :config do | ||
it 'registers an offense when using `describe` with only execute string' do | ||
expect_offense(<<~RUBY) | ||
describe `time` do | ||
^^^^^^ Description should be descriptive. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `context` with only execute string' do | ||
expect_offense(<<~RUBY) | ||
context `time` do | ||
^^^^^^ Description should be descriptive. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `it` with only execute string' do | ||
expect_offense(<<~RUBY) | ||
it `time` do | ||
^^^^^^ Description should be descriptive. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `describe` with only regex' do | ||
expect_offense(<<~RUBY) | ||
describe /time/ do | ||
^^^^^^ Description should be descriptive. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `describe` with only Integer' do | ||
expect_offense(<<~RUBY) | ||
describe 10000 do | ||
^^^^^ Description should be descriptive. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `describe` with method call' do | ||
expect_no_offenses(<<~RUBY) | ||
describe foo.to_s do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `describe` with local variable' do | ||
expect_no_offenses(<<~RUBY) | ||
types.each do |type| | ||
describe type do | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `describe` with ' \ | ||
'instance variable' do | ||
expect_no_offenses(<<~RUBY) | ||
describe @foo do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `describe` with ' \ | ||
'grobal variable' do | ||
expect_no_offenses(<<~RUBY) | ||
describe $foo do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `describe` with a string' do | ||
expect_no_offenses(<<~RUBY) | ||
describe '#foo' do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `describe` with a class' do | ||
expect_no_offenses(<<~RUBY) | ||
describe Foo do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `context` with a string' do | ||
expect_no_offenses(<<~RUBY) | ||
context 'when foo is bar' do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `it` with a string' do | ||
expect_no_offenses(<<~RUBY) | ||
it 'does something' do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `describe` with an ' \ | ||
'interpolation string' do | ||
expect_no_offenses(<<~RUBY) | ||
describe "foo \#{bar}" do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `describe` with a ' \ | ||
'heredoc string' do | ||
expect_no_offenses(<<~RUBY) | ||
describe <<~DESC do | ||
foo | ||
DESC | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `describe` with a ' \ | ||
'string concatenation' do | ||
expect_no_offenses(<<~RUBY) | ||
describe 'foo' + `time` do | ||
context 'when ' + 1.to_s do | ||
it 'returns ' + /something/.to_s do | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
end |