Skip to content

Commit 072ffe9

Browse files
authored
Merge pull request #1249 from ydah/fix-empty-example-group-false-positive
[Fix #1244] Fix a false positive for `RSpec/EmptyExampleGroup` when expectations in case statement
2 parents 7e59a1b + 45b868e commit 072ffe9

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Master (Unreleased)
44

5+
* Fix a false positive for `RSpec/EmptyExampleGroup` when expectations in case statement. ([@ydah][])
6+
57
## 2.9.0 (2022-02-28)
68

79
* Add new `RSpec/BeNil` cop. ([@bquorning][])
@@ -672,3 +674,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
672674
[@leoarnold]: https://github.com/leoarnold
673675
[@harry-graham]: https://github.com/harry-graham
674676
[@oshiro3]: https://github.com/oshiro3
677+
[@ydah]: https://github.com/ydah

lib/rubocop/cop/rspec/empty_example_group.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,23 @@ def offensive?(body)
145145
return true unless body
146146
return false if conditionals_with_examples?(body)
147147

148-
if body.if_type?
148+
if body.if_type? || body.case_type?
149149
!examples_in_branches?(body)
150150
else
151151
!examples?(body)
152152
end
153153
end
154154

155155
def conditionals_with_examples?(body)
156-
return unless body.begin_type?
156+
return unless body.begin_type? || body.case_type?
157157

158-
body.each_descendant(:if).any? do |if_node|
159-
examples_in_branches?(if_node)
158+
body.each_descendant(:if, :case).any? do |condition_node|
159+
examples_in_branches?(condition_node)
160160
end
161161
end
162162

163-
def examples_in_branches?(if_node)
164-
if_node.branches.any? { |branch| examples?(branch) }
163+
def examples_in_branches?(condition_node)
164+
condition_node.branches.any? { |branch| examples?(branch) }
165165
end
166166
end
167167
end

spec/rubocop/cop/rspec/empty_example_group_spec.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,69 @@
110110
RUBY
111111
end
112112

113+
it 'ignores example group with examples defined in `case` branches' do
114+
expect_no_offenses(<<~RUBY)
115+
describe Foo do
116+
case bar
117+
when baz
118+
it { expect(result).to be(true) }
119+
end
120+
end
121+
122+
describe Foo do
123+
case bar
124+
when baz
125+
it { expect(result).to be(true) }
126+
else
127+
warn 'Enforce appropriate warnings.'
128+
end
129+
end
130+
RUBY
131+
end
132+
133+
it 'ignores example group with examples but no examples in `case` branches' do
134+
expect_no_offenses(<<~RUBY)
135+
describe Foo do
136+
case bar
137+
when baz
138+
warn 'Enforce appropriate warnings.'
139+
end
140+
141+
it { expect(result).to have_ads }
142+
end
143+
RUBY
144+
end
145+
146+
it 'flags an empty example group with no examples defined in `case`' \
147+
'branches' do
148+
expect_offense(<<~RUBY)
149+
describe Foo do
150+
^^^^^^^^^^^^ Empty example group detected.
151+
case bar
152+
when baz
153+
warn 'Enforce appropriate warnings.'
154+
else
155+
warn 'Enforce appropriate warnings.'
156+
end
157+
end
158+
159+
describe Foo do
160+
^^^^^^^^^^^^ Empty example group detected.
161+
case bar
162+
when baz
163+
else
164+
end
165+
end
166+
167+
describe Foo do
168+
^^^^^^^^^^^^ Empty example group detected.
169+
case bar
170+
when baz
171+
end
172+
end
173+
RUBY
174+
end
175+
113176
it 'ignores example group with examples defined in iterator' do
114177
expect_no_offenses(<<~RUBY)
115178
describe 'RuboCop monthly' do

0 commit comments

Comments
 (0)