lib/rubocop/minitest/assert_offense.rb in rubocop-minitest-0.35.1 vs lib/rubocop/minitest/assert_offense.rb in rubocop-minitest-0.36.0
- old
+ new
@@ -73,15 +73,17 @@
#
# rubocop:disable Metrics/ModuleLength
module AssertOffense
private
- def setup
- cop_name = self.class.to_s.delete_suffix('Test')
- return unless RuboCop::Cop::Minitest.const_defined?(cop_name)
+ def cop
+ @cop ||= begin
+ cop_name = self.class.to_s.delete_suffix('Test')
+ raise "Cop not defined: #{cop_name}" unless RuboCop::Cop::Minitest.const_defined?(cop_name)
- @cop = RuboCop::Cop::Minitest.const_get(cop_name).new(configuration)
+ RuboCop::Cop::Minitest.const_get(cop_name).new(configuration)
+ end
end
def format_offense(source, **replacements)
replacements.each do |keyword, value|
value = value.to_s
@@ -93,32 +95,31 @@
end
def assert_no_offenses(source, file = nil)
setup_assertion
- offenses = inspect_source(source, @cop, file)
+ offenses = inspect_source(source, cop, file)
expected_annotations = RuboCop::RSpec::ExpectOffense::AnnotatedSource.parse(source)
actual_annotations = expected_annotations.with_offense_annotations(offenses)
assert_equal(source, actual_annotations.to_s)
end
def assert_offense(source, file = nil, **replacements)
setup_assertion
+ enable_autocorrect
- @cop.instance_variable_get(:@options)[:autocorrect] = true
-
source = format_offense(source, **replacements)
expected_annotations = RuboCop::RSpec::ExpectOffense::AnnotatedSource.parse(source)
if expected_annotations.plain_source == source
raise 'Use `assert_no_offenses` to assert that no offenses are found'
end
@processed_source = parse_source!(expected_annotations.plain_source, file)
- @offenses = _investigate(@cop, @processed_source)
+ @offenses = _investigate(cop, @processed_source)
actual_annotations = expected_annotations.with_offense_annotations(@offenses)
assert_equal(expected_annotations.to_s, actual_annotations.to_s)
end
@@ -128,10 +129,14 @@
report = team.investigate(processed_source)
@last_corrector = report.correctors.first || RuboCop::Cop::Corrector.new(processed_source)
report.offenses
end
+ def enable_autocorrect
+ cop.instance_variable_get(:@options)[:autocorrect] = true
+ end
+
def assert_correction(correction, loop: true)
raise '`assert_correction` must follow `assert_offense`' unless @processed_source
iteration = 0
new_source = loop do
@@ -147,10 +152,10 @@
end
# Prepare for next loop
@processed_source = parse_source!(corrected_source, @processed_source.path)
- _investigate(@cop, @processed_source)
+ _investigate(cop, @processed_source)
end
assert_equal(correction, new_source)
end