-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make `RSpec/BeNil` cop configurable with a `be_nil` and a `be` style. Inspired by @marcandre's comment at #1239 (comment) I see absolutely no reason to prefer `be_nil` to `be nil`. The latter is stricter, simpler and requires less knowledge of rspec.
- Loading branch information
Showing
5 changed files
with
136 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::BeNil do | ||
it 'registers an offense when using `#be` for `nil` value' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to be(nil) | ||
^^^^^^^ Prefer `be_nil` over `be(nil)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to be_nil | ||
RUBY | ||
let(:cop_config) do | ||
{ 'EnforcedStyle' => enforced_style } | ||
end | ||
|
||
it 'does not register an offense when using `#be_nil`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).to be_nil | ||
RUBY | ||
context 'with EnforcedStyle `be_nil`' do | ||
let(:enforced_style) { 'be_nil' } | ||
|
||
it 'registers an offense when using `#be` for `nil` value' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to be(nil) | ||
^^^^^^^ Prefer `be_nil` over `be(nil)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to be_nil | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `#be_nil`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).to be_nil | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `#be` with other values' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).to be(true) | ||
expect(foo).to be(false) | ||
expect(foo).to be(1) | ||
expect(foo).to be("yes") | ||
expect(foo).to be(Class.new) | ||
RUBY | ||
end | ||
end | ||
|
||
it 'does not register an offense when using `#be` with other values' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).to be(true) | ||
expect(foo).to be(false) | ||
expect(foo).to be(1) | ||
expect(foo).to be("yes") | ||
expect(foo).to be(Class.new) | ||
RUBY | ||
context 'with EnforcedStyle `be`' do | ||
let(:enforced_style) { 'be' } | ||
|
||
it 'does not register an offense when using `#be` for `nil` value' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).to be(nil) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `#be_nil`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to be_nil | ||
^^^^^^ Prefer `be(nil)` over `be_nil`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to be(nil) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using other `#be_*` methods' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).to be_truthy | ||
expect(foo).to be_falsey | ||
expect(foo).to be_fooish | ||
RUBY | ||
end | ||
end | ||
end |