Skip to content

RSpec/ExampleWording unintentionally removes escapes #1799

@r7kamura

Description

@r7kamura

I noticed that it autocorrects as follows, which causes syntax error:

# before
it 'should return foo\'s bar' do
end

# after
it 'returns foo's bar' do
end

This can be confirmed by the following test code.

diff --git a/spec/rubocop/cop/rspec/example_wording_spec.rb b/spec/rubocop/cop/rspec/example_wording_spec.rb
index 100ae719..ae83c6ac 100644
--- a/spec/rubocop/cop/rspec/example_wording_spec.rb
+++ b/spec/rubocop/cop/rspec/example_wording_spec.rb
@@ -351,5 +351,18 @@ RSpec.describe RuboCop::Cop::RSpec::ExampleWording do
         end
       RUBY
     end
+
+    it 'keeps escaped single-quote after autocorrection' 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
 end
Failures:

  1) RuboCop::Cop::RSpec::ExampleWording when `DisallowedExamples: Workz` keeps escaped single-quote after autocorrection
     Failure/Error:
       expect_correction(<<~'RUBY')
         it 'returns foo\'s bar' do
         end
       RUBY
     
       expected: "it 'returns foo\\'s bar' do\nend\n"
            got: "it 'returns foo's bar' do\nend\n"
     
       (compared using ==)
     
       Diff:
       @@ -1 +1 @@
       -it 'returns foo\'s bar' do
       +it 'returns foo's bar' do
       
     # ./spec/rubocop/cop/rspec/example_wording_spec.rb:362:in `block (3 levels) in <top (required)>'

Since this cop supports not only str but also dstr, it would be difficult to simply fix it with String#inspect 🤔

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions