Skip to content

Commit

Permalink
Add AllowedPatterns configuration option to `RSpec/NoExpectationExa…
Browse files Browse the repository at this point in the history
…mple`

Fix: rubocop#1385

This cop can be customized allowed expectation methods pattern
with `AllowedPatterns`. \Aexpect_ and \Aassert_ are allowed by default.

### @ example `AllowedPatterns: [\Aexpect_]`
```ruby
# bad
it do
  not_expect_something
end

# good
it do
  expect_something
end
```
  • Loading branch information
ydah committed Oct 10, 2022
1 parent 4a05fd3 commit 79e7a11
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Add `require_implicit` style to `RSpec/ImplicitSubject`. ([@r7kamura][])
* Fix a false positive for `RSpec/Capybara/SpecificMatcher` when `have_css("a")` without attribute. ([@ydah][])
* Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample`. ([@ydah][])

## 2.13.2 (2022-09-23)

Expand Down
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,11 @@ RSpec/NoExpectationExample:
Enabled: pending
Safe: false
VersionAdded: '2.13'
VersionChanged: '2.14'
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NoExpectationExample
AllowedPatterns:
- "\\Aexpect_"
- "\\Aassert_"

RSpec/NotToNot:
Description: Checks for consistent method usage for negating expectations.
Expand Down
43 changes: 42 additions & 1 deletion docs/modules/ROOT/pages/cops_rspec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3503,7 +3503,7 @@ end
| No
| No
| 2.13
| -
| 2.14
|===

Checks if an example contains any expectation.
Expand All @@ -3520,6 +3520,9 @@ add the following configuration:
Expectations:
- custom_expect

This cop can be customized allowed expectation methods pattern
with `AllowedPatterns`. \Aexpect_ and \Aassert_ are allowed by default.

=== Examples

[source,ruby]
Expand All @@ -3535,6 +3538,44 @@ it do
end
----

==== `AllowedPatterns` configuration

[source,ruby]
----
# .rubocop.yml
# RSpec/NoExpectationExample:
# AllowedPatterns:
# - \Aexpect_
# - \Aassert_
----

[source,ruby]
----
# bad
it do
not_expect_something
end
# good
it do
expect_something
end
it do
assert_something
end
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| AllowedPatterns
| `\Aexpect_`, `\Aassert_`
| Array
|===

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NoExpectationExample
Expand Down
38 changes: 38 additions & 0 deletions lib/rubocop/cop/rspec/no_expectation_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,34 @@ module RSpec
# expect(a?).to be(true)
# end
#
# This cop can be customized allowed expectation methods pattern
# with `AllowedPatterns`. \Aexpect_ and \Aassert_ are allowed by default.
#
# @example `AllowedPatterns` configuration
#
# # .rubocop.yml
# # RSpec/NoExpectationExample:
# # AllowedPatterns:
# # - \Aexpect_
# # - \Aassert_
#
# @example
# # bad
# it do
# not_expect_something
# end
#
# # good
# it do
# expect_something
# end
#
# it do
# assert_something
# end
#
class NoExpectationExample < Base
include AllowedPattern
MSG = 'No expectation found in this example.'

# @!method regular_or_focused_example?(node)
Expand Down Expand Up @@ -61,11 +88,22 @@ def on_block(node)
return unless regular_or_focused_example?(node)
return if including_any_expectation?(node)
return if including_any_skip_example?(node)
return if including_any_allowed_pattern?(node)

add_offense(node)
end

alias on_numblock on_block

private

def including_any_allowed_pattern?(node)
node.each_descendant(:send) do |send_node|
return true if matches_allowed_pattern?(send_node.method_name)
end

false
end
end
end
end
Expand Down
33 changes: 33 additions & 0 deletions spec/rubocop/cop/rspec/no_expectation_example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,39 @@
end
end

context 'when `AllowedPatterns: [\Aexpect_]`' do
let(:cop_config) { { 'AllowedPatterns' => ['\Aexpect_'] } }

context 'with only allowed pattern methods are used' do
it 'registers no offenses' do
expect_no_offenses(<<~RUBY)
it { expect_something }
RUBY
end
end

context 'with allowed pattern methods and other method are used' do
it 'registers no offenses' do
expect_no_offenses(<<~RUBY)
it do
do_something
expect_something
do_something
end
RUBY
end
end

context 'with only not allowed pattern methods are used' do
it 'does something' do
expect_offense(<<~RUBY)
it { not_expect_something }
^^^^^^^^^^^^^^^^^^^^^^^^^^^ No expectation found in this example.
RUBY
end
end
end

context 'when Ruby 2.7', :ruby27 do
context 'with no expectation example with it' do
it 'registers an offense' do
Expand Down

0 comments on commit 79e7a11

Please sign in to comment.