Skip to content

Commit

Permalink
Add tests for expect_no_corrections and expect_correction loop be…
Browse files Browse the repository at this point in the history
…haviour

The change for this was made in rubocop#12763 and rubocop#12774
  • Loading branch information
Earlopain committed Mar 11, 2024
1 parent eceedb7 commit aae1baa
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
36 changes: 36 additions & 0 deletions spec/rubocop/rspec_expect_offense_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

RSpec.describe RuboCop::RSpec, :config do
context 'with a cop that loops during autocorrection' do
let(:cop_class) { RuboCop::Cop::Test::InfiniteLoopDuringAutocorrectCop }

it '`expect_no_corrections` raises' do
expect_offense(<<~RUBY)
class Test
^^^^^^^^^^ Class must be a Module
end
RUBY

expect { expect_no_corrections }.to raise_error(RuboCop::Runner::InfiniteCorrectionLoop)
end
end

context 'with a cop that loops after autocorrecting something' do
let(:cop_class) { RuboCop::Cop::Test::InfiniteLoopDuringAutocorrectWithChangeCop }

it '`expect_correction` raises' do
expect_offense(<<~RUBY)
class Test
^^^^^^^^^^ Class must be a Module
end
RUBY

expect do
expect_correction(<<~RUBY)
module Test
end
RUBY
end.to raise_error(RuboCop::Runner::InfiniteCorrectionLoop)
end
end
end
18 changes: 18 additions & 0 deletions spec/support/cops/infinite_loop_during_autocorrect.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Test
class InfiniteLoopDuringAutocorrectCop < RuboCop::Cop::Base
extend AutoCorrector

def on_class(node)
add_offense(node, message: 'Class must be a Module') do |corrector|
# Replace the offense with itself, will be picked up again next loop
corrector.replace(node, node.source)
end
end
end
end
end
end
24 changes: 24 additions & 0 deletions spec/support/cops/infinite_loop_during_autocorrect_with_change.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Test
class InfiniteLoopDuringAutocorrectWithChangeCop < RuboCop::Cop::Base
extend AutoCorrector

def on_class(node)
add_offense(node, message: 'Class must be a Module') do |corrector|
corrector.replace(node.loc.keyword, 'module')
end
end

def on_module(node)
add_offense(node, message: 'Module must be a Class') do |corrector|
# Will register an offense during the next loop again
corrector.replace(node, node.source)
end
end
end
end
end
end

0 comments on commit aae1baa

Please sign in to comment.