Skip to content

Commit 7586bc3

Browse files
committed
Fix false negative for RSpec/PredicateMatcher when expectation contains custom failure message
This leaves `predicate_matcher_block?` since RSpec doesn't seem to handle that case
1 parent 781a866 commit 7586bc3

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Master (Unreleased)
44

55
- Fix wrong autocorrect for `RSpec/ScatteredSetup` when hook contains heredoc. ([@earlopain])
6+
- Fix false negative for `RSpec/PredicateMatcher` when expectation contains custom failure message. ([@earlopain])
67

78
## 3.0.1 (2024-06-11)
89

lib/rubocop/cop/rspec/predicate_matcher.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def check_inflected(node)
3232
(block $(send !nil? #predicate? ...) ...)
3333
$(send !nil? #predicate? ...)})
3434
$#Runners.all
35-
$#boolean_matcher?)
35+
$#boolean_matcher? ...)
3636
PATTERN
3737

3838
# @!method be_bool?(node)
@@ -183,8 +183,12 @@ def heredoc_argument?(matcher)
183183
(send
184184
(send nil? :expect $!nil?)
185185
#Runners.all
186-
{$(send nil? #predicate_matcher_name? ...)
187-
(block $(send nil? #predicate_matcher_name? ...) ...)})
186+
{
187+
$(send nil? #predicate_matcher_name? ...)
188+
(block $(send nil? #predicate_matcher_name? ...) ...)
189+
}
190+
...
191+
)
188192
PATTERN
189193

190194
# @!method predicate_matcher_block?(node)

spec/rubocop/cop/rspec/predicate_matcher_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
expect_offense(<<~RUBY)
1717
expect(foo.empty?).to be_truthy
1818
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
19+
expect(foo.empty?).to be_truthy, 'fail'
20+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
1921
expect(foo.empty?).not_to be_truthy
2022
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
2123
expect(foo.empty?).to_not be_truthy
@@ -30,6 +32,7 @@
3032

3133
expect_correction(<<~RUBY)
3234
expect(foo).to be_empty
35+
expect(foo).to be_empty, 'fail'
3336
expect(foo).not_to be_empty
3437
expect(foo).not_to be_empty
3538
expect(foo).not_to be_empty
@@ -42,6 +45,8 @@
4245
expect_offense(<<~RUBY)
4346
expect(foo.exist?).to be_truthy
4447
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `exist` matcher over `exist?`.
48+
expect(foo.exist?).to be_truthy, 'fail'
49+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `exist` matcher over `exist?`.
4550
expect(foo.exists?).to be_truthy
4651
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `exist` matcher over `exists?`.
4752
expect(foo.has_something?).to be_truthy
@@ -58,6 +63,7 @@
5863

5964
expect_correction(<<~RUBY)
6065
expect(foo).to exist
66+
expect(foo).to exist, 'fail'
6167
expect(foo).to exist
6268
expect(foo).to have_something
6369
expect(foo).not_to have_something
@@ -71,6 +77,8 @@
7177
expect_offense(<<~RUBY)
7278
expect(foo.something?('foo')).to be_truthy
7379
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_something` matcher over `something?`.
80+
expect(foo.something?('foo')).to be_truthy, 'fail'
81+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_something` matcher over `something?`.
7482
expect(foo.something?('foo', 'bar')).to be_truthy
7583
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_something` matcher over `something?`.
7684
expect(foo.something? 1, 2).to be_truthy
@@ -85,6 +93,7 @@
8593

8694
expect_correction(<<~RUBY)
8795
expect(foo).to be_something('foo')
96+
expect(foo).to be_something('foo'), 'fail'
8897
expect(foo).to be_something('foo', 'bar')
8998
expect(foo).to be_something 1, 2
9099
expect(foo).to have_key('foo')
@@ -112,6 +121,8 @@
112121
expect_offense(<<~RUBY)
113122
expect(foo.all?(&:present?)).to be_truthy
114123
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_all` matcher over `all?`.
124+
expect(foo.all?(&:present?)).to be_truthy, 'fail'
125+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_all` matcher over `all?`.
115126
expect(foo.all? { |x| x.present? }).to be_truthy
116127
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_all` matcher over `all?`.
117128
expect(foo.all?(n) { |x| x.present? }).to be_truthy
@@ -134,6 +145,7 @@
134145

135146
expect_correction(<<~RUBY)
136147
expect(foo).to be_all(&:present?)
148+
expect(foo).to be_all(&:present?), 'fail'
137149
expect(foo).to be_all { |x| x.present? }
138150
expect(foo).to be_all(n) { |x| x.present? }
139151
expect(foo).to be_all { present }
@@ -151,13 +163,15 @@
151163
it 'accepts a predicate method that is not checked true/false' do
152164
expect_no_offenses(<<~RUBY)
153165
expect(foo.something?).to eq "something"
166+
expect(foo.something?).to eq "something", "fail"
154167
expect(foo.has_something?).to eq "something"
155168
RUBY
156169
end
157170

158171
it 'accepts non-predicate method' do
159172
expect_no_offenses(<<~RUBY)
160173
expect(foo.something).to be(true)
174+
expect(foo.something).to be(true), 'fail'
161175
expect(foo.has_something).to be(true)
162176
RUBY
163177
end
@@ -171,6 +185,7 @@
171185
it 'accepts strict checking boolean matcher' do
172186
expect_no_offenses(<<~RUBY)
173187
expect(foo.empty?).to eq(true)
188+
expect(foo.empty?).to eq(true), 'fail'
174189
expect(foo.empty?).to be(true)
175190
expect(foo.empty?).to be(false)
176191
expect(foo.empty?).not_to be true
@@ -188,6 +203,8 @@
188203
expect_offense(<<~RUBY)
189204
expect(foo.empty?).to eq(true)
190205
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
206+
expect(foo.empty?).to eq(true), 'fail'
207+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
191208
expect(foo.empty?).not_to be(true)
192209
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
193210
expect(foo.empty?).to be(true)
@@ -202,6 +219,7 @@
202219

203220
expect_correction(<<~RUBY)
204221
expect(foo).to be_empty
222+
expect(foo).to be_empty, 'fail'
205223
expect(foo).not_to be_empty
206224
expect(foo).to be_empty
207225
expect(foo).not_to be_empty
@@ -220,6 +238,8 @@
220238
expect_offense(<<~RUBY)
221239
expect(foo).to be_empty
222240
^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `empty?` over `be_empty` matcher.
241+
expect(foo).to be_empty, 'fail'
242+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `empty?` over `be_empty` matcher.
223243
expect(foo).not_to be_empty
224244
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `empty?` over `be_empty` matcher.
225245
expect(foo).to have_something
@@ -252,6 +272,7 @@
252272
it 'accepts built in matchers' do
253273
expect_no_offenses(<<~RUBY)
254274
expect(foo).to be_truthy
275+
expect(foo).to be_truthy, 'fail'
255276
expect(foo).to be_falsey
256277
expect(foo).to be_falsy
257278
expect(foo).to have_attributes(name: 'foo')
@@ -275,13 +296,16 @@
275296
it 'accepts non-predicate matcher' do
276297
expect_no_offenses(<<~RUBY)
277298
expect(foo).to be(true)
299+
expect(foo).to be(true), 'fail'
278300
RUBY
279301
end
280302

281303
it 'registers an offense for a predicate method with built-in equiv' do
282304
expect_offense(<<~RUBY)
283305
expect(foo).to be_something
284306
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `something?` over `be_something` matcher.
307+
expect(foo).to be_something, 'fail'
308+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `something?` over `be_something` matcher.
285309
expect(foo).not_to be_something
286310
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `something?` over `be_something` matcher.
287311
expect(foo).to have_something
@@ -300,6 +324,7 @@
300324

301325
expect_correction(<<~RUBY)
302326
expect(foo.something?).to #{matcher_true}
327+
expect(foo.something?).to #{matcher_true}, 'fail'
303328
expect(foo.something?).to #{matcher_false}
304329
expect(foo.has_something?).to #{matcher_true}
305330
expect(foo.is_a?(Array)).to #{matcher_true}
@@ -403,6 +428,7 @@
403428
'with no argument' do
404429
expect_no_offenses(<<~RUBY)
405430
expect(foo).to include
431+
expect(foo).to include, 'fail'
406432
RUBY
407433
end
408434

0 commit comments

Comments
 (0)