Skip to content

Commit

Permalink
Use new expect_correction in Style N-P
Browse files Browse the repository at this point in the history
Use newer expect_offense and expect_correction in specs for Style cops N-P

Merge 'register offense' and 'auto-correct' spec examples where possible.

Use single-quoted heredoc for nested_parenthesized_calls_spec line
continuation
  • Loading branch information
biinari authored and bbatsov committed Aug 7, 2020
1 parent be47dc7 commit 64122b9
Show file tree
Hide file tree
Showing 20 changed files with 1,636 additions and 1,284 deletions.
68 changes: 28 additions & 40 deletions spec/rubocop/cop/style/negated_if_spec.rb
Expand Up @@ -21,6 +21,13 @@
some_method if !a_condition
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `unless` over `if` for negative conditions.
RUBY

expect_correction(<<~RUBY)
unless a_condition
some_method
end
some_method unless a_condition
RUBY
end

it 'registers an offense for if with "not" condition' do
Expand All @@ -32,6 +39,13 @@
some_method if not a_condition
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `unless` over `if` for negative conditions.
RUBY

expect_correction(<<~RUBY)
unless a_condition
some_method
end
some_method unless a_condition
RUBY
end

it 'accepts an if/else with negative condition' do
Expand Down Expand Up @@ -82,31 +96,14 @@
RUBY
end

it 'autocorrects for postfix' do
corrected = autocorrect_source('bar if !foo')

expect(corrected).to eq 'bar unless foo'
end

it 'autocorrects by replacing if not with unless' do
corrected = autocorrect_source('something if !x.even?')
expect(corrected).to eq 'something unless x.even?'
end

it 'autocorrects by replacing parenthesized if not with unless' do
corrected = autocorrect_source('something if (!x.even?)')
expect(corrected).to eq 'something unless (x.even?)'
end

it 'autocorrects for prefix' do
corrected = autocorrect_source(<<~RUBY)
if !foo
end
expect_offense(<<~RUBY)
something if (!x.even?)
^^^^^^^^^^^^^^^^^^^^^^^ Favor `unless` over `if` for negative conditions.
RUBY

expect(corrected).to eq <<~RUBY
unless foo
end
expect_correction(<<~RUBY)
something unless (x.even?)
RUBY
end
end
Expand All @@ -129,23 +126,16 @@
^^^^^^^ Favor `unless` over `if` for negative conditions.
end
RUBY
end

it 'does not register an offense for postfix' do
expect_no_offenses('foo if !bar')
end

it 'autocorrects for prefix' do
corrected = autocorrect_source(<<~RUBY)
if !foo
end
RUBY

expect(corrected).to eq <<~RUBY
expect_correction(<<~RUBY)
unless foo
end
RUBY
end

it 'does not register an offense for postfix' do
expect_no_offenses('foo if !bar')
end
end

describe 'with “postfix” style' do
Expand All @@ -165,6 +155,10 @@
foo if !bar
^^^^^^^^^^^ Favor `unless` over `if` for negative conditions.
RUBY

expect_correction(<<~RUBY)
foo unless bar
RUBY
end

it 'does not register an offense for prefix' do
Expand All @@ -173,12 +167,6 @@
end
RUBY
end

it 'autocorrects for postfix' do
corrected = autocorrect_source('bar if !foo')

expect(corrected).to eq 'bar unless foo'
end
end

it 'does not blow up for ternary ops' do
Expand Down
68 changes: 28 additions & 40 deletions spec/rubocop/cop/style/negated_unless_spec.rb
Expand Up @@ -21,6 +21,13 @@
some_method unless !a_condition
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `if` over `unless` for negative conditions.
RUBY

expect_correction(<<~RUBY)
if a_condition
some_method
end
some_method if a_condition
RUBY
end

it 'registers an offense for unless with "not" condition' do
Expand All @@ -32,6 +39,13 @@
some_method unless not a_condition
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `if` over `unless` for negative conditions.
RUBY

expect_correction(<<~RUBY)
if a_condition
some_method
end
some_method if a_condition
RUBY
end

it 'accepts an unless/else with negative condition' do
Expand Down Expand Up @@ -65,31 +79,14 @@
RUBY
end

it 'autocorrects for postfix' do
corrected = autocorrect_source('bar unless !foo')

expect(corrected).to eq 'bar if foo'
end

it 'autocorrects by replacing unless not with if' do
corrected = autocorrect_source('something unless !x.even?')
expect(corrected).to eq 'something if x.even?'
end

it 'autocorrects by replacing parenthesized unless not with if' do
corrected = autocorrect_source('something unless (!x.even?)')
expect(corrected).to eq 'something if (x.even?)'
end

it 'autocorrects for prefix' do
corrected = autocorrect_source(<<~RUBY)
unless !foo
end
expect_offense(<<~RUBY)
something unless (!x.even?)
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `if` over `unless` for negative conditions.
RUBY

expect(corrected).to eq <<~RUBY
if foo
end
expect_correction(<<~RUBY)
something if (x.even?)
RUBY
end
end
Expand All @@ -112,23 +109,16 @@
^^^^^^^^^^^ Favor `if` over `unless` for negative conditions.
end
RUBY
end

it 'does not register an offense for postfix' do
expect_no_offenses('foo unless !bar')
end

it 'autocorrects for prefix' do
corrected = autocorrect_source(<<~RUBY)
unless !foo
end
RUBY

expect(corrected).to eq <<~RUBY
expect_correction(<<~RUBY)
if foo
end
RUBY
end

it 'does not register an offense for postfix' do
expect_no_offenses('foo unless !bar')
end
end

describe 'with “postfix” style' do
Expand All @@ -148,6 +138,10 @@
foo unless !bar
^^^^^^^^^^^^^^^ Favor `if` over `unless` for negative conditions.
RUBY

expect_correction(<<~RUBY)
foo if bar
RUBY
end

it 'does not register an offense for prefix' do
Expand All @@ -156,12 +150,6 @@
end
RUBY
end

it 'autocorrects for postfix' do
corrected = autocorrect_source('bar unless !foo')

expect(corrected).to eq 'bar if foo'
end
end

it 'does not blow up for ternary ops' do
Expand Down
38 changes: 34 additions & 4 deletions spec/rubocop/cop/style/negated_while_spec.rb
Expand Up @@ -12,6 +12,13 @@
some_method while !a_condition
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `until` over `while` for negative conditions.
RUBY

expect_correction(<<~RUBY)
until a_condition
some_method
end
some_method until a_condition
RUBY
end

it 'registers an offense for until with exclamation point condition' do
Expand All @@ -23,6 +30,13 @@
some_method until !a_condition
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `while` over `until` for negative conditions.
RUBY

expect_correction(<<~RUBY)
while a_condition
some_method
end
some_method while a_condition
RUBY
end

it 'registers an offense for while with "not" condition' do
Expand All @@ -34,6 +48,13 @@
some_method while not a_condition
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `until` over `while` for negative conditions.
RUBY

expect_correction(<<~RUBY)
until (a_condition)
some_method
end
some_method until a_condition
RUBY
end

it 'accepts a while where only part of the condition is negated' do
Expand All @@ -58,19 +79,28 @@
end

it 'autocorrects by replacing while not with until' do
corrected = autocorrect_source(<<~RUBY)
expect_offense(<<~RUBY)
something while !x.even?
^^^^^^^^^^^^^^^^^^^^^^^^ Favor `until` over `while` for negative conditions.
something while(!x.even?)
^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `until` over `while` for negative conditions.
RUBY
expect(corrected).to eq <<~RUBY

expect_correction(<<~RUBY)
something until x.even?
something until(x.even?)
RUBY
end

it 'autocorrects by replacing until not with while' do
corrected = autocorrect_source('something until !x.even?')
expect(corrected).to eq 'something while x.even?'
expect_offense(<<~RUBY)
something until !x.even?
^^^^^^^^^^^^^^^^^^^^^^^^ Favor `while` over `until` for negative conditions.
RUBY

expect_correction(<<~RUBY)
something while x.even?
RUBY
end

it 'does not blow up for empty while condition' do
Expand Down

0 comments on commit 64122b9

Please sign in to comment.