Skip to content

Commit

Permalink
Merge pull request #1135 from hosamaly/master
Browse files Browse the repository at this point in the history
Allow `RSpec/ContextWording` to accept multi-word prefixes
  • Loading branch information
pirj committed Mar 23, 2021
2 parents 695d004 + 35f197e commit a8101ae
Show file tree
Hide file tree
Showing 4 changed files with 40 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)

* Allow `RSpec/ContextWording` to accept multi-word prefixes. ([@hosamaly][])

## 2.2.0 (2021-02-02)

* Fix `HooksBeforeExamples`, `LeadingSubject`, `LetBeforeExamples` and `ScatteredLet` autocorrection to take into account inline comments and comments immediately before the moved node. ([@Darhazer][])
Expand Down Expand Up @@ -603,3 +605,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@sl4vr]: https://github.com/sl4vr
[@ahukkanen]: https://github.com/ahukkanen
[@dvandersluis]: https://github.com/dvandersluis
[@hosamaly]: https://github.com/hosamaly
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops_rspec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ Checks that `context` docstring starts with an allowed prefix.
The default list of prefixes is minimal. Users are encouraged to tailor
the configuration to meet project needs. Other acceptable prefixes may
include `if`, `unless`, `for`, `before`, `after`, or `during`.
They may consist of multiple words if desired.

=== Examples

Expand Down
7 changes: 6 additions & 1 deletion lib/rubocop/cop/rspec/context_wording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module RSpec
# The default list of prefixes is minimal. Users are encouraged to tailor
# the configuration to meet project needs. Other acceptable prefixes may
# include `if`, `unless`, `for`, `before`, `after`, or `during`.
# They may consist of multiple words if desired.
#
# @see https://rspec.rubystyle.guide/#context-descriptions
# @see http://www.betterspecs.org/#contexts
Expand Down Expand Up @@ -52,7 +53,7 @@ def on_block(node)
private

def bad_prefix?(description)
!prefixes.include?(description.split(/\b/).first)
!prefix_regex.match?(description)
end

def joined_prefixes
Expand All @@ -66,6 +67,10 @@ def joined_prefixes
def prefixes
cop_config['Prefixes'] || []
end

def prefix_regex
/^#{Regexp.union(prefixes)}\b/
end
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/cop/rspec/context_wording_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,35 @@
end
RUBY
end

context 'with a multi-word prefix' do
let(:cop_config) { { 'Prefixes' => ['assuming that'] } }

it 'skips descriptions with allowed multi-word prefixes' do
expect_no_offenses(<<-RUBY)
context 'assuming that display name is present' do
end
RUBY
end
end

context 'with special regex characters' do
let(:cop_config) { { 'Prefixes' => ['a$b\d'] } }

it 'matches the full prefix' do
expect_offense(<<-RUBY)
context 'a' do
^^^ Start context description with 'a$b\\d'.
end
RUBY
end

it 'matches special characters' do
expect_no_offenses(<<-RUBY)
context 'a$b\\d something' do
end
RUBY
end
end
end
end

0 comments on commit a8101ae

Please sign in to comment.