module RuboCop::RSpec::ExpectOffense
def expect_correction(correction)
def expect_correction(correction) unless @processed_source raise '`expect_correction` must follow `expect_offense`' end corrector = RuboCop::Cop::Corrector.new(@processed_source.buffer, cop.corrections) new_source = corrector.rewrite expect(new_source).to eq(correction) end
def expect_no_corrections
def expect_no_corrections unless @processed_source raise '`expect_no_corrections` must follow `expect_offense`' end return if cop.corrections.empty? # In order to print a nice diff, e.g. what source got corrected to, # we need to run the actual corrections corrector = RuboCop::Cop::Corrector.new(@processed_source.buffer, cop.corrections) new_source = corrector.rewrite expect(new_source).to eq(@processed_source.buffer.source) end
def expect_no_offenses(source, file = nil)
def expect_no_offenses(source, file = nil) inspect_source(source, file) expect(cop.offenses).to be_empty end
def expect_offense(source, file = nil)
def expect_offense(source, file = nil) RuboCop::Formatter::DisabledConfigFormatter .config_to_allow_offenses = {} RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {} cop.instance_variable_get(:@options)[:auto_correct] = true expected_annotations = AnnotatedSource.parse(source) if expected_annotations.plain_source == source raise 'Use `expect_no_offenses` to assert that no offenses are found' end @processed_source = parse_source(expected_annotations.plain_source, file) unless @processed_source.valid_syntax? raise 'Error parsing example code' end _investigate(cop, @processed_source) actual_annotations = expected_annotations.with_offense_annotations(cop.offenses) expect(actual_annotations.to_s).to eq(expected_annotations.to_s) end