Sha256: 5587baa7ba75b2a461d72f9e6c764cb45ff078c0e55cd97d9f0270e187fcdef2

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 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

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.29.1 spec/rubocop/cop/style/collection_methods_spec.rb
rubocop-0.29.0 spec/rubocop/cop/style/collection_methods_spec.rb