Sha256: f80b331760baabe8934886dbf988c2c565192f1065ccff21c6585b09b21281e0

Contents?: true

Size: 1.41 KB

Versions: 11

Compression:

Stored size: 1.41 KB

Contents

# encoding: utf-8

require 'spec_helper'

describe RuboCop::Cop::Style::CollectionMethods, :config do
  cop_config = {
    'PreferredMethods' => {
      'collect' => 'map',
      'inject' => 'reduce',
      'detect' => 'find',
      'find_all' => 'select'
    }
  }

  subject(:cop) { described_class.new(config) }
  let(:cop_config) { cop_config }

  cop_config['PreferredMethods'].each do |method, preferred_method|
    it "registers an offense for #{method} with block" do
      inspect_source(cop, ["[1, 2, 3].#{method} { |e| e + 1 }"])
      expect(cop.offenses.size).to eq(1)
      expect(cop.messages)
        .to eq(["Prefer `#{preferred_method}` over `#{method}`."])
    end

    it "registers an offense for #{method} with proc param" do
      inspect_source(cop, ["[1, 2, 3].#{method}(&:test)"])
      expect(cop.offenses.size).to eq(1)
      expect(cop.messages)
        .to eq(["Prefer `#{preferred_method}` over `#{method}`."])
    end

    it "accepts #{method} with more than 1 param" do
      inspect_source(cop, ["[1, 2, 3].#{method}(other, &:test)"])
      expect(cop.offenses).to be_empty
    end

    it "accepts #{method} without a block" do
      inspect_source(cop, ["[1, 2, 3].#{method}"])
      expect(cop.offenses).to be_empty
    end

    it 'auto-corrects to preferred method' do
      new_source = autocorrect_source(cop, 'some.collect(&:test)')
      expect(new_source).to eq('some.map(&:test)')
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/spec/rubocop/cop/style/collection_methods_spec.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/spec/rubocop/cop/style/collection_methods_spec.rb
rubocop-0.28.0 spec/rubocop/cop/style/collection_methods_spec.rb
rubocop-0.27.1 spec/rubocop/cop/style/collection_methods_spec.rb
rubocop-0.27.0 spec/rubocop/cop/style/collection_methods_spec.rb
rubocop-0.26.1 spec/rubocop/cop/style/collection_methods_spec.rb
rubocop-0.26.0 spec/rubocop/cop/style/collection_methods_spec.rb
rubocop-0.25.0 spec/rubocop/cop/style/collection_methods_spec.rb
rubocop-0.24.1 spec/rubocop/cop/style/collection_methods_spec.rb
rubocop-0.24.0 spec/rubocop/cop/style/collection_methods_spec.rb
rubocop-0.23.0 spec/rubocop/cop/style/collection_methods_spec.rb