Skip to content

Commit

Permalink
Make Rspec/ExampleWording handle Unicode RIGHT SINGLE QUOTATION MARK
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jdufresne committed Jul 8, 2024
1 parent 77f026b commit 59d9db0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
2 changes: 2 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 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])
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/rspec/example_wording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/rspec/wording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
39 changes: 39 additions & 0 deletions spec/rubocop/cop/rspec/example_wording_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions spec/rubocop/rspec/wording_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down

0 comments on commit 59d9db0

Please sign in to comment.