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 8, 2023
1 parent 9d51eb0 commit c5e511c
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

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

## 2.25.0 (2023-10-27)

- Add support single quoted string and percent string and heredoc for `RSpec/Rails/HttpStatus`. ([@ydah])
Expand Down Expand Up @@ -855,6 +857,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
2 changes: 1 addition & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ RSpec/ExampleWording:
DisallowedExamples:
- works
VersionAdded: '1.0'
VersionChanged: '2.13'
VersionChanged: "<<next>>"
StyleGuide: https://rspec.rubystyle.guide/#should-in-example-docstrings
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExampleWording

Expand Down
13 changes: 13 additions & 0 deletions 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 = /\Aw(?:ill|on't)\b/i.freeze
IT_PREFIX = /\Ait /i.freeze

# @!method it_description(node)
Expand All @@ -66,6 +71,8 @@ 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 Down Expand Up @@ -106,6 +113,12 @@ def replacement_text(node)
ignore: ignored_words,
replace: custom_transform
).rewrite
elsif text.match?(WILL_PREFIX)
RuboCop::RSpec::Wording.new(
text,
ignore: ignored_words,
replace: custom_transform
).rewrite
else
text.sub(IT_PREFIX, '')
end
Expand Down
6 changes: 6 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 @@ -21,6 +23,10 @@ def rewrite
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
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 c5e511c

Please sign in to comment.