Skip to content

Commit d8342e1

Browse files
committed
Add AllowedExplicitMatchers config option for RSpec/PredicateMatcher
1 parent 7e2e3fc commit d8342e1

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Fix documentation rake task to support Rubocop 0.75. ([@nickcampbell18][])
77
* Fix `RSpec/SubjectStub` to detect implicit subjects stubbed. ([@QQism][])
88
* Fix `RSpec/Pending` not flagging `skip` with string values. ([@pirj][])
9+
* Add `AllowedExplicitMatchers` config option for `RSpec/PredicateMatcher`. ([@mkrawc][])
910

1011
## 1.36.0 (2019-09-27)
1112

@@ -460,3 +461,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
460461
[@nickcampbell18]: https://github.com/nickcampbell18
461462
[@QQism]: https://github.com/QQism
462463
[@kellysutton]: https://github.com/kellysutton
464+
[@mkrawc]: https://github.com/mkrawc

config/default.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ RSpec/PredicateMatcher:
368368
Enabled: true
369369
Strict: true
370370
EnforcedStyle: inflected
371+
AllowedExplicitMatchers: []
371372
SupportedStyles:
372373
- inflected
373374
- explicit

lib/rubocop/cop/rspec/predicate_matcher.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ module ExplicitHelper
138138

139139
private
140140

141+
def allowed_explicit_matchers
142+
cop_config.fetch('AllowedExplicitMatchers', []) + BUILT_IN_MATCHERS
143+
end
144+
141145
def check_explicit(node) # rubocop:disable Metrics/MethodLength
142146
predicate_matcher_block?(node) do |_actual, matcher|
143147
add_offense(
@@ -178,7 +182,7 @@ def check_explicit(node) # rubocop:disable Metrics/MethodLength
178182
def predicate_matcher_name?(name)
179183
name = name.to_s
180184

181-
return false if BUILT_IN_MATCHERS.include?(name)
185+
return false if allowed_explicit_matchers.include?(name)
182186

183187
name.start_with?('be_', 'have_') && !name.end_with?('?')
184188
end

manual/cops_rspec.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,6 +2430,7 @@ Name | Default value | Configurable values
24302430
--- | --- | ---
24312431
Strict | `true` | Boolean
24322432
EnforcedStyle | `inflected` | `inflected`, `explicit`
2433+
AllowedExplicitMatchers | `[]` | Array
24332434

24342435
### References
24352436

spec/rubocop/cop/rspec/predicate_matcher_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
let(:cop_config) do
77
{ 'EnforcedStyle' => enforced_style,
8-
'Strict' => strict }
8+
'Strict' => strict,
9+
'AllowedExplicitMatchers' => allowed_explicit_matchers }
910
end
11+
let(:allowed_explicit_matchers) { [] }
1012

1113
context 'when enforced style is `inflected`' do
1214
let(:enforced_style) { 'inflected' }
@@ -270,6 +272,16 @@
270272
RUBY
271273
end
272274

275+
context 'when custom matchers are allowed' do
276+
let(:allowed_explicit_matchers) { ['have_http_status'] }
277+
278+
it 'accepts custom allowed explicit matchers' do
279+
expect_no_offenses(<<-RUBY)
280+
expect(foo).to have_http_status(:ok)
281+
RUBY
282+
end
283+
end
284+
273285
it 'accepts non-predicate matcher' do
274286
expect_no_offenses(<<-RUBY)
275287
expect(foo).to be(true)

0 commit comments

Comments
 (0)