From 59d9db078c8f451269889676569ca24f90b910bb Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Mon, 8 Jul 2024 09:52:27 -0700 Subject: [PATCH] Make Rspec/ExampleWording handle Unicode RIGHT SINGLE QUOTATION MARK MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some people and editors write contractions such as "shouldn't" and "won't" using the Unicode RIGHT SINGLE QUOTATION MARK, "shouldn’t" and "won’t" so make Rspec/ExampleWording understand this alternative spelling. --- CHANGELOG.md | 2 + lib/rubocop/cop/rspec/example_wording.rb | 4 +- lib/rubocop/rspec/wording.rb | 4 +- .../rubocop/cop/rspec/example_wording_spec.rb | 39 +++++++++++++++++++ spec/rubocop/rspec/wording_spec.rb | 5 +++ 5 files changed, 50 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de307ae25..80b038f06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Master (Unreleased) +- Add support for Unicode RIGHT SINGLE QUOTATION MARK in `RSpec/ExampleWording`. ([@jdufresne]) + ## 3.0.2 (2024-07-02) - Fix wrong autocorrect for `RSpec/ScatteredSetup` when hook contains heredoc. ([@earlopain]) diff --git a/lib/rubocop/cop/rspec/example_wording.rb b/lib/rubocop/cop/rspec/example_wording.rb index a650183f2..a07e6abfa 100644 --- a/lib/rubocop/cop/rspec/example_wording.rb +++ b/lib/rubocop/cop/rspec/example_wording.rb @@ -55,8 +55,8 @@ class ExampleWording < Base 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 + SHOULD_PREFIX = /\Ashould(?:n't|n’t)?\b/i.freeze + WILL_PREFIX = /\A(?:will|won't|won’t)\b/i.freeze IT_PREFIX = /\Ait /i.freeze # @!method it_description(node) diff --git a/lib/rubocop/rspec/wording.rb b/lib/rubocop/rspec/wording.rb index baee2818a..57929c3e3 100644 --- a/lib/rubocop/rspec/wording.rb +++ b/lib/rubocop/rspec/wording.rb @@ -4,10 +4,10 @@ module RuboCop module RSpec # RSpec example wording rewriter class Wording - SHOULDNT_PREFIX = /\Ashould(?:n't| not)\b/i.freeze + SHOULDNT_PREFIX = /\Ashould(?:n't|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 + WONT_PREFIX = /\Awo(?:n't|n’t)\b/i.freeze ES_SUFFIX_PATTERN = /(?:o|s|x|ch|sh|z)\z/i.freeze IES_SUFFIX_PATTERN = /[^aeou]y\z/i.freeze diff --git a/spec/rubocop/cop/rspec/example_wording_spec.rb b/spec/rubocop/cop/rspec/example_wording_spec.rb index 100ae719d..40645886b 100644 --- a/spec/rubocop/cop/rspec/example_wording_spec.rb +++ b/spec/rubocop/cop/rspec/example_wording_spec.rb @@ -148,6 +148,19 @@ 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 @@ -161,6 +174,19 @@ 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 @@ -200,6 +226,19 @@ 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 diff --git a/spec/rubocop/rspec/wording_spec.rb b/spec/rubocop/rspec/wording_spec.rb index 80f7c8a43..7248f0dda 100644 --- a/spec/rubocop/rspec/wording_spec.rb +++ b/spec/rubocop/rspec/wording_spec.rb @@ -24,15 +24,20 @@ 'should wish me luck' => 'wishes me luck', 'should really only return one item' => 'really only returns one item', "shouldn't return something" => 'does not return something', + 'shouldn’t return something' => 'does not return something', 'SHOULD RETAIN UPPERCASE' => 'RETAINS UPPERCASE', "shouldn't be true" => 'is not true', + 'shouldn’t be true' => 'is not true', "SHOULDN'T BE true" => 'IS NOT true', + 'SHOULDN’T BE true' => 'IS NOT true', "SHOULDN'T NOT RETAIN UPPERCASE" => 'DOES NOT NOT RETAIN UPPERCASE', + 'SHOULDN’T NOT RETAIN UPPERCASE' => 'DOES NOT NOT RETAIN UPPERCASE', 'should WORRY' => 'WORRIES', 'should WISH me luck' => 'WISHES me luck', '' => '', 'should' => '', "shouldn't" => 'does not', + 'shouldn’t' => 'does not', 'should not' => 'does not', 'should fizz' => 'fizzes' }