Skip to content

Commit

Permalink
Add looping to expect_correction
Browse files Browse the repository at this point in the history
In some places, we spec what happens after one auto-correct iteration
and what happens after _many_ auto-correct iterations.
  • Loading branch information
bquorning authored and bbatsov committed Jun 1, 2020
1 parent 09ff7ea commit e4af299
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions lib/rubocop/rspec/expect_offense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,35 @@ def expect_offense(source, file = nil, **replacements)

expect(actual_annotations.to_s).to eq(expected_annotations.to_s)
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

def expect_correction(correction)
def expect_correction(correction, loop: false)
raise '`expect_correction` must follow `expect_offense`' unless @processed_source

corrector =
RuboCop::Cop::Corrector.new(@processed_source.buffer, cop.corrections)
new_source = corrector.rewrite
iteration = 0
new_source = loop do
iteration += 1

corrector =
RuboCop::Cop::Corrector.new(@processed_source.buffer, cop.corrections)
corrected_source = corrector.rewrite

break corrected_source unless loop
break corrected_source if cop.corrections.empty?

if iteration > RuboCop::Runner::MAX_ITERATIONS
raise RuboCop::Runner::InfiniteCorrectionLoop.new(@processed_source.path, [])
end

# Prepare for next loop
cop.instance_variable_set(:@corrections, [])
@processed_source = parse_source(corrected_source,
@processed_source.path)
_investigate(cop, @processed_source)
end

expect(new_source).to eq(correction)
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

def expect_no_corrections
raise '`expect_no_corrections` must follow `expect_offense`' unless @processed_source
Expand Down

0 comments on commit e4af299

Please sign in to comment.