Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RSpec/ExampleWording in escaping and % string literals #1800

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Fix a false positive for `RSpec/ExpectActual` when used with rspec-rails routing matchers. ([@naveg])
- Add new `RSpec/RepeatedSubjectCall` cop. ([@drcapulet])
- Add configuration option `ResponseMethods` to `RSpec/Rails/HaveHttpStatus`. ([@ydah])
- Fix `RSpec/ExampleWording` in escaping and `%` string literals. ([@r7kamura])

## 2.26.1 (2024-01-05)

Expand Down
23 changes: 16 additions & 7 deletions lib/rubocop/cop/rspec/example_wording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,27 @@ def add_wording_offense(node, message)
add_offense(docstring, message: message) do |corrector|
next if node.heredoc?

corrector.replace(docstring, replacement_text(node))
if node.str_type? && needs_escape?(node)
corrector.replace(node, replacement_text(node).inspect)
else
corrector.replace(docstring, replacement_text(node))
end
end
end

def docstring(node)
expr = node.source_range
if node.str_type? && !node.heredoc?
node.source_range.with(
begin_pos: node.loc.begin.end_pos,
end_pos: node.loc.end.begin_pos
)
else
node.source_range.adjust(begin_pos: 1, end_pos: -1)
end
end

Parser::Source::Range.new(
expr.source_buffer,
expr.begin_pos + 1,
expr.end_pos - 1
)
def needs_escape?(node)
node.value.include?(node.loc.end.source)
end

def replacement_text(node)
Expand Down
90 changes: 90 additions & 0 deletions spec/rubocop/cop/rspec/example_wording_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,94 @@
RUBY
end
end

context "when message includes `'` in `'...'`" do
it 'corrects message with `String#inspect`' do
expect_offense(<<~'RUBY')
it 'should return foo\'s bar' do
^^^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
end
RUBY

expect_correction(<<~RUBY)
it "returns foo's bar" do
end
RUBY
end
end

context 'when message includes `"` in `"..."`' do
it 'corrects message with `String#inspect`' do
expect_offense(<<~'RUBY')
it "should return \"foo\"" do
^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
end
RUBY

expect_correction(<<~'RUBY')
it "returns \"foo\"" do
end
RUBY
end
end

context 'when message includes `!` in `%!...!`' do
it 'corrects message with `String#inspect`' do
expect_offense(<<~'RUBY')
it %!should return foo\!! do
^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
end
RUBY

expect_correction(<<~RUBY)
it "returns foo!" do
end
RUBY
end
end

context 'when message includes `)` in `%q(...)`' do
it 'corrects message with `String#inspect`' do
expect_offense(<<~RUBY)
it %q(should return foo (bar)) do
pirj marked this conversation as resolved.
Show resolved Hide resolved
^^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
end
RUBY

expect_correction(<<~RUBY)
it "returns foo (bar)" do
end
RUBY
end
end

context 'when message includes `"` in `%q(...)`' do
it 'corrects message with direct substring replacement' do
expect_offense(<<~RUBY)
it %q(should return "foo") do
^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
end
RUBY

expect_correction(<<~RUBY)
it %q(returns "foo") do
end
RUBY
end
end

context 'when message includes `"` and `)` in `%q(...)`' do
it 'corrects message with `String#inspect`' do
expect_offense(<<~RUBY)
it %q(should return "foo (bar)") do
^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
end
RUBY

expect_correction(<<~'RUBY')
it "returns \"foo (bar)\"" do
end
RUBY
end
end
end