File tree Expand file tree Collapse file tree 3 files changed +72
-6
lines changed Expand file tree Collapse file tree 3 files changed +72
-6
lines changed Original file line number Diff line number Diff line change 2
2
3
3
## Master (Unreleased)
4
4
5
+ * Fix a false positive for ` RSpec/EmptyExampleGroup ` when expectations in case statement. ([ @ydah ] [ ] )
6
+
5
7
## 2.9.0 (2022-02-28)
6
8
7
9
* Add new ` RSpec/BeNil ` cop. ([ @bquorning ] [ ] )
@@ -672,3 +674,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
672
674
[ @leoarnold ] : https://github.com/leoarnold
673
675
[ @harry-graham ] : https://github.com/harry-graham
674
676
[ @oshiro3 ] : https://github.com/oshiro3
677
+ [ @ydah ] : https://github.com/ydah
Original file line number Diff line number Diff line change @@ -145,23 +145,23 @@ def offensive?(body)
145
145
return true unless body
146
146
return false if conditionals_with_examples? ( body )
147
147
148
- if body . if_type?
148
+ if body . if_type? || body . case_type?
149
149
!examples_in_branches? ( body )
150
150
else
151
151
!examples? ( body )
152
152
end
153
153
end
154
154
155
155
def conditionals_with_examples? ( body )
156
- return unless body . begin_type?
156
+ return unless body . begin_type? || body . case_type?
157
157
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 )
160
160
end
161
161
end
162
162
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 ) }
165
165
end
166
166
end
167
167
end
Original file line number Diff line number Diff line change 110
110
RUBY
111
111
end
112
112
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
+
113
176
it 'ignores example group with examples defined in iterator' do
114
177
expect_no_offenses ( <<~RUBY )
115
178
describe 'RuboCop monthly' do
You can’t perform that action at this time.
0 commit comments