Sha256: 19d48e905571ff00a114666fd873caf50bbe6114e6edba4f5d25ec7a0650f1b0
Contents?: true
Size: 1.4 KB
Versions: 8
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 offence for #{method} with block" do inspect_source(cop, ["[1, 2, 3].#{method} { |e| e + 1 }"]) expect(cop.offences.size).to eq(1) expect(cop.messages) .to eq(["Prefer #{preferred_method} over #{method}."]) end it "registers an offence for #{method} with proc param" do inspect_source(cop, ["[1, 2, 3].#{method}(&:test)"]) expect(cop.offences.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.offences).to be_empty end it "accepts #{method} without a block" do inspect_source(cop, ["[1, 2, 3].#{method}"]) expect(cop.offences).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
8 entries across 8 versions & 2 rubygems