Sha256: 806643b892108a81ed1ff0f97b15867e29e1e649dd49474033a35b6c0332e90f

Contents?: true

Size: 1.47 KB

Versions: 17

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

require "rubocop"
require "rubocop/cop/legacy/corrector"

module RubocopHelper
  attr_accessor :cop

  private

  RUBY_VERSION = 2.5
  private_constant :RUBY_VERSION

  def assert_no_offenses(source)
    investigate(RuboCop::ProcessedSource.new(source, RUBY_VERSION, nil))
    assert_predicate(cop.offenses, :empty?, "Did not expect Rubocop to find offenses")
  end

  def assert_offense(source)
    investigate(RuboCop::ProcessedSource.new(source, RUBY_VERSION, nil))
    refute_predicate(cop.offenses, :empty?, "Expected Rubocop to find offenses")
  end

  def assert_no_autocorrect(source)
    corrected = autocorrect_source(source)
    assert_equal(source, corrected)
  end

  def autocorrect_source(source)
    RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
    RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
    cop.instance_variable_get(:@options)[:auto_correct] = true

    processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION, nil)
    investigate(processed_source)

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

  def investigate(processed_source)
    forces = RuboCop::Cop::Force.all.each_with_object([]) do |klass, instances|
      instances << klass.new([cop])
    end

    commissioner = RuboCop::Cop::Commissioner.new([cop], forces, raise_error: true)
    commissioner.investigate(processed_source)
    commissioner
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
statsd-instrument-3.5.9 test/helpers/rubocop_helper.rb
statsd-instrument-3.5.8 test/helpers/rubocop_helper.rb
statsd-instrument-3.5.7 test/helpers/rubocop_helper.rb
statsd-instrument-3.5.6 test/helpers/rubocop_helper.rb
statsd-instrument-3.5.5 test/helpers/rubocop_helper.rb
statsd-instrument-3.5.4 test/helpers/rubocop_helper.rb
statsd-instrument-3.5.3 test/helpers/rubocop_helper.rb
statsd-instrument-3.5.2 test/helpers/rubocop_helper.rb
statsd-instrument-3.5.1 test/helpers/rubocop_helper.rb
statsd-instrument-3.5.0 test/helpers/rubocop_helper.rb
statsd-instrument-3.4.0 test/helpers/rubocop_helper.rb
statsd-instrument-3.3.0 test/helpers/rubocop_helper.rb
statsd-instrument-3.2.1 test/helpers/rubocop_helper.rb
statsd-instrument-3.2.0 test/helpers/rubocop_helper.rb
statsd-instrument-3.1.2 test/helpers/rubocop_helper.rb
statsd-instrument-3.1.1 test/helpers/rubocop_helper.rb
statsd-instrument-3.1.0 test/helpers/rubocop_helper.rb