forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for
expect_no_corrections
and expect_correction
loop be…
…haviour The change for this was made in rubocop#12763 and rubocop#12774
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 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
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 |
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 |
---|---|---|
@@ -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
24
spec/support/cops/infinite_loop_during_autocorrect_with_change.rb
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 |
---|---|---|
@@ -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 |