Skip to content

Commit

Permalink
Convert lint tests to use expect_correction
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosenblum authored and bbatsov committed Aug 19, 2019
1 parent fee3987 commit 5657091
Show file tree
Hide file tree
Showing 16 changed files with 368 additions and 461 deletions.
18 changes: 10 additions & 8 deletions spec/rubocop/cop/lint/big_decimal_new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@

let(:config) { RuboCop::Config.new }

it 'registers an offense when using `BigDecimal.new()`' do
it 'registers an offense and corrects using `BigDecimal.new()`' do
expect_offense(<<~RUBY)
BigDecimal.new(123.456, 3)
^^^ `BigDecimal.new()` is deprecated. Use `BigDecimal()` instead.
RUBY

expect_correction(<<~RUBY)
BigDecimal(123.456, 3)
RUBY
end

it 'registers an offense when using `::BigDecimal.new()`' do
it 'registers an offense and corrects using `::BigDecimal.new()`' do
expect_offense(<<~RUBY)
::BigDecimal.new(123.456, 3)
^^^ `::BigDecimal.new()` is deprecated. Use `::BigDecimal()` instead.
RUBY

expect_correction(<<~RUBY)
::BigDecimal(123.456, 3)
RUBY
end

it 'does not register an offense when using `BigDecimal()`' do
expect_no_offenses(<<~RUBY)
BigDecimal(123.456, 3)
RUBY
end

it 'autocorrects `BigDecimal()`' do
new_source = autocorrect_source('BigDecimal.new(123.456, 3)')

expect(new_source).to eq 'BigDecimal(123.456, 3)'
end
end
45 changes: 25 additions & 20 deletions spec/rubocop/cop/lint/deprecated_class_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,82 @@
subject(:cop) { described_class.new }

context 'prefer `File.exist?` over `File.exists?`' do
it 'registers an offense for File.exists?' do
it 'registers an offense and corrects File.exists?' do
expect_offense(<<~RUBY)
File.exists?(o)
^^^^^^^ `File.exists?` is deprecated in favor of `File.exist?`.
RUBY

expect_correction(<<~RUBY)
File.exist?(o)
RUBY
end

it 'registers an offense for ::File.exists?' do
it 'registers an offense and corrects ::File.exists?' do
expect_offense(<<~RUBY)
::File.exists?(o)
^^^^^^^ `File.exists?` is deprecated in favor of `File.exist?`.
RUBY

expect_correction(<<~RUBY)
::File.exist?(o)
RUBY
end

it 'does not register an offense for File.exist?' do
expect_no_offenses('File.exist?(o)')
end

it 'auto-corrects File.exists? with File.exist?' do
new_source = autocorrect_source('File.exists?(something)')
expect(new_source).to eq('File.exist?(something)')
end
end

context 'prefer `Dir.exist?` over `Dir.exists?`' do
it 'registers an offense for Dir.exists?' do
it 'registers an offense and corrects Dir.exists?' do
expect_offense(<<~RUBY)
Dir.exists?(o)
^^^^^^^ `Dir.exists?` is deprecated in favor of `Dir.exist?`.
RUBY

expect_correction(<<~RUBY)
Dir.exist?(o)
RUBY
end

it 'registers an offense for ::Dir.exists?' do
it 'registers an offense and corrects ::Dir.exists?' do
expect_offense(<<~RUBY)
::Dir.exists?(o)
^^^^^^^ `Dir.exists?` is deprecated in favor of `Dir.exist?`.
RUBY

expect_correction(<<~RUBY)
::Dir.exist?(o)
RUBY
end

it 'does not register an offense for Dir.exist?' do
expect_no_offenses('Dir.exist?(o)')
end

it 'auto-corrects Dir.exists? with Dir.exist?' do
new_source = autocorrect_source('Dir.exists?(something)')
expect(new_source).to eq('Dir.exist?(something)')
end

it 'does not register an offense for offensive method `exists?`'\
'on other receivers' do
expect_no_offenses('Foo.exists?(o)')
end
end

context 'prefer `block_given?` over `iterator?`' do
it 'registers an offense for iterator?' do
it 'registers an offense and corrects iterator?' do
expect_offense(<<~RUBY)
iterator?
^^^^^^^^^ `iterator?` is deprecated in favor of `block_given?`.
RUBY

expect_correction(<<~RUBY)
block_given?
RUBY
end

it 'does not register an offense for block_given?' do
expect_no_offenses('block_given?')
end

it 'autocorrects `iterator?` to `block_given?`' do
new_source = autocorrect_source('iterator?')
expect(new_source).to eq('block_given?')
end

it 'does not register an offense for offensive method `iterator?`'\
'on other receivers' do
expect_no_offenses('Foo.iterator?')
Expand Down
12 changes: 2 additions & 10 deletions spec/rubocop/cop/lint/empty_ensure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@
RSpec.describe RuboCop::Cop::Lint::EmptyEnsure do
subject(:cop) { described_class.new }

it 'registers an offense for empty ensure' do
it 'registers an offense and corrects empty ensure' do
expect_offense(<<~RUBY)
begin
something
ensure
^^^^^^ Empty `ensure` block detected.
end
RUBY
end

it 'autocorrects for empty ensure' do
corrected = autocorrect_source(<<~RUBY)
begin
something
ensure
end
RUBY
expect(corrected).to eq(<<~RUBY)
expect_correction(<<~RUBY)
begin
something
Expand Down
22 changes: 10 additions & 12 deletions spec/rubocop/cop/lint/empty_interpolation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@
RSpec.describe RuboCop::Cop::Lint::EmptyInterpolation do
subject(:cop) { described_class.new }

it 'registers an offense for #{} in interpolation' do
it 'registers an offense and corrects #{} in interpolation' do
expect_offense(<<-'RUBY'.strip_indent)
"this is the #{}"
^^^ Empty interpolation detected.
RUBY

expect_correction(<<-'RUBY'.strip_indent)
"this is the "
RUBY
end

it 'registers an offense for #{ } in interpolation' do
it 'registers an offense and corrects #{ } in interpolation' do
expect_offense(<<-'RUBY'.strip_indent)
"this is the #{ }"
^^^^ Empty interpolation detected.
RUBY

expect_correction(<<-'RUBY'.strip_indent)
"this is the "
RUBY
end

it 'finds interpolations in string-like contexts' do
Expand All @@ -31,14 +39,4 @@
it 'accepts non-empty interpolation' do
expect_no_offenses('"this is #{top} silly"')
end

it 'autocorrects empty interpolation' do
new_source = autocorrect_source('"this is the #{}"')
expect(new_source).to eq('"this is the "')
end

it 'autocorrects empty interpolation containing a space' do
new_source = autocorrect_source('"this is the #{ }"')
expect(new_source).to eq('"this is the "')
end
end
40 changes: 16 additions & 24 deletions spec/rubocop/cop/lint/inherit_exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,55 @@
context 'with enforced style set to `runtime_error`' do
let(:cop_config) { { 'EnforcedStyle' => 'runtime_error' } }

it 'registers an offense' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
class C < Exception; end
^^^^^^^^^ Inherit from `RuntimeError` instead of `Exception`.
RUBY
end

it 'auto-corrects' do
corrected = autocorrect_source('class C < Exception; end')

expect(corrected).to eq('class C < RuntimeError; end')
expect_correction(<<~RUBY)
class C < RuntimeError; end
RUBY
end

context 'when creating a subclass using Class.new' do
it 'registers an offense' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
Class.new(Exception)
^^^^^^^^^ Inherit from `RuntimeError` instead of `Exception`.
RUBY
end

it 'auto-corrects' do
corrected = autocorrect_source('Class.new(Exception)')

expect(corrected).to eq('Class.new(RuntimeError)')
expect_correction(<<~RUBY)
Class.new(RuntimeError)
RUBY
end
end
end

context 'with enforced style set to `standard_error`' do
let(:cop_config) { { 'EnforcedStyle' => 'standard_error' } }

it 'registers an offense' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
class C < Exception; end
^^^^^^^^^ Inherit from `StandardError` instead of `Exception`.
RUBY
end

it 'auto-corrects' do
corrected = autocorrect_source('class C < Exception; end')

expect(corrected).to eq('class C < StandardError; end')
expect_correction(<<~RUBY)
class C < StandardError; end
RUBY
end

context 'when creating a subclass using Class.new' do
it 'registers an offense' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
Class.new(Exception)
^^^^^^^^^ Inherit from `StandardError` instead of `Exception`.
RUBY
end

it 'auto-corrects' do
corrected = autocorrect_source('Class.new(Exception)')

expect(corrected).to eq('Class.new(StandardError)')
expect_correction(<<~RUBY)
Class.new(StandardError)
RUBY
end
end
end
Expand Down
59 changes: 27 additions & 32 deletions spec/rubocop/cop/lint/ordered_magic_comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,62 @@
RSpec.describe RuboCop::Cop::Lint::OrderedMagicComments, :config do
subject(:cop) { described_class.new(config) }

it 'registers an offense when `encoding` magic comment does not ' \
'precede all other magic comments' do
it 'registers an offense and corrects when an `encoding` magic comment ' \
'does not precede all other magic comments' do
expect_offense(<<~RUBY)
# frozen_string_literal: true
# encoding: ascii
^^^^^^^^^^^^^^^^^ The encoding magic comment should precede all other magic comments.
RUBY

expect_correction(<<~RUBY)
# encoding: ascii
# frozen_string_literal: true
RUBY
end

it 'registers an offense when `coding` magic comment ' \
it 'registers an offense and corrects when `coding` magic comment ' \
'does not precede all other magic comments' do
expect_offense(<<~RUBY)
# frozen_string_literal: true
# coding: ascii
^^^^^^^^^^^^^^^ The encoding magic comment should precede all other magic comments.
RUBY

expect_correction(<<~RUBY)
# coding: ascii
# frozen_string_literal: true
RUBY
end

it 'registers an offense when `-*- encoding : ascii-8bit -*-` ' \
it 'registers an offense and corrects when `-*- encoding : ascii-8bit -*-` ' \
'magic comment does not precede all other magic comments' do
expect_offense(<<~RUBY)
# frozen_string_literal: true
# -*- encoding : ascii-8bit -*-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The encoding magic comment should precede all other magic comments.
RUBY

expect_correction(<<~RUBY)
# -*- encoding : ascii-8bit -*-
# frozen_string_literal: true
RUBY
end

it 'registers an offense when using `frozen_string_literal` magic comment ' \
'is next of shebang' do
it 'registers an offense and corrects when using `frozen_string_literal` ' \
'magic comment is next of shebang' do
expect_offense(<<~RUBY)
#!/usr/bin/env ruby
# frozen_string_literal: true
# encoding: ascii
^^^^^^^^^^^^^^^^^ The encoding magic comment should precede all other magic comments.
RUBY

expect_correction(<<~RUBY)
#!/usr/bin/env ruby
# encoding: ascii
# frozen_string_literal: true
RUBY
end

it 'does not register an offense when using `encoding` magic comment ' \
Expand Down Expand Up @@ -80,30 +101,4 @@
puts x
RUBY
end

it 'autocorrects ordered magic comments' do
new_source = autocorrect_source(<<~RUBY)
# frozen_string_literal: true
# encoding: ascii
RUBY

expect(new_source).to eq <<~RUBY
# encoding: ascii
# frozen_string_literal: true
RUBY
end

it 'autocorrects ordered magic comments with shebang' do
new_source = autocorrect_source(<<~RUBY)
#!/usr/bin/env ruby
# frozen_string_literal: true
# encoding: ascii
RUBY

expect(new_source).to eq <<~RUBY
#!/usr/bin/env ruby
# encoding: ascii
# frozen_string_literal: true
RUBY
end
end
Loading

0 comments on commit 5657091

Please sign in to comment.