Skip to content

Commit

Permalink
Make RSpec/ExampleWording handle "it will" future tense
Browse files Browse the repository at this point in the history
Fixes #1751
  • Loading branch information
jdufresne committed Dec 10, 2023
1 parent 6a21ef4 commit 5c19818
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Master (Unreleased)

- Add new `RSpec/RedundantPredicateMatcher` cop. ([@ydah])
- Add support for correcting "it will" (future tense) for `RSpec/ExampleWording`. ([@jdufresne])

## 2.25.0 (2023-10-27)

Expand Down Expand Up @@ -857,6 +858,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@ignaciovillaverde]: https://github.com/ignaciovillaverde
[@jaredbeck]: https://github.com/jaredbeck
[@jaredmoody]: https://github.com/jaredmoody
[@jdufresne]: https://github.com/jdufresne
[@jeffreyc]: https://github.com/jeffreyc
[@jfragoulis]: https://github.com/jfragoulis
[@johnny-miyake]: https://github.com/johnny-miyake
Expand Down
3 changes: 3 additions & 0 deletions docs/modules/ROOT/pages/cops_rspec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,9 @@ case sensitive.
it 'should find nothing' do
end
it 'will find nothing' do
end
# good
it 'finds nothing' do
end
Expand Down
11 changes: 10 additions & 1 deletion lib/rubocop/cop/rspec/example_wording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ module RSpec
# it 'should find nothing' do
# end
#
# it 'will find nothing' do
# end
#
# # good
# it 'finds nothing' do
# end
Expand All @@ -47,11 +50,13 @@ class ExampleWording < Base
extend AutoCorrector

MSG_SHOULD = 'Do not use should when describing your tests.'
MSG_WILL = 'Do not use the future tense when describing your tests.'
MSG_IT = "Do not repeat 'it' when describing your tests."
MSG_INSUFFICIENT_DESCRIPTION = 'Your example description is ' \
'insufficient.'

SHOULD_PREFIX = /\Ashould(?:n't)?\b/i.freeze
WILL_PREFIX = /\A(?:will|won't)\b/i.freeze
IT_PREFIX = /\Ait /i.freeze

# @!method it_description(node)
Expand All @@ -62,10 +67,13 @@ class ExampleWording < Base
} ...) ...)
PATTERN

# rubocop:disable Metrics/MethodLength
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
it_description(node) do |description_node, message|
if message.match?(SHOULD_PREFIX)
add_wording_offense(description_node, MSG_SHOULD)
elsif message.match?(WILL_PREFIX)
add_wording_offense(description_node, MSG_WILL)
elsif message.match?(IT_PREFIX)
add_wording_offense(description_node, MSG_IT)
elsif insufficient_docstring?(description_node)
Expand All @@ -74,6 +82,7 @@ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
end
end
end
# rubocop:enable Metrics/MethodLength

private

Expand All @@ -100,7 +109,7 @@ def docstring(node)
def replacement_text(node)
text = text(node)

if text.match?(SHOULD_PREFIX)
if text.match?(SHOULD_PREFIX) || text.match?(WILL_PREFIX)
RuboCop::RSpec::Wording.new(
text,
ignore: ignored_words,
Expand Down
8 changes: 8 additions & 0 deletions lib/rubocop/rspec/wording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module RSpec
class Wording
SHOULDNT_PREFIX = /\Ashould(?:n't| not)\b/i.freeze
SHOULDNT_BE_PREFIX = /#{SHOULDNT_PREFIX} be\b/i.freeze
WILL_NOT_PREFIX = /\Awill not\b/i.freeze
WONT_PREFIX = /\Awon't\b/i.freeze
ES_SUFFIX_PATTERN = /(?:o|s|x|ch|sh|z)\z/i.freeze
IES_SUFFIX_PATTERN = /[^aeou]y\z/i.freeze

Expand All @@ -15,16 +17,22 @@ def initialize(text, ignore:, replace:)
@replacements = replace
end

# rubocop:disable Metrics/MethodLength
def rewrite
case text
when SHOULDNT_BE_PREFIX
replace_prefix(SHOULDNT_BE_PREFIX, 'is not')
when SHOULDNT_PREFIX
replace_prefix(SHOULDNT_PREFIX, 'does not')
when WILL_NOT_PREFIX
replace_prefix(WILL_NOT_PREFIX, 'does not')
when WONT_PREFIX
replace_prefix(WONT_PREFIX, 'does not')
else
remove_should_and_pluralize
end
end
# rubocop:enable Metrics/MethodLength

private

Expand Down
104 changes: 104 additions & 0 deletions spec/rubocop/cop/rspec/example_wording_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,110 @@
RUBY
end

it 'finds description with `will` at the beginning' do
expect_offense(<<-RUBY)
it 'will do something' do
^^^^^^^^^^^^^^^^^ Do not use the future tense when describing your tests.
end
RUBY

expect_correction(<<-RUBY)
it 'does something' do
end
RUBY
end

it 'finds interpolated description with `will` at the beginning' do
expect_offense(<<-'RUBY')
it "will do #{:stuff}" do
^^^^^^^^^^^^^^^^^ Do not use the future tense when describing your tests.
end
RUBY

expect_correction(<<-'RUBY')
it "does #{:stuff}" do
end
RUBY
end

it 'finds description with `Will` at the beginning' do
expect_offense(<<-RUBY)
it 'Will do something' do
^^^^^^^^^^^^^^^^^ Do not use the future tense when describing your tests.
end
RUBY

expect_correction(<<-RUBY)
it 'does something' do
end
RUBY
end

it "finds description with `won't` at the beginning" do
expect_offense(<<-RUBY)
it "won't do something" do
^^^^^^^^^^^^^^^^^^ Do not use the future tense when describing your tests.
end
RUBY

expect_correction(<<-RUBY)
it "does not do something" do
end
RUBY
end

it "finds description with `WON'T` at the beginning" do
expect_offense(<<-RUBY)
it "WON'T do something" do
^^^^^^^^^^^^^^^^^^ Do not use the future tense when describing your tests.
end
RUBY

expect_correction(<<-RUBY)
it "DOES NOT do something" do
end
RUBY
end

it 'flags a lone will' do
expect_offense(<<-RUBY)
it 'will' do
^^^^ Do not use the future tense when describing your tests.
end
RUBY

expect_correction(<<-RUBY)
it '' do
end
RUBY
end

it 'flags a lone will not' do
expect_offense(<<-RUBY)
it 'will not' do
^^^^^^^^ Do not use the future tense when describing your tests.
end
RUBY

expect_correction(<<-RUBY)
it 'does not' do
end
RUBY
end

it "flags a lone won't" do
expect_offense(<<-RUBY)
it "won't" do
^^^^^ Do not use the future tense when describing your tests.
end
RUBY

expect_correction(<<-RUBY)
it "does not" do
end
RUBY
end

it 'finds leading its' do
expect_offense(<<-RUBY)
it "it does something" do
Expand Down

0 comments on commit 5c19818

Please sign in to comment.